How to plot predicted values with actual values when we have multi-index

Viewed 6611

I am having time-series data I am trying to predict spot prices for next-day. My data looks like:enter image description here

I did groupby on f_area and I end up with multiindex. Now I am trying to do predictions using RandomForestRegressor.

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error

y = area3['y'].values
X = area3[['f_price', 'day_of_week', 'day_of_month']]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)

model = RandomForestRegressor()
model = model.fit(X_train, y_train)
y_pred = model.predict(X_test)

Now when I try to plot y_test(actual values) and y_pred(predicted values)

fig, ax = plt.subplots()
ax.plot(y_test)
ax.plot(y_pred)

I got this graph. enter image description here

What I want need is to have date on X axis and I am unable to so it because of the multi-index. How can I do this or remove the multiindex? I tried to remove the multiindex via reset_index but it doesn't work in my case Thanks

1 Answers

First you take one variable and store the date column into list form

Note: make sure your list c length and y_pred, y-test size are same.

fig=plt.figure()
c=area3['f_date'].to_list()
plt.plot(c,y_test)
plt.plot(c,y_pred)
plt.show()
Related