-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathclassification_models.py
More file actions
186 lines (141 loc) · 6.54 KB
/
classification_models.py
File metadata and controls
186 lines (141 loc) · 6.54 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
import sklearn
from abc import ABC, abstractmethod
from sklearn.linear_model import Perceptron, SGDClassifier, LogisticRegression, RidgeClassifier, LinearRegression, ElasticNet
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from xgboost import XGBClassifier
from data_processing import DataProcessing
class SkLearnModelFactory(ABC):
"""Abstract implementation of a model. Each specified model inherits from this base class.
Methods decorated with @abstractmethod must be implemented and if not, the interpreter will throw an error. Methods not decorated will be shared by all other classes that inherit from Model.
"""
@abstractmethod
def __name__(self):
return "ML MODEL BASE"
def get_model_name(self):
return self.__name__()
def __init__(self, random_state=42):
self.classifer = None
self.random_state = random_state
@abstractmethod
def train_model(self):
pass
def predict(self, X_test, to_series: bool = True):
predictions = self.classifer.predict(X_test)
if to_series:
return DataProcessing.array_to_df(predictions)
return predictions
@staticmethod
def select_model(model_name: str, random_state=42):
"""Select a model with specified random state."""
models = {
"perceptron": SkLearnPerceptronModel(random_state),
"sgd_classifier": SkLearnSGDClassifier(random_state),
"logistic_regression": SkLearnLogisticRegression(random_state),
"ridge_classifier": SkLearnRidgeClassifier(random_state),
"linear_regression": SkLearnLinearRegression(random_state),
"elastic_net": SkLearnElasticNet(random_state),
"decision_tree_classifier": SkLearnDecisionTreeClassifier(random_state),
"random_forest_classifier": SkLearnRandomForestClassifier(random_state),
"gradient_boosting_classifier": SkLearnGradientBoostingClassifier(random_state),
"support_vector_machine_classifier": SkLearnSVC(random_state),
"x_gradient_boosting_classifier": CustomXGBClassifier(random_state)
}
if model_name in models:
return models[model_name]
else:
raise ValueError(f"Model {model_name} not found. Available models: {list(models.keys())}")
# Models that NEED random_state:
class SkLearnSGDClassifier(SkLearnModelFactory):
def __init__(self, random_state=42):
super().__init__(random_state)
def __name__(self):
return "SDG Classifier"
def train_model(self, X, y):
self.classifer = SGDClassifier(random_state=self.random_state)
self.classifer.fit(X, y)
class SkLearnLogisticRegression(SkLearnModelFactory):
def __init__(self, random_state=42):
super().__init__(random_state)
def __name__(self):
return "Logistic Regression"
def train_model(self, X, y):
self.classifer = LogisticRegression(random_state=self.random_state, max_iter=1000)
self.classifer.fit(X, y)
class SkLearnDecisionTreeClassifier(SkLearnModelFactory):
def __init__(self, random_state=42):
super().__init__(random_state)
def __name__(self):
return "Decision Tree"
def train_model(self, X, y):
self.classifer = DecisionTreeClassifier(random_state=self.random_state)
self.classifer.fit(X, y)
class SkLearnRandomForestClassifier(SkLearnModelFactory):
def __init__(self, random_state=42):
super().__init__(random_state)
def __name__(self):
return "Random Forest"
def train_model(self, X, y):
self.classifer = RandomForestClassifier(random_state=self.random_state)
self.classifer.fit(X, y)
class SkLearnGradientBoostingClassifier(SkLearnModelFactory):
def __init__(self, random_state=42):
super().__init__(random_state)
def __name__(self):
return "Gradient Boosting Machine"
def train_model(self, X, y):
self.classifer = GradientBoostingClassifier(random_state=self.random_state)
self.classifer.fit(X, y)
class CustomXGBClassifier(SkLearnModelFactory):
def __init__(self, random_state=42):
super().__init__(random_state)
def __name__(self):
return "X Gradient Boosting Machine"
def train_model(self, X, y):
self.classifer = XGBClassifier(random_state=self.random_state)
self.classifer.fit(X, y)
# Models that DON'T need random_state (deterministic):
class SkLearnPerceptronModel(SkLearnModelFactory):
def __init__(self, random_state=42):
super().__init__(random_state)
def __name__(self):
return "Perceptron"
def train_model(self, X, y):
self.classifer = Perceptron(random_state=self.random_state) # Has random_state parameter
self.classifer.fit(X, y)
class SkLearnRidgeClassifier(SkLearnModelFactory):
def __init__(self, random_state=42):
super().__init__(random_state)
def __name__(self):
return "Ridge Classifier"
def train_model(self, X, y):
self.classifer = RidgeClassifier(random_state=self.random_state) # Has random_state parameter
self.classifer.fit(X, y)
# LinearRegression and ElasticNet - keep as-is (deterministic, no random_state param)
class SkLearnLinearRegression(SkLearnModelFactory):
def __init__(self, random_state=42):
super().__init__(random_state)
def __name__(self):
return "Linear Regression"
def train_model(self, X, y):
self.classifer = LinearRegression() # No random_state parameter
self.classifer.fit(X, y)
class SkLearnElasticNet(SkLearnModelFactory):
def __init__(self, random_state=42):
super().__init__(random_state)
def __name__(self):
return "Elastic Net"
def train_model(self, X, y):
self.classifer = ElasticNet(random_state=self.random_state) # Has random_state parameter
self.classifer.fit(X, y)
class SkLearnSVC(SkLearnModelFactory):
def __init__(self, random_state=42):
super().__init__(random_state)
def __name__(self):
return "Support Vector Machine"
def train_model(self, X, y):
self.classifer = SVC(kernel='linear', C=1, random_state=self.random_state) # Has random_state parameter
return self.classifer.fit(X, y)
def get_score(self, X, y):
return self.classifer.score(X, y)