-
Notifications
You must be signed in to change notification settings - Fork 27
/
baseline_models.py
239 lines (187 loc) · 6.9 KB
/
baseline_models.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
import random
import numpy as np
from sklearn import ensemble, linear_model, neighbors, svm, tree
from models.basemodel import BaseModel
"""
Define all Models implemented by the Sklearn library:
Linear Model, KNN, SVM, Decision Tree, Random Forest
"""
"""
Linear Model - Ordinary least squares Linear Regression / Logistic Regression
Takes no hyperparameters
"""
# TabZilla: add function to generate seeded random parameters, and default parameters.
class LinearModel(BaseModel):
def __init__(self, params, args):
super().__init__(params, args)
if args.objective == "regression":
self.model = linear_model.LinearRegression(n_jobs=-1)
elif args.objective == "classification":
self.model = linear_model.LogisticRegression(
multi_class="multinomial", n_jobs=-1
)
elif args.objective == "binary":
self.model = linear_model.LogisticRegression(n_jobs=-1)
@classmethod
def define_trial_parameters(cls, trial, args):
params = dict()
return params
@classmethod
def get_random_parameters(cls, seed: int):
params = dict()
return params
@classmethod
def default_parameters(cls):
params = dict()
return params
def get_classes(self):
return self.model.classes_
"""
K-Neighbors Regressor - Regression/Classification based on k-nearest neighbors
Takes number of neighbors as hyperparameters
"""
class KNN(BaseModel):
def __init__(self, params, args):
super().__init__(params, args)
if args.objective == "regression":
self.model = neighbors.KNeighborsRegressor(
n_neighbors=params["n_neighbors"],
algorithm=params["knn_alg"],
leaf_size=params["leaf_size"],
n_jobs=-1,
)
elif args.objective == "classification" or args.objective == "binary":
self.model = neighbors.KNeighborsClassifier(
n_neighbors=params["n_neighbors"],
algorithm=params["knn_alg"],
leaf_size=params["leaf_size"],
n_jobs=-1,
)
def fit(self, X, y, X_val=None, y_val=None):
return super().fit(X, y, X_val, y_val)
@classmethod
def define_trial_parameters(cls, trial, args):
params = {
"n_neighbors": trial.suggest_categorical(
"n_neighbors", list(range(3, 42, 2))
),
"knn_alg": trial.suggest_categorical("knn_alg", ["kd_tree", "ball_tree"]),
"leaf_size": trial.suggest_int("leaf_size", [30, 50, 70, 100, 300]),
}
return params
@classmethod
def get_random_parameters(cls, seed: int):
rs = np.random.RandomState(seed)
params = {
"n_neighbors": 1 + 2 * rs.randint(1, 21),
"knn_alg": rs.choice(["kd_tree", "ball_tree"]),
"leaf_size": rs.choice([30, 50, 70, 100, 300]),
}
return params
@classmethod
def default_parameters(cls):
params = {
"n_neighbors": 9,
"knn_alg": "kd_tree",
"leaf_size": 30,
}
return params
def get_classes(self):
return self.model.classes_
"""
Support Vector Machines - Epsilon-Support Vector Regression / C-Support Vector Classification
Takes the regularization parameter as hyperparameter
"""
class SVM(BaseModel):
def __init__(self, params, args):
super().__init__(params, args)
if args.objective == "regression":
self.model = svm.SVR(C=params["C"])
elif args.objective == "classification" or args.objective == "binary":
self.model = svm.SVC(C=params["C"], probability=True)
@classmethod
def define_trial_parameters(cls, trial, args):
params = {"C": trial.suggest_float("C", 1e-10, 1e10, log=True)}
return params
@classmethod
def get_random_parameters(cls, seed: int):
rs = np.random.RandomState(seed)
params = {"C": np.power(10, rs.uniform(-10, 10))}
return params
@classmethod
def default_parameters(cls):
params = {"C": 1.0}
return params
def get_classes(self):
return self.model.classes_
"""
Decision Tree - Decision Tree Regressor/Classifier
Takes the maximum depth of the tree as hyperparameter
"""
class DecisionTree(BaseModel):
def __init__(self, params, args):
super().__init__(params, args)
if args.objective == "regression":
self.model = tree.DecisionTreeRegressor(max_depth=params["max_depth"])
elif args.objective == "classification" or args.objective == "binary":
self.model = tree.DecisionTreeClassifier(max_depth=params["max_depth"])
@classmethod
def define_trial_parameters(cls, trial, args):
params = {"max_depth": trial.suggest_int("max_depth", 2, 12, log=True)}
return params
@classmethod
def get_random_parameters(cls, seed: int):
rs = np.random.RandomState(seed)
params = {"max_depth": int(np.round(np.power(2, rs.uniform(1, np.log2(12)))))}
return params
@classmethod
def default_parameters(cls):
params = {"max_depth": 5}
return params
def get_classes(self):
return self.model.classes_
"""
Random Forest - Random Forest Regressor/Classifier
Takes the maximum depth of the trees and the number of estimators as hyperparameter
"""
class RandomForest(BaseModel):
def __init__(self, params, args):
super().__init__(params, args)
if args.objective == "regression":
self.model = ensemble.RandomForestRegressor(
n_estimators=params["n_estimators"],
max_depth=params["max_depth"],
n_jobs=-1,
)
elif args.objective == "classification" or args.objective == "binary":
self.model = ensemble.RandomForestClassifier(
n_estimators=params["n_estimators"],
max_depth=params["max_depth"],
n_jobs=-1,
)
@classmethod
def define_trial_parameters(cls, trial, args):
params = {
"max_depth": trial.suggest_int("max_depth", 2, 12, log=True),
"n_estimators": trial.suggest_int("n_estimators", 5, 100, log=True),
}
return params
@classmethod
def get_random_parameters(cls, seed: int):
rs = np.random.RandomState(seed)
params = {
"max_depth": int(np.round(np.power(2, rs.uniform(1, np.log2(12))))),
"n_estimators": int(
np.round(np.power(5, rs.uniform(1, np.log2(100) / np.log2(5))))
),
}
return params
@classmethod
def default_parameters(cls):
params = {
"max_depth": 5,
"n_estimators": 50,
}
return params
def get_classes(self):
return self.model.classes_