1

How do I make a moving average of y_pred_org and plot it? I have tried this in the following way but receive an error AttributeError: 'list' object has no attribute 'rolling' I am sure this is something small but I am new to this.

# 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()

How should I adjust this for it to function properly? The following code will function properly .shift(whatever shift you need) can also be added if a phase shift is needed

# 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()
1

1 Answer 1

3

If your data is in a pandas DataFrame you can use a built in rolling average.

df['Y_Predict'] = df.iloc[:,col].rolling(window=5).mean()
Sign up to request clarification or add additional context in comments.

5 Comments

I have edited the question to attempt to show what this has led me to try. Let me know what you think.
Is that the full error message you receive? I think you need to pass the rolling average data as an argument plot().
The error was: AttributeError: 'list' object has no attribute 'rolling' with this code
Your data needs to be in a dataframe for my suggestion to work properly. See Pandas library
Thank you @KillerToilet both for your name and for helping me solve this problem!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.