繁体   English   中英

如何在 python 中使用 plot 移动平均线?

[英]How do I plot a moving average in python?

如何制作 y_pred_org 和 plot 的移动平均线呢? 我已经通过以下方式进行了尝试,但收到了错误 AttributeError: 'list' object has no attribute 'rolling' 我确信这有点小,但我对此并不陌生。

# Visualize the prediction with rolling average
from matplotlib import pyplot as plt
plt.figure()
['y_pred_org'].rolling(window=50).mean().plot()
plt.plot(y_test_t_org)
plt.title('Prediction vs Real Stock Price')
plt.ylabel('Price')
plt.xlabel('Days')
plt.legend(['Prediction', 'Real'], loc='upper left')
#plt.show()

我应该如何正确地将其调整为 function? 以下代码将 function 正确。如果需要相移,也可以添加移位(您需要的任何移位)

# Visualize the prediction with rolling average
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('Prediction vs Real Stock Price')
plt.ylabel('Price')
plt.xlabel('Days')
plt.legend(['Prediction', 'Real'], loc='upper left')
#plt.show()

如果您的数据在 pandas DataFrame 中,您可以使用内置的滚动平均值。

df['Y_Predict'] = df.iloc[:,col].rolling(window=5).mean()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM