如何使用Python绘制多项式回归并计算均方误差?

时间:2020-02-28 22:53:51

标签: python pandas dataframe regression plotly

我有一个使用数据框的应用程序,其感兴趣的列是pm2.5值和日期值。我在数据框中注册了超过43.000个值,目标是使用多项式模型回归来预测pm2.5的值。 我必须将70%的数据用作训练,将30%的数据用作测试,并比较实际值和预测值之间的值。为此,我使用均方误差。 到目前为止,我已经成功地训练了模型并预测了数据,但问题是,无论我为多项式使用的等级如何,图几乎都不会改变,而且均方误差也不会改变。我认为多项式的等级越大,预测越好,因此图越好。 如何在修改坡度时修改代码以获得不同的地块值?而且,要获得不同的均方误差!

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
import plotly.express as px
from datetime import datetime
from sklearn.metrics import mean_squared_error

def parser(x):
    return datetime.strptime(x, '%d/%m/%Y')

data = pd.read_csv("https://raw.githubusercontent.com/iulianastroia/csv_data/master/final_dataframe.csv")

data['day'] = pd.to_datetime(data['day'], dayfirst=True)
data = data.sort_values(by=['day'])

X = data['time'].values.reshape(-1, 1)
y = data['pm25'].values.reshape(-1, 1)

# Splitting the dataset into training(70%) and test(30%)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
                                                    shuffle=False)

# Fitting Polynomial Regression to the dataset
poly_reg = PolynomialFeatures(degree=4)#if i put grade=20, almost nothing changes; 
#also, grade=1 doesn't create linear regression
X_poly = poly_reg.fit_transform(X_train)
print("x poly", X_poly)
pol_reg = LinearRegression()
pol_reg.fit(X_poly, y_train)

x_test_transformed = []
for i in X_test:
    print("*", i)
    x_test_transformed.append(i)

dfObj = pd.DataFrame(x_test_transformed)
dfObj.columns = ['x_test_transformed']
dfObj['color'] = 2 #red color for plot
data['color'] = 2#red color for plot

fig = px.line(x=data['readable time'], y=y)
fig.update_traces(name='Actual Data', showlegend=True, mode='lines+markers')
print("LEN", len(X_test))
print("LEN", len(data))
fig.add_trace(
    px.scatter(x=data['readable time'], y=pol_reg.predict(poly_reg.fit_transform(X)), color=data['color']).data[0])
y=pol_reg.predict(poly_reg.fit_transform(X_test))
fig.update_traces(name='Predicted Data', showlegend=True)
fig.update_layout(coloraxis_showscale=False, title='Real data and forecast for November 2019', showlegend=True,yaxis_title='Pm2.5', xaxis_title='Day')

fig.show()

predicted_list = pol_reg.predict(poly_reg.fit_transform(X))
predicted_list = [arr.tolist() for arr in predicted_list]
print("MSE(mean squared error)", mean_squared_error(data['pm25'], predicted_list))

0 个答案:

没有答案
相关问题