-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
166 lines (126 loc) · 5.42 KB
/
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
import numpy as np
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from xgboost import XGBClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.cluster import KMeans
class SeverityLogisticRegressionClassifier:
def __init__(self, C=1.0, max_iter=100, random_state=None):
self.model = LogisticRegression(C=C, max_iter=max_iter, random_state=random_state)
self.best_params_ = None
def tune_hyperparameters(self, X_train, y_train):
param_grid = {
'C': [0.1, 1.0, 10.0],
'max_iter': [100, 200, 300]
}
# Initialize GridSearchCV
grid_search = GridSearchCV(self.model, param_grid, cv=3, scoring='accuracy', verbose=2, n_jobs=-1)
# Search
grid_search.fit(X_train, y_train)
self.best_params_ = grid_search.best_params_
self.model = grid_search.best_estimator_
def train(self, X_train, y_train):
if self.best_params_:
print(f"Training with best parameters: {self.best_params_}")
self.model.fit(X_train, y_train)
def predict(self, X_test):
return self.model.predict(X_test)
class SeverityGradientBoostingClassifier:
def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3, random_state=None):
self.model = GradientBoostingClassifier(n_estimators=n_estimators, learning_rate=learning_rate, max_depth=max_depth, random_state=random_state)
self.best_params_ = None
def tune_hyperparameters(self, X_train, y_train):
param_grid = {
'n_estimators': [100, 200, 300],
'learning_rate': [0.01, 0.1, 0.5],
'max_depth': [3, 5, 7]
}
# Initialize GridSearchCV
grid_search = GridSearchCV(self.model, param_grid, cv=3, scoring='accuracy', verbose=2, n_jobs=-1)
# Search
grid_search.fit(X_train, y_train)
self.best_params_ = grid_search.best_params_
self.model = grid_search.best_estimator_
def train(self, X_train, y_train):
if self.best_params_:
print(f"Training with best parameters: {self.best_params_}")
self.model.fit(X_train, y_train)
def predict(self, X_test):
return self.model.predict(X_test)
class SeverityXGBoostClassifier:
def __init__(self):
self.model = XGBClassifier()
self.best_params_ = None
def tune_hyperparameters(self, X_train, y_train):
param_grid = {
'n_estimators': [100, 200, 300],
'learning_rate': [0.01, 0.1, 0.5],
'max_depth': [3, 5, 7]
}
# Initialize GridSearchCV
grid_search = GridSearchCV(self.model, param_grid, cv=3, scoring='accuracy', verbose=2, n_jobs=-1)
# Search
grid_search.fit(X_train, y_train)
self.best_params_ = grid_search.best_params_
self.model = grid_search.best_estimator_
def train(self, X_train, y_train):
if self.best_params_:
print(f"Training with best parameters: {self.best_params_}")
self.model.fit(X_train, y_train)
def predict(self, X_test):
return self.model.predict(X_test)
class SeverityKMeansClusterClassifier:
def __init__(self, k=3, random_state=None):
self.model = KMeans(n_clusters=k, random_state=random_state)
self.best_params_ = None
def tune_hyperparameters(self, X_train, y_train):
param_grid = {
'n_clusters': [3, 5, 7],
'max_iter': [100, 200, 300]
}
# Initialize GridSearchCV
grid_search = GridSearchCV(self.model, param_grid, cv=3, scoring='accuracy', verbose=2, n_jobs=-1)
# Search
grid_search.fit(X_train, y_train)
self.best_params_ = grid_search.best_params_
self.model = grid_search.best_estimator_
def train(self, X_train, y_train):
if self.best_params_:
print(f"Training with best parameters: {self.best_params_}")
self.model.fit(X_train, y_train)
def predict(self, X_test):
return self.model.predict(X_test)
class SeverityRandomForestClassifier:
def __init__(self, n_estimators=100, random_state=None):
self.model = RandomForestClassifier(n_estimators=n_estimators, criterion='entropy', random_state=random_state)
self.best_params_ = None
def tune_hyperparameters(self, X_train, y_train):
param_grid = {
'n_estimators': [100, 200, 300],
'max_depth': [None, 10, 20, 30],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
}
# Initialize GridSearchCV
grid_search = GridSearchCV(self.model, param_grid, cv=3, scoring='accuracy', verbose=2, n_jobs=-1)
# Search
grid_search.fit(X_train, y_train)
self.best_params_ = grid_search.best_params_
self.model = grid_search.best_estimator_
def train(self, X_train, y_train):
"""
Train the classifier.
:param X_train: training features
:param y_train: training labels
"""
if self.best_params_:
print(f"Training with best parameters: {self.best_params_}")
self.model.fit(X_train, y_train)
def predict(self, X_test):
"""
Make predictions with the classifier.
:param X_test: test features
:return: predictions
"""
return self.model.predict(X_test)