-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid_search_params.py
273 lines (228 loc) · 11 KB
/
grid_search_params.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import time
import numpy as np
from matplotlib import pyplot as plt
from sklearn.ensemble import GradientBoostingRegressor, HistGradientBoostingRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVR
from load_balancer_model import get_x_and_y_clustering, get_x_and_y_svm, get_x_and_y_rf
# TRAIN: bool = True
TRAIN: bool = False
TRAINING_CSV_PATH_CLUSTERING = 'LoadBalancerDatasets/ClusteringTimesRecord-2023-08-18.csv'
def __fit_predict_and_plot(x_train: np.ndarray, y_train: np.ndarray, x_test_axis: np.ndarray,
test_data: np.ndarray, model):
"""Fits model with x and y, prints predictions with test_data and plots a scatter plot."""
model.fit(x_train, y_train)
# Creates a new figure with 2 subplots
fig, (ax1, ax2) = plt.subplots(1, 2)
# Plots the predictions for train data
number_of_features_train = x_train[:, 0]
predictions_train = model.predict(x_train)
print('predictions_train:')
print(predictions_train)
ax1.title.set_text('Train data')
ax1.scatter(number_of_features_train, y_train, label='Real values')
ax1.scatter(number_of_features_train, predictions_train, label='Predictions', color='red')
ax1.set_xlabel('Number of features')
ax1.set_ylabel('Execution time (s)')
ax1.legend()
# Plots the predictions with test_data
predictions_test = model.predict(test_data)
print('predictions_test:')
print(predictions_test)
ax2.title.set_text('Test data')
ax2.scatter(x_test_axis, predictions_test, label='Predictions')
ax2.set_xlabel('Number of features')
ax2.set_ylabel('Execution time (s)')
ax2.legend()
plt.show()
def grid_search_gradient_booster(x: np.ndarray, y: np.ndarray):
"""Makes a GridSearch for the GradientBoostingRegressor algorithm."""
param_grid = {
'n_estimators': [100, 200, 300, 400, 500],
'max_depth': [2, 3, 4, 5, 6],
'learning_rate': [0.01, 0.05, 0.1, 0.2, 0.3],
'min_samples_split': [2, 3, 4, 5, 6],
'min_samples_leaf': [10, 20, 30, 40, 50]
}
model = GradientBoostingRegressor()
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5, n_jobs=-1, verbose=2)
grid_search.fit(x, y)
print('Best params:')
print(grid_search.best_params_)
def grid_search_hist_gradient_booster(x: np.ndarray, y: np.ndarray):
"""Makes a GridSearch for the HistGradientBoostingRegressor algorithm."""
param_grid = {
'max_iter': [100, 200, 300, 400, 500],
'max_depth': [2, 3, 4, 5, 6],
'learning_rate': [0.01, 0.05, 0.1, 0.2, 0.3],
'max_leaf_nodes': [31, 41, 51, 61, 71],
'min_samples_leaf': [10, 20, 30, 40, 50]
}
model = HistGradientBoostingRegressor()
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5, n_jobs=-1, verbose=2)
grid_search.fit(x, y)
print(grid_search.best_params_)
def grid_search_svm_poly(x: np.ndarray, y: np.ndarray):
"""Makes a GridSearch for the SVR algorithm with poly kernel."""
param_grid = {
'kernel': ['poly'],
'degree': [2, 3],
'C': [0.1, 0.5, 1]
}
model = SVR()
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5, n_jobs=-1, verbose=2)
grid_search.fit(x, y)
print(grid_search.best_params_)
def grid_search_svm_rbf_sigmoid(x: np.ndarray, y: np.ndarray):
"""Makes a GridSearch for the SVR algorithm."""
param_grid = {
'kernel': ['rbf', 'sigmoid'],
'gamma': ['scale', 'auto'],
'C': [0.1, 0.5, 1, 2, 3, 4, 5]
}
model = SVR()
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5, n_jobs=-1, verbose=2)
grid_search.fit(x, y)
print(grid_search.best_params_)
def train_with_grid_search(x: np.ndarray, y: np.ndarray):
print('HistGradientBoostingRegressor:')
start = time.time()
grid_search_hist_gradient_booster(x, y)
print(f'Time grid_search_hist_gradient_booster: {time.time() - start}')
# print('GradientBoostingRegressor:')
# start = time.time()
# grid_search_gradient_booster(x, y)
# print(f'Time grid_search_gradient_booster: {time.time() - start}')
# print('SVM Poly:')
# start = time.time()
# grid_search_svm_poly(x, y)
# print(f'Time svm: {time.time() - start}')
# print('SVM RBF and sigmoid:')
# start = time.time()
# grid_search_svm_rbf_sigmoid(x, y)
# print(f'Time svm: {time.time() - start}')
def test_best_models_svm(x: np.ndarray, y: np.ndarray, number_of_features: np.ndarray, data: np.ndarray):
"""Tests best models computed previously"""
# HistGradientBoostingRegressor
best_hist_params = {'learning_rate': 0.2, 'max_depth': 4, 'max_iter': 300, 'max_leaf_nodes': 41,
'min_samples_leaf': 20}
model = HistGradientBoostingRegressor(**best_hist_params)
__fit_predict_and_plot(x, y, number_of_features, data, model)
def test_best_models_rf(x: np.ndarray, y: np.ndarray, number_of_features: np.ndarray, data: np.ndarray):
"""Tests best models computed previously"""
# HistGradientBoostingRegressor
best_hist_params = {'learning_rate': 0.01, 'max_depth': 2, 'max_iter': 400, 'max_leaf_nodes': 31,
'min_samples_leaf': 40}
model = HistGradientBoostingRegressor(**best_hist_params)
__fit_predict_and_plot(x, y, number_of_features, data, model)
def test_best_models_clustering(x: np.ndarray, y: np.ndarray, number_of_features: np.ndarray, data: np.ndarray):
"""Tests best models computed previously"""
# HistGradientBoostingRegressor
best_hist_params = {'learning_rate': 0.01, 'max_depth': 6, 'max_iter': 400, 'max_leaf_nodes': 41,
'min_samples_leaf': 10}
model = HistGradientBoostingRegressor(**best_hist_params)
__fit_predict_and_plot(x, y, number_of_features, data, model)
# GradientBoostingRegressor
# best_grad_boost_params = {'learning_rate': 0.01, 'max_depth': 6, 'min_samples_leaf': 10, 'min_samples_split': 3,
# 'n_estimators': 200}
# model = GradientBoostingRegressor(**best_grad_boost_params)
# __fit_predict_and_plot(x, y, number_of_features, data, model)
# SVM Poly
# best_svm_poly_params = {'C': 1, 'degree': 3, 'kernel': 'poly'}
# model = SVR(**best_svm_poly_params)
# __fit_predict_and_plot(x, y, number_of_features, data, model)
def get_test_data_for_clustering():
"""
Generates a random dataset using the structure [number_of_features, 1082, 2, 1, 2] and dtype = np.float32.
Where number of features should be generated in order from 10 to 100 and then from 100 to 25000 samples.
This struct is only for Clustering.
"""
data_10_to_100 = np.array([i for i in range(10, 110, 10)])
data_100_to_20000 = np.array([i for i in range(100, 20100, 100)])
data = np.concatenate((data_10_to_100, data_100_to_20000))
number_of_elements = data.shape[0]
data = data.reshape((number_of_elements, 1))
data = np.concatenate((data, np.full((number_of_elements, 1), 1082)), axis=1)
data = np.concatenate((data, np.full((number_of_elements, 1), 2)), axis=1)
data = np.concatenate((data, np.full((number_of_elements, 1), 1)), axis=1)
data = np.concatenate((data, np.full((number_of_elements, 1), 2)), axis=1)
data = data.astype(np.float32)
print('Test data:')
print(data)
return data
def get_test_data_for_rf():
"""
Generates a random dataset using the structure [number_of_features, 1082, 2, 1, 2] and dtype = np.float32.
Where number of features should be generated in order from 10 to 100 and then from 100 to 25000 samples.
This struct is only for RF.
"""
data_10_to_100 = np.array([i for i in range(10, 110, 10)])
data_100_to_20000 = np.array([i for i in range(100, 20100, 100)])
data = np.concatenate((data_10_to_100, data_100_to_20000))
number_of_elements = data.shape[0]
data = data.reshape((number_of_elements, 1))
data = np.concatenate((data, np.full((number_of_elements, 1), 1082)), axis=1)
data = np.concatenate((data, np.full((number_of_elements, 1), 10)), axis=1) # Number of Trees
data = data.astype(np.float32)
print('Test data:')
print(data)
return data
def get_test_data_for_svm():
"""
Generates a random dataset using the structure [number_of_features, 1082, 2, 1, 2] and dtype = np.float32.
Where number of features should be generated in order from 10 to 100 and then from 100 to 25000 samples.
This struct is only for SVM.
"""
data_10_to_100 = np.array([i for i in range(10, 110, 10)])
data_100_to_20000 = np.array([i for i in range(100, 20100, 100)])
data = np.concatenate((data_10_to_100, data_100_to_20000))
number_of_elements = data.shape[0]
data = data.reshape((number_of_elements, 1))
data = np.concatenate((data, np.full((number_of_elements, 1), 1082)), axis=1)
data = np.concatenate((data, np.full((number_of_elements, 1), 1)), axis=1) # Optimizer = 'avltree'
data = np.concatenate((data, np.full((number_of_elements, 1), 0)), axis=1) # Kernel = 'linear'
data = data.astype(np.float32)
print('Test data:')
print(data)
return data
def main():
# Clustering
def clustering():
data = get_test_data_for_clustering()
number_of_features = data[:, 0].copy() # Gets number of features from data (without MinMax transformation)
x_clustering, _x_clustering_without_min_max, y_clustering, _ord_encoder_clustering, min_max_scaler_clustering = get_x_and_y_clustering()
if TRAIN:
train_with_grid_search(x_clustering, y_clustering)
else:
data[:, [0, 1]] = min_max_scaler_clustering.transform(data[:, [0, 1]])
print('Test data after MinMaxScaling by clustering:')
print(data)
test_best_models_clustering(x_clustering, y_clustering, number_of_features, data)
# SVM
def svm():
data = get_test_data_for_svm()
number_of_features = data[:, 0].copy() # Gets number of features from data (without MinMax transformation)
x_svm, _x_svm_without_min_max, y_svm, _ord_encoder_svm, min_max_scaler_svm = get_x_and_y_svm()
if TRAIN:
train_with_grid_search(x_svm, y_svm)
else:
data[:, [0, 1]] = min_max_scaler_svm.transform(data[:, [0, 1]])
print('Test data after MinMaxScaling by SVM:')
print(data)
test_best_models_svm(x_svm, y_svm, number_of_features, data)
def rf():
data = get_test_data_for_rf()
number_of_features = data[:, 0].copy() # Gets number of features from data (without MinMax transformation)
x_rf, _x_svm_without_min_max, y_rf, _ord_encoder_rf, min_max_scaler_rf = get_x_and_y_rf()
if TRAIN:
train_with_grid_search(x_rf, y_rf)
else:
data[:, [0, 1]] = min_max_scaler_rf.transform(data[:, [0, 1]])
print('Test data after MinMaxScaling by RF:')
print(data)
test_best_models_rf(x_rf, y_rf, number_of_features, data)
clustering()
svm()
rf()
if __name__ == '__main__':
main()