-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mutivariate_Poly_Reg_Forecast.py
129 lines (96 loc) · 3.42 KB
/
Mutivariate_Poly_Reg_Forecast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
# coding: utf-8
# In[56]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
import operator
from pandas.plotting import register_matplotlib_converters
# In[57]:
df = pd.read_csv('/home/sim06/Downloads/cer3.csv')
time = pd.date_range(start='1961',periods=56,freq='Y')
df.set_index(time,inplace=True)
df = df.drop(df[['No','month','day','year']],axis=1)
# In[58]:
X = df.drop(df[['Cereals']],axis=1)
Y = df[['Cereals']]
# In[59]:
register_matplotlib_converters(explicit=True)
# In[60]:
poly_model = PolynomialFeatures(degree=1)
x_poly = poly_model.fit_transform(X)
poly_model.fit(x_poly,Y['Cereals'])
MR_model = LinearRegression()
MR_model.fit(x_poly,Y['Cereals'])
X_predicted = MR_model.predict(x_poly)
rmse = np.sqrt(mean_squared_error(Y['Cereals'],X_predicted))
r2 = r2_score(Y['Cereals'],X_predicted)
print("Root Mean Square Error : ",rmse)
print("r2 score : ",r2)
s = np.asarray(Y.Cereals)
diff = X_predicted - s
#for i in range(len(diff)):
#print(diff[i])
ans = MR_model.predict(poly_model.fit_transform(X))
time1 = pd.date_range(start='1961',periods=56,freq='Y')
predictions = pd.DataFrame(ans,columns=['Cereals'])
predictions.set_index(time1,inplace=True)
plt.plot(predictions[['Cereals']],color='green',label='Predicted')
plt.plot(df[['Cereals']],color='red',label='Real')
plt.title("Polynomial of degree 1")
plt.legend()
plt.show()
#print('The predicted and true plots are not same this means that polynomial of degree 1 does not fits the given data.')
# In[61]:
poly_model = PolynomialFeatures(degree=2)
x_poly = poly_model.fit_transform(X)
poly_model.fit(x_poly,Y['Cereals'])
MR_model = LinearRegression()
MR_model.fit(x_poly,Y['Cereals'])
X_predicted = MR_model.predict(x_poly)
rmse = np.sqrt(mean_squared_error(Y['Cereals'],X_predicted))
r2 = r2_score(Y['Cereals'],X_predicted)
print("Root Mean Square Error : ",rmse)
print("r2 score : ",r2)
s = np.asarray(Y.Cereals)
diff = X_predicted - s
#for i in range(len(diff)):
#print(diff[i])
ans = MR_model.predict(poly_model.fit_transform(X))
time1 = pd.date_range(start='1961',periods=56,freq='Y')
predictions = pd.DataFrame(ans,columns=['Cereals'])
predictions.set_index(time1,inplace=True)
plt.plot(predictions[['Cereals']],color='green',label='Predicted')
plt.plot(df[['Cereals']],color='red',label='Real')
plt.title("Polynomial of degree 2")
plt.legend()
plt.show()
# In[62]:
poly_model = PolynomialFeatures(degree=3)
x_poly = poly_model.fit_transform(X)
poly_model.fit(x_poly,Y['Cereals'])
MR_model = LinearRegression()
MR_model.fit(x_poly,Y['Cereals'])
X_predicted = MR_model.predict(x_poly)
rmse = np.sqrt(mean_squared_error(Y['Cereals'],X_predicted))
r2 = r2_score(Y['Cereals'],X_predicted)
print("Root Mean Square Error : ",rmse)
print("r2 score : ",r2)
s = np.asarray(Y.Cereals)
diff = X_predicted - s
#for i in range(len(diff)):
#print(diff[i])
ans = MR_model.predict(poly_model.fit_transform(X))
time1 = pd.date_range(start='1961',periods=56,freq='Y')
predictions = pd.DataFrame(ans,columns=['Cereals'])
predictions.set_index(time1,inplace=True)
plt.plot(predictions[['Cereals']],color='green',label='Predicted')
plt.plot(df[['Cereals']],color='red',label='Real')
plt.title("Polynomial of degree 3")
plt.legend()
plt.show()
# In[ ]: