Отобразите прогноз на 10 лет с использованием полиномиальной регрессии на python - PullRequest
0 голосов
/ 11 июля 2020
• 1000

Пока искал, чтобы построить его, я нашел это выражение, которое помогает мне делать прогноз на годы lin.predict (poly.fit_transform ([[2018]])) , но есть ли Как я могу сделать прогноз до 2020 года и построить график с этими значениями прогноза?

#Linear regression for one country
df=Countries_New.transpose()
x=df.index.values.reshape(-1,1)

#Choose here the country name to make the prediction
Country_Name = 'Austria'


try:
    print(f'The chosen country is {Country_Name}')
    print('')
    y=df[Country_Name].values.reshape(-1,1)
    for i in range(1,5):
        #print('The degree of the equation is: '+str(i))

        #Fit the Poly regression
        poly = PolynomialFeatures(degree = i)
        x_poly = poly.fit_transform(x)
        poly.fit(x_poly,y)

        lin=LinearRegression()
        lin.fit(x_poly,y)
        y_poly_pred = lin.predict(x_poly)

        rmse = np.sqrt(mean_squared_error(y,y_poly_pred))
        r2 = r2_score(y,y_poly_pred)
        print('The RMSE is {} and R2 is {}'. format(rmse, r2))
        #print('')
        #print('The generation (original value) for 2018 is {}'.format(y[-1]))
        #print('The prediction for 2018 is {}'.format(lin.predict(poly.fit_transform([[2018]]))))
        #print('The prediction for 2019 is {}'.format(lin.predict(poly.fit_transform([[2019]]))))
        #print('The prediction for 2020 is {}'.format(lin.predict(poly.fit_transform([[2020]]))))

        #for i in range(2019,2029):
        #    x_poly.append(i,lin.predict(poly.fit_transform([[2020]])),axis=1)
        
        #Plot Poly regression
        plt.scatter(x,y,color='blue')
        plt.plot(x,lin.predict(x_poly),color='red')
        plt.title('Polynomial Regression degree '+str(i))
        plt.xlabel('Year')
        plt.ylabel('Renewable Generation (TWh)')
        plt.show()

except:
    print('Country not found')

enter image description here введите описание изображения здесь

...