Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用sklearn线性回归拟合时间序列+;策划_Python_Pandas_Plot - Fatal编程技术网

Python 使用sklearn线性回归拟合时间序列+;策划

Python 使用sklearn线性回归拟合时间序列+;策划,python,pandas,plot,Python,Pandas,Plot,get_DP()输出了以下时间序列: 注意:1900-2015年间每个月都有一个DP值,我只是将其排除在外以避免混乱 我想在这个数据框架上使用一个简单的回归来计算这个金融变量的alpha和beta(截距和系数)。我有以下代码打算这样做: reg = linear_model.LinearRegression() df = get_DP() df=df.reset_index() reg.fit(df['date'].values.reshape((1389,1)), df['DP'].value

get_DP()输出了以下时间序列:

注意:1900-2015年间每个月都有一个DP值,我只是将其排除在外以避免混乱

我想在这个数据框架上使用一个简单的回归来计算这个金融变量的alpha和beta(截距和系数)。我有以下代码打算这样做:

reg = linear_model.LinearRegression()
df = get_DP()
df=df.reset_index()
reg.fit(df['date'].values.reshape((1389,1)), df['DP'].values)
print("beta: {}".format(reg.coef_))
print("alpha: {}".format(reg.intercept_))
plt.scatter(df['date'].values.reshape((1389,1)), df['DP'].values,  color='black')
plt.plot(df['date'].values.reshape((1389,1)), df['DP'].values, color='blue', linewidth=3)
然而,我认为对x轴数据(日期)的重塑会弄乱整个回归,因为曲线图是这样的:

我犯错误了吗?由于pandas用0.20删除了OLS函数,我不完全确定使用数据帧进行回归的最佳工具是什么。

试试这个

reg=linear\u model.LinearRegression()
df=get_DP()
df=df.reset_index()
调整拟合(df.date.values.reformate(-1,1),df.DP.values.reformate(-1,1))
打印(“beta:{}”.format(reg.coefè))
打印(“alpha:{}”.format(reg.intercept))
plt.散射(df.date.dt.date,df.DP.values,color='black')
plt.绘图(df.date.dt.date,df.DP.values,color='blue',线宽=3)
请参见试试这个

reg=linear\u model.LinearRegression()
df=get_DP()
df=df.reset_index()
调整拟合(df.date.values.reformate(-1,1),df.DP.values.reformate(-1,1))
打印(“beta:{}”.format(reg.coefè))
打印(“alpha:{}”.format(reg.intercept))
plt.散射(df.date.dt.date,df.DP.values,color='black')
plt.绘图(df.date.dt.date,df.DP.values,color='blue',线宽=3)

在我看来,这不是回归的问题,而是情节的维度问题。如果重塑x轴的形状,点可能会显得不那么垂直;)我不完全确定,我在这篇文章中排除了很多数据来消除混乱,但从1900年到2015年,每个月底都有一个DP值。在整个x轴上难道不应该有一个点吗?在我看来,这不是回归的问题,而是绘图的维度问题。如果重塑x轴的形状,点可能会显得不那么垂直;)我不完全确定,我在这篇文章中排除了很多数据来消除混乱,但从1900年到2015年,每个月底都有一个DP值。在整个x轴上不应该有一个点吗?所以重塑才是问题所在,但我不确定这意味着什么或改变了什么。对于这些数据帧,这些值的意义是什么?但主要问题是
date
plot不能与
datetime64[ns]
一起使用。或者试试看,这很有帮助。你的意思是
重塑(-1,1)
?它只是将形状设置为向量(列)而不是行。”“值”是1D np.array,但
sklearn
需要矩阵。因此,问题在于重塑,但我不确定它的含义或更改。对于这些数据帧,这些值的意义是什么?但主要问题是
date
plot不能与
datetime64[ns]
一起使用。或者试试看,这很有帮助。你的意思是
重塑(-1,1)
?它只是将形状设置为向量(列)而不是行。”“值”是1D np.array,但
sklearn
需要矩阵。
reg = linear_model.LinearRegression()
df = get_DP()
df=df.reset_index()
reg.fit(df['date'].values.reshape((1389,1)), df['DP'].values)
print("beta: {}".format(reg.coef_))
print("alpha: {}".format(reg.intercept_))
plt.scatter(df['date'].values.reshape((1389,1)), df['DP'].values,  color='black')
plt.plot(df['date'].values.reshape((1389,1)), df['DP'].values, color='blue', linewidth=3)