如何计算y_pred_org的移动平均并绘制出来?我尝试过以下方法,但收到错误AttributeError: ‘list’ object has no attribute ‘rolling’。我确定这是一个小问题,但我对此很新手。
# 使用滚动平均值可视化预测
from matplotlib import pyplot as plt
plt.figure()
['y_pred_org'].rolling(window=50).mean().plot()
plt.plot(y_test_t_org)
plt.title('预测与实际股价')
plt.ylabel('价格')
plt.xlabel('天数')
plt.legend(['预测', '实际'], loc='upper left')
#plt.show()
我应该如何调整代码以使其正常工作?以下代码将正常运行。如果需要相位移,还可以添加.shift(所需的移位)。
# 使用滚动平均值可视化预测
from matplotlib import pyplot as plt
plt.figure()
df = DataFrame(data = y_pred_org)
df.rolling(30, center=True).mean().plot()
plt.plot(y_test_t_org)
plt.title('预测与实际股价')
plt.ylabel('价格')
plt.xlabel('天数')
plt.legend(['预测', '实际'], loc='upper left')
#plt.show()
回答:
如果您的数据在pandas DataFrame中,可以使用内置的滚动平均值。
df['Y_Predict'] = df.iloc[:,col].rolling(window=5).mean()