Upload classifier.py
Browse files- classifier.py +41 -0
classifier.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sklearn.naive_bayes import MultinomialNB
|
2 |
+
from sklearn.linear_model import LogisticRegression
|
3 |
+
from sklearn.neighbors import KNeighborsClassifier
|
4 |
+
# from sklearn.linear_model import SGDClassifier
|
5 |
+
from sklearn.svm import LinearSVC
|
6 |
+
from sklearn.metrics import accuracy_score
|
7 |
+
from sklearn.metrics import recall_score
|
8 |
+
from sklearn.metrics import f1_score
|
9 |
+
from sklearn.metrics import precision_score
|
10 |
+
|
11 |
+
|
12 |
+
class Classifier():
|
13 |
+
def __init__(self, classifier_type):
|
14 |
+
if classifier_type == "Logistic Regression":
|
15 |
+
self.classifier = LogisticRegression(max_iter=1000)
|
16 |
+
elif classifier_type == "KNN":
|
17 |
+
self.classifier = KNeighborsClassifier(
|
18 |
+
algorithm='brute', n_jobs=-1)
|
19 |
+
elif classifier_type == "Naive Bayes":
|
20 |
+
self.classifier = MultinomialNB()
|
21 |
+
elif classifier_type == "SVM":
|
22 |
+
self.classifier = LinearSVC(C=0.0001)
|
23 |
+
|
24 |
+
def classify(self, X):
|
25 |
+
prediction = self.classifier.predict(X)
|
26 |
+
return prediction
|
27 |
+
|
28 |
+
def train(self, X_train, Y_train):
|
29 |
+
self.classifier.fit(X_train, Y_train)
|
30 |
+
|
31 |
+
def validate(self, X_validate, y_validate):
|
32 |
+
prediction = self.classify(X_validate)
|
33 |
+
accuracy = accuracy_score(y_validate, prediction)*100
|
34 |
+
precision = precision_score(y_validate, prediction, average="weighted")
|
35 |
+
recall = recall_score(y_validate, prediction, average="weighted")
|
36 |
+
f1_scoree = f1_score(y_validate, prediction, average="weighted")
|
37 |
+
|
38 |
+
return precision, recall, f1_scoree, accuracy
|
39 |
+
|
40 |
+
def score(self, data, labels):
|
41 |
+
return self.classifier.score(data, labels)
|