-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBivariateClassification.py
71 lines (55 loc) · 2.57 KB
/
BivariateClassification.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
import numpy as np
import pandas as pd
# picking models for prediction.
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
# ensemble models for better performance
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier
# Model evaluation
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.metrics import accuracy_score, f1_score
from MiscellaneousFunctions import display_classification_report
class BivariateClassification():
def __init__(self):
self.logistic_regression = LogisticRegression()
self.support_vector_machine = SVC()
self.decision_tree_classifier = DecisionTreeClassifier()
self.random_forest_classifier = RandomForestClassifier()
self.adaboost_classifer = AdaBoostClassifier()
self.all_models = [self.logistic_regression, self.support_vector_machine, self.decision_tree_classifier,
self.random_forest_classifier, self.adaboost_classifer]
self.all_model_names = ['Logistic Regression', 'Support Vector Classifier', 'Decision Tree Classifier',
'Random Forest Classifier', 'Adaboost Classifier']
self.metric_names = ['Accuracy Score', 'Confusion Matrix',
'F1 Score']
self.train_scores = []
self.test_scores = []
self.metric_list = [accuracy_score, confusion_matrix, f1_score]
self.metrics = []
data = {'Model Names': self.all_model_names}
self.all_model_info = pd.DataFrame(data)
def fit(self, x_train, x_test, y_train, y_test):
'''
fits models to data and stores results for metrics
'''
self.x_train = x_train
self.x_test = x_test
self.y_train = y_train
self.y_test = y_test
for model in self.all_models:
model.fit(x_train, y_train)
self.apply_metrics()
def apply_metrics(self):
self.metrics = []
for metric in self.metric_list:
metric_name = str(metric).split(' ')[1]
for model in self.all_models:
metric_item = metric(self.y_test, model.predict(self.x_test))
self.metrics.append(metric_item)
self.all_model_info[metric_name] = self.metrics
self.metrics = []
def display_report(self):
display_classification_report(self.all_model_names, self.all_models, self.y_test, self.x_test)
return self.all_model_info