-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleML.py
More file actions
235 lines (170 loc) · 8.31 KB
/
SimpleML.py
File metadata and controls
235 lines (170 loc) · 8.31 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import pandas as pd
import numpy as np
from sklearn import linear_model
from sklearn.linear_model import Ridge
from sklearn import metrics
from sklearn.svm import SVR
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import SGD
from sklearn.preprocessing import StandardScaler
import time
start = time.time()
data = pd.read_csv("data.csv")
# Prepping Data
data = data.sample(frac=1).reset_index(drop=True)
XLabels = ['TotalT', 'Temp', 'LSR', 'CA', 'Size', 'IsoT', 'HeatT', 'Ramp', 'F_X', 'Ro', 'logRo', 'P']
X = data[XLabels]
# Scaling X
sc = StandardScaler()
X = sc.fit_transform(X)
numData = len(data.index)
numTrain = int(numData * 0.7)
numTest = int(numData * .15)
# print(numTest, numTrain)
train_Frame, valid_Frame, test_Frame, train_valid_Frame = data.iloc[:numTrain, :], data.iloc[numTrain:-numTest,
:], data.iloc[-numTest:,
:], data.iloc[:-numTest:, :]
y_train, y_valid, y_test, y_train_valid = train_Frame['Yield'], valid_Frame['Yield'], test_Frame['Yield'], \
train_valid_Frame['Yield']
X_train, X_valid, X_test, X_train_valid = X[:numTrain, :], X[numTrain:-numTest, :], X[-numTest:, :], X[:-numTest, :]
# Simple Linear Regression
###
print("Staring Linear Regression ------------------")
regr = linear_model.LinearRegression()
regr.fit(X_train_valid, y_train_valid)
coeffs = dict(zip(XLabels, regr.coef_))
y_pred = regr.predict(X_test)
predAndActual = pd.DataFrame({'Pred': y_pred, 'Test': y_test})
#predAndActual.to_csv("OverallSimpleLinear.csv")
linearError = pd.Series(y_test - y_pred, name='Lin Err').abs()
test_Frame = pd.concat([test_Frame, linearError], axis=1)
test_Frame.to_csv('TestDataWithErrors.csv')
print('Linear regression Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Linear regression Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
print('Linear regression Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
print("Ending Linear Regression ------------------")
##Ridge Regression
print("Staring Ridge Regression ------------------")
# alphas = [0]
alphas = [0, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10, 100]
errors = []
# Creating Different models for different alphas
for a in alphas:
ridgeModel = Ridge(alpha=a)
ridgeModel.fit(X_train, y_train)
y_pred = ridgeModel.predict(X_valid)
error = metrics.mean_absolute_error(y_valid, y_pred)
errors.append(error)
best_alpha = alphas[np.argmin(errors)]
print("Ridge Regression best alpha is: ", best_alpha)
best_model = Ridge(alpha=best_alpha)
best_model.fit(X_train_valid, y_train_valid)
y_pred = best_model.predict(X_test)
predAndActual = pd.DataFrame({'Pred': y_pred, 'Test': y_test})
#predAndActual.to_csv("OverallRidge.csv")
ridgeError = pd.Series(y_test - y_pred, name='Rid Err').abs()
test_Frame = pd.concat([test_Frame, ridgeError], axis=1)
test_Frame.to_csv('TestDataWithErrors.csv')
# Evaluation
print('Ridge Regression Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Ridge Regression Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
print('Ridge Regression Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
print("Ridge Regression Coefficients: ", best_model.coef_)
print("Ending Ridge Regression ------------------")
# SVM Models
print("Starting Support Vector Regression ------------------")
kernels = ['poly', 'rbf', 'linear']
epsilons = [0.1, 5, 10, 20]
Cs = [0.1, 1, 10, 20]
gammas = ['scale', 'auto']
errors = []
for kern in kernels:
for ep in epsilons:
for C_ in Cs:
for gam in gammas:
svrModel = SVR(kernel=kern, gamma=gam, epsilon=ep, cache_size=2000, C=C_)
# svrModel.fit(X_train, y_train, sample_weight=train_weights)
svrModel.fit(X_train, y_train)
y_pred = svrModel.predict(X_valid)
error = metrics.mean_absolute_error(y_valid, y_pred)
errors.append(error)
index_of_lowest_error = np.argmin(errors)
best_kernel = kernels[int(index_of_lowest_error / (len(epsilons) * len(Cs) * len(gammas)))] # Good
best_ep = epsilons[
int((index_of_lowest_error % (len(epsilons) * len(Cs) * len(gammas))) / (len(Cs) * len(gammas)))] # Good
best_C = Cs[int((index_of_lowest_error % (len(Cs) * len(gammas))) / len(gammas))] # Good
best_gamma = gammas[index_of_lowest_error % len(gammas)]
print("SVR Best kernel is: ", best_kernel)
print("SVR Best Epsilon is: ", best_ep)
print("SVR Best C is: ", best_C)
print("SVR Best Gamma is: ", best_gamma)
# # Make it run a little faster, hardcode best
best_model = SVR(kernel=best_kernel, gamma=best_gamma, epsilon=best_ep, cache_size=2000, C=best_C)
# best_model.fit(X_train_valid, y_train_valid, sample_weight=train_valid_weights)
best_model.fit(X_train_valid, y_train_valid)
y_pred = best_model.predict(X_test)
predAndActual = pd.DataFrame({'Pred': y_pred, 'Test': y_test})
#predAndActual.to_csv("OverallSVR.csv")
svrError = pd.Series(y_test - y_pred, name='SVR Err').abs()
test_Frame = pd.concat([test_Frame, svrError], axis=1)
test_Frame.to_csv('TestDataWithErrors.csv')
print('SVR Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('SVR Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
print('SVR Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
print("Ending Support Vector Regression ------------------")
## Artificial Neural Network
# Keras
print("Starting Neural Network------------------")
learningRates = [0.002, 0.005, 0.01, 0.02]
batchSizes = [64, 128, 256, 512, 1024]
dropoutRates = [0.00, 0.001, 0.01, 0.1]
errors = []
for lr_ in learningRates:
for bs in batchSizes:
for dr in dropoutRates:
model = Sequential()
model.add(Dense(units=12, activation='sigmoid', input_dim=12))
model.add(Dropout(dr))
model.add(Dense(units=12, activation='sigmoid'))
model.add(Dense(units=6, activation='sigmoid'))
model.add(Dense(units=6, activation='sigmoid'))
model.add(Dense(units=1, activation='softplus'))
sgd = SGD(lr=lr_)
model.compile(loss='mean_squared_error', optimizer=sgd, metrics=['accuracy'])
model.fit(X_train, y_train, epochs=3000, batch_size=bs, verbose=0)
y_pred = model.predict(X_valid, batch_size=bs)
y_pred = y_pred.flatten()
error = metrics.mean_absolute_error(y_valid, y_pred)
errors.append(error)
index_of_lowest_error = np.argmin(errors)
best_lr = learningRates[int(index_of_lowest_error / (len(batchSizes) * len(dropoutRates)))] # Good
best_bs = batchSizes[int((index_of_lowest_error % (len(batchSizes) * len(dropoutRates))) / len(dropoutRates))] # Good
best_dr = dropoutRates[index_of_lowest_error % len(dropoutRates)] # Good
print("ANN Best Learning Rate is: ", best_lr)
print("ANN Best Batch Size is: ", best_bs)
print("ANN Best Dropout Rate is: ", best_dr)
# Using best values
model = Sequential()
model.add(Dense(units=12, activation='sigmoid', input_dim=12))
model.add(Dropout(best_dr))
model.add(Dense(units=12, activation='sigmoid'))
model.add(Dense(units=6, activation='sigmoid'))
model.add(Dense(units=6, activation='sigmoid'))
model.add(Dense(units=1, activation='softplus'))
sgd = SGD(lr=best_lr)
model.compile(loss='mean_squared_error', optimizer=sgd, metrics=['accuracy'])
model.fit(X_train_valid, y_train_valid, epochs=3000, batch_size=best_bs, verbose=0)
# loss_and_metrics = model.evaluate(X_test, y_test,batch_size=best_bs)
y_pred = model.predict(X_test, batch_size=best_bs)
y_pred = y_pred.flatten()
nnError = pd.Series(y_test - y_pred, name='NN Err').abs()
test_Frame = pd.concat([test_Frame, nnError], axis=1)
test_Frame.to_csv('TestDataWithErrors.csv')
print('ANN Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('ANN Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
print('ANN Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
print("Ending Neural Network Regression ------------------")
end = time.time()
duration = end - start
print("Execution Time is:", duration /60, "min")