-
Notifications
You must be signed in to change notification settings - Fork 0
/
LearningModelGen.py
212 lines (160 loc) · 8.27 KB
/
LearningModelGen.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import os
import numpy as np
import pandas as pd
import sklearn
from IPython.core.display_functions import display
from sklearn.linear_model import RidgeCV
from sklearn.model_selection import train_test_split
from sklearn.svm import SVR
from sklearn.metrics import r2_score, mean_squared_error
import seaborn as sns
from sklearn import svm
import matplotlib.pyplot as plt
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import DotProduct, WhiteKernel
from sklearn import linear_model
from sklearn.kernel_ridge import KernelRidge
from sklearn import tree
from sklearn.ensemble import RandomForestRegressor
from sklearn.neural_network import MLPRegressor
from inspect import signature
TrainData = pd.read_csv('TrainData.csv')
X = TrainData.values[:, [2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]]
Y = TrainData.values[:, 23]
m = len(Y)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, random_state=1)
print('Total no of training examples (m) = %s \n' % (m))
model_ols = linear_model.LinearRegression(normalize=True)
model_ols.fit(X, Y)
print('OLS Model Complete, listing stats')
print('coef= ', model_ols.coef_)
print('intercept= ', model_ols.intercept_)
olsprede = pd.DataFrame(model_ols.predict(X), columns=['Predicted E'])
olsacte = pd.DataFrame(Y, columns=['Actual E'])
olsacte = olsacte.reset_index(drop=True) # Drop the index so that we can concat it, to create new dataframe
df_actual_vs_predicted = pd.concat([olsacte, olsprede], axis=1)
df_actual_vs_predicted.T
mse = mean_squared_error(olsprede, olsacte)
rmse = np.sqrt(mse)
print('OLS root mean squared error:', rmse)
krr = KernelRidge(alpha=1.0)
krr.fit(X, Y)
print('KRR Model Complete')
krrprede = pd.DataFrame(krr.predict(X), columns=['Predicted E'])
krracte = pd.DataFrame(Y, columns=['Actual E'])
krracte = krracte.reset_index(drop=True) # Drop the index so that we can concat it, to create new dataframe
krr_actual_vs_predicted = pd.concat([krracte, krrprede], axis=1)
krr_actual_vs_predicted.T
krrrmse = np.sqrt(mean_squared_error(krrprede, krracte))
print('KRR root mean squared error:', krrrmse)
regr = svm.SVR()
regr.fit(X, Y)
print('SVR Model Complete')
svrprede = pd.DataFrame(regr.predict(X), columns=['Predicted E'])
svracte = pd.DataFrame(Y, columns=['Actual E'])
svracte = krracte.reset_index(drop=True) # Drop the index so that we can concat it, to create new dataframe
svr_actual_vs_predicted = pd.concat([svracte, svrprede], axis=1)
svr_actual_vs_predicted.T
svrrmse = np.sqrt(mean_squared_error(svrprede, svracte))
print('SVR root mean squared error:', svrrmse)
gprkernel = DotProduct() + WhiteKernel()
gpr = GaussianProcessRegressor(kernel=gprkernel, random_state=0).fit(X, Y)
gpr.score(X, Y)
print('GPR Model Complete, score:', gpr.score(X, Y))
gprprede = pd.DataFrame(gpr.predict(X), columns=['Predicted E'])
gpracte = pd.DataFrame(Y, columns=['Actual E'])
gpracte = gpracte.reset_index(drop=True) # Drop the index so that we can concat it, to create new dataframe
gpr_actual_vs_predicted = pd.concat([gpracte, gprprede], axis=1)
gpr_actual_vs_predicted.T
gprrmse = np.sqrt(mean_squared_error(gprprede, gpracte))
print('GPR root mean squared error:', gprrmse)
clf = tree.DecisionTreeRegressor()
clf = clf.fit(X, Y)
print('DTR Model Complete')
clfprede = pd.DataFrame(clf.predict(X), columns=['Predicted E'])
clfacte = pd.DataFrame(Y, columns=['Actual E'])
clfacte = gpracte.reset_index(drop=True) # Drop the index so that we can concat it, to create new dataframe
clf_actual_vs_predicted = pd.concat([clfacte, clfprede], axis=1)
clf_actual_vs_predicted.T
clfrmse = np.sqrt(mean_squared_error(clfprede, clfacte))
print('DTR root mean squared error:', clfrmse)
rtr = RandomForestRegressor()
rtr = rtr.fit(X, Y)
print('RTR Model Complete')
rtrprede = pd.DataFrame(rtr.predict(X), columns=['Predicted E'])
rtracte = pd.DataFrame(Y, columns=['Actual E'])
rtracte = rtracte.reset_index(drop=True) # Drop the index so that we can concat it, to create new dataframe
rtr_actual_vs_predicted = pd.concat([rtracte, rtrprede], axis=1)
rtr_actual_vs_predicted.T
rtrrmse = np.sqrt(mean_squared_error(rtrprede, rtracte))
print('RTR root mean squared error:', rtrrmse)
rcv = RidgeCV()
rcv = rcv.fit(X, Y)
print('RCV Model Complete')
rcvprede = pd.DataFrame(rcv.predict(X), columns=['Predicted E'])
rcvacte = pd.DataFrame(Y, columns=['Actual E'])
rcvacte = rcvacte.reset_index(drop=True) # Drop the index so that we can concat it, to create new dataframe
rcv_actual_vs_predicted = pd.concat([rcvacte, rcvprede], axis=1)
rcv_actual_vs_predicted.T
rcvrmse = np.sqrt(mean_squared_error(rcvprede, rcvacte))
print('RCV root mean squared error:', rcvrmse)
mlpr = MLPRegressor(random_state=1, max_iter=500).fit(X_train, Y_train)
print('MLPR Model Complete, score:', mlpr.score(X_test, Y_test))
mlprprede = pd.DataFrame(mlpr.predict(X), columns=['Predicted E'])
mlpracte = pd.DataFrame(Y, columns=['Actual E'])
mlpracte = rtracte.reset_index(drop=True) # Drop the index so that we can concat it, to create new dataframe
mlpr_actual_vs_predicted = pd.concat([mlpracte, mlprprede], axis=1)
mlpr_actual_vs_predicted.T
mlprrmse = np.sqrt(mean_squared_error(mlprprede, mlpracte))
print('MLPR root mean squared error:', mlprrmse)
class GenModel:
TotalTraining = pd.DataFrame(columns=['Tr Predicted E', 'Tr Actual E'])
TotalTesting = pd.DataFrame(columns=['Te Predicted E', 'Te Actual E'])
Totalrmse = pd.DataFrame(columns=['Training RMSE', 'Testing RMSE'])
def __init__(self, model):
for ModelN in range(10):
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, random_state=ModelN)
model.fit(X_train, Y_train)
# Training Array Maker
TrGMprede = pd.DataFrame(model.predict(X_train), columns=['Tr Predicted E'])
TrGMacte = pd.DataFrame(Y_train, columns=['Tr Actual E'])
TrGMacte = TrGMacte.reset_index(drop=True) # Drop the index so that we can concat it, to create new
# dataframe
TrGM_actual_vs_predicted = pd.concat([TrGMacte, TrGMprede], axis=1)
self.TotalTraining = pd.concat([self.TotalTraining, TrGM_actual_vs_predicted], axis=0)
TrGMrmse = np.sqrt(mean_squared_error(TrGMprede, TrGMacte))
# Test Array Maker
TeGMprede = pd.DataFrame(model.predict(X_test), columns=['Te Predicted E'])
TeGMacte = pd.DataFrame(Y_test, columns=['Te Actual E'])
TeGMacte = TeGMacte.reset_index(drop=True) # Drop the index so that we can concat it, to create new
# dataframe
TeGM_actual_vs_predicted = pd.concat([TeGMacte, TeGMprede], axis=1)
self.TotalTesting = pd.concat([self.TotalTesting, TeGM_actual_vs_predicted], axis=0)
TeGMrmse = np.sqrt(mean_squared_error(TeGMprede, TeGMacte))
self.Totalrmse.loc[len(self.Totalrmse.index)] = [TrGMrmse, TeGMrmse]
continue
def ReturnArray(self, model):
# printing arrays for debug
# print(self.TotalTraining)
# print(self.TotalTesting)
self.Totalrmse.loc['mean'] = self.Totalrmse.mean()
# print(self.Totalrmse)
# print model completion
print(model, 'generation complete.')
# saving arrays for future use
isExist = os.path.exists('Models/' + model)
if not isExist:
# Create a new directory because it does not exist
os.makedirs('Models/' + model)
self.TotalTraining.to_csv('Models/' + model + '/TrainArray.csv', index=False)
self.TotalTesting.to_csv('Models/' + model + '/TestArray.csv', index=False)
self.Totalrmse.to_csv('Models/' + model + '/RMSEArray.csv', index=False)
print(model, 'arrays saved.')
# print('Model:', model, 'Training Root Mean Squared Error:',
# np.sqrt(mean_squared_error(self.TotalTraining.iloc[0], self.TotalTraining.iloc[1])))
# print('Model:', model, 'Testing Root Mean Squared Error:',
# np.sqrt(mean_squared_error(self.TotalTesting.iloc[0], self.TotalTesting.iloc[1])))
GenModel(RidgeCV()).ReturnArray("RidgeCV")
# # To do: loop training and prediction 10 times, redo LearningModelGel with classes, add prediction and use split
# training data, graphs, predict our MatGenOutput and create array or graph, statistics.
# graph of number of iterations vs accuracy