-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifiers.py
227 lines (189 loc) · 6.77 KB
/
classifiers.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
from abc import ABC, abstractmethod
from typing import Optional
from vectorizers import Vectorizer
from sklearn.feature_extraction.text import CountVectorizer
import streamlit as st
from sklearn.naive_bayes import MultinomialNB
from sklearn.ensemble import RandomForestClassifier
from sklearn import svm
from sklearn.pipeline import Pipeline
class Classifier(ABC):
"""Abstract base class for classifiers."""
@abstractmethod
def __init__(self) -> None:
pass
@abstractmethod
def get_algorithm_settings(self) -> dict[str, any]:
pass
def fit(self, X, y, **fit_params):
"""Fit the classifier to the training data.
Args:
X: The input features.
y: The target labels.
**fit_params: Additional fitting parameters.
Returns:
The fitted pipeline.
"""
assert hasattr(
self, "pipeline"
), f"{self} does not have the `pipeline` attribute."
assert self.pipeline is not None, "Pipeline has not been initialized."
self.pipeline.fit(X, y, **fit_params)
return self.pipeline
def predict(self, X):
"""Make predictions on new data.
Args:
X: The input features.
Returns:
The predicted labels.
"""
assert hasattr(
self, "pipeline"
), f"{self} does not have the `pipeline` attribute."
assert self.pipeline is not None and isinstance(
self.pipeline, Pipeline
), "Pipeline has not been initialized."
assert self.pipeline.__sklearn_is_fitted__(), "The Pipeline is not fitted."
return self.pipeline.predict(X)
def get_feature_extractor_settings(self, parameter_settnigs_enabled=True):
"""Get the settings for the feature extractor (vectorizer).
Args:
parameter_settnigs_enabled: Whether to enable parameter settings for the feature extractor.
Returns:
None
"""
self.vectorizer = Vectorizer.vectorizer_selection().get_vectorizer()
def get_clf(self):
"""Get the classifier.
Returns:
The classifier.
"""
return self.clf
def get_parameters(self, vectorizer: Optional[CountVectorizer] = None):
"""Get the parameters for the classifier.
Args:
vectorizer: Optional pre-defined vectorizer.
Returns:
None
"""
# TODO: add method for hyperparameter optimization
# self.optimize_hyperparameters = st.toggle(
# "Optimize Hyperparameters", False, disabled=True,
# key=f"optimize_hyperparameters_key_{self.__class__.__name__}",
# )
if not vectorizer:
self.get_feature_extractor_settings()
else:
self.vectorizer = vectorizer
self.algo_settings = self.get_algorithm_settings()
self.clf = self.algorithm_class(
**self.algo_settings,
)
self.pipeline = Pipeline([("vectorizer", self.vectorizer), ("clf", self.clf)])
class NaiveBayes(Classifier):
"""Naive Bayes classifier."""
def __init__(self):
self.algorithm_class = MultinomialNB
def get_algorithm_settings(self):
"""Get the algorithm settings for Naive Bayes.
Returns:
The algorithm settings.
"""
settings = dict()
with st.expander("Naive Bayes Hyperparameters"):
settings["alpha"] = st.slider(
"Alpha",
0.0,
1.0,
1.0,
0.25,
help="Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).",
)
settings["fit_prior"] = st.toggle(
"Fit Prior",
True,
help="Whether to learn class prior probabilities or not. If false, a uniform prior will be used.",
)
return settings
class RandomForest(Classifier):
"""Random Forest classifier."""
def __init__(self):
self.feature_extractor = None
self.algorithm_class = RandomForestClassifier
self.clf = None
def get_algorithm_settings(self):
"""Get the algorithm settings for Random Forest.
Returns:
The algorithm settings.
"""
# get hyperparameters
settings = dict()
with st.expander("Random Forest Hyperparameters"):
settings["n_estimators"] = st.slider(
"N Estimators",
1,
100,
100,
help="The number of trees in the forest.",
)
settings["criterion"] = st.selectbox(
"Criterion",
["gini", "entropy"],
index=0,
help="The function to measure the quality of a split.",
)
settings["min_samples_split"] = st.slider(
"Min Samples Split",
2,
10,
2,
help="The minimum number of samples required to split an internal node.",
)
settings["min_samples_leaf"] = st.slider(
"Min Samples Leaf",
1,
10,
1,
help="The minimum number of samples required to be at a leaf node.",
)
return settings
class SVM(Classifier):
"""Support Vector Machine (SVM) classifier."""
def __init__(self):
self.algorithm_class = svm.SVC
def get_algorithm_settings(self):
"""Get the algorithm settings for SVM.
Returns:
The algorithm settings.
"""
settings = dict()
with st.expander("SVM Hyperparameters"):
settings["C"] = st.slider(
"C",
0.0,
1.0,
1.0,
0.25,
help="Penalty parameter C of the error term.",
)
settings["kernel"] = st.selectbox(
"Kernel",
["linear", "poly", "rbf", "sigmoid"],
index=0,
help="Specifies the kernel type to be used in the algorithm.",
)
settings["degree"] = st.slider(
"Degree",
1,
100,
3,
format="%01d",
help="Degree of the polynomial kernel function ('poly'). Ignored by all other kernels.",
)
settings["gamma"] = st.selectbox(
"Gamma",
["scale", "auto"],
index=0,
help="Kernel coefficient for 'rbf', 'poly' and 'sigmoid'. If gamma='scale' (default) is passed then it uses 1 / (n_features * X.var()) as value of gamma, if 'auto', uses 1 / n_features.",
)
return settings