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)