File size: 1,611 Bytes
7aa666d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
# from sklearn.linear_model import SGDClassifier
from sklearn.svm import LinearSVC
from sklearn.metrics import accuracy_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import precision_score


class Classifier():
    def __init__(self, classifier_type):
        if classifier_type == "Logistic Regression":
            self.classifier = LogisticRegression(max_iter=1000)
        elif classifier_type == "KNN":
            self.classifier = KNeighborsClassifier(
                algorithm='brute', n_jobs=-1)
        elif classifier_type == "Naive Bayes":
            self.classifier = MultinomialNB()
        elif classifier_type == "SVM":
            self.classifier = LinearSVC(C=0.0001)

    def classify(self, X):
        prediction = self.classifier.predict(X)
        return prediction

    def train(self, X_train, Y_train):
        self.classifier.fit(X_train, Y_train)

    def validate(self, X_validate, y_validate):
        prediction = self.classify(X_validate)
        accuracy = accuracy_score(y_validate, prediction)*100
        precision = precision_score(y_validate, prediction, average="weighted")
        recall = recall_score(y_validate, prediction, average="weighted")
        f1_scoree = f1_score(y_validate, prediction, average="weighted")

        return precision, recall, f1_scoree, accuracy

    def score(self, data, labels):
        return self.classifier.score(data, labels)