-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatisticalModelImplementer.py
148 lines (106 loc) · 4.6 KB
/
StatisticalModelImplementer.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
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 5 16:08:16 2020
@author: mishr
"""
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
from sklearn.neighbors import KNeighborsClassifier
# 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
class StatisticalModelImplenter():
def __init__(self):
self.__logistic_regression = None
self.__decision_tree_classifier = None
self.__support_vector_machine = None
self.__adaboost_classifer = None
self.__random_forest_classifier = None
self.__kneighbors_classifier = None
self.__best_n_estimator = None
self.__all_models = []
self.__all_model_names = []
def fit(self, x_train, y_train):
self.__x_train = x_train
self.__y_train = y_train
logistic_regressor = LogisticRegression()
logistic_regressor.fit(self.__x_train, self.__y_train)
self.__all_model_names.append('Logistic Regression')
self.__all_models.append(logistic_regressor)
# Decision Tree Classifier
dtree = DecisionTreeClassifier(criterion='entropy')
dtree.fit(self.__x_train, self.__y_train)
self.__all_model_names.append('Decision Tree Classifier')
self.__all_models.append(dtree)
# Support Vector Classifier
svc = SVC(kernel='rbf')
svc.fit(self.__x_train, self.__y_train)
self.__all_model_names.append('Support Vector Classifier')
self.__all_models.append(svc)
# K-Nearest Neighbors Classifier
self.best_n_estimator()
knn_classifier = KNeighborsClassifier(
n_neighbors=self.__best_n_estimator)
knn_classifier.fit(self.__x_train, self.__y_train)
self.__all_model_names.append('KNeighbors CLassifier')
self.__all_models.append(knn_classifier)
# Random Forest Classifier
random_forest = RandomForestClassifier(
n_estimators=15, criterion='entropy', random_state=42)
random_forest.fit(self.__x_train, self.__y_train)
self.__all_model_names.append('Random Forest Classifier')
self.__all_models.append(random_forest)
# Adaboost Classifier
adaboost_classifier = AdaBoostClassifier(n_estimators=3)
adaboost_classifier.fit(x_train, y_train)
self.__all_model_names.append('Adaboost Classifier')
self.__all_models.append(adaboost_classifier)
# Fit complete message
print('All models have been fit.')
def best_n_estimator(self):
error_rate = []
# Will take some time
k_values = list(filter(lambda x: x % 2 == 1, range(0, 50)))
for i in k_values:
knn = KNeighborsClassifier(n_neighbors=i)
knn.fit(self.__x_train, self.__y_train)
pred_i = knn.predict(self.__x_test)
error_rate.append(np.mean(pred_i != self.__y_test))
best_k_index = error_rate.index(np.min(error_rate))
self.__best_n_estimator = best_k_index * 2 + 1
def fit_test_set(self, user_x_test, user_y_test):
self.__x_test = user_x_test
self.__y_test = user_y_test
def apply_metric(self, metric):
metric_list = []
for _, model in enumerate(self.__all_models):
metric_item = metric(self.__y_test, model.predict(self.__x_test))
metric_list.append(metric_item)
self.report_printer(metric_list)
def report_printer(self, list_of_metric):
all_model_metrics = dict(zip(self.__all_model_names, list_of_metric))
for name, matrix in all_model_metrics.items():
print('{}\n{}\n\n'.format(name, matrix))
def count_values(self, list_of_values):
zeros = list_of_values.count(0)
ones = list_of_values.count(1)
if zeros == ones:
print('zeros == 1')
elif zeros > ones:
return 0
else:
return 1
def ensemble_model(self):
output_count = []
for _, model in enumerate(self.__all_models):
list_of_outputs = list(model.predict(self.__x_test))
output_count.append(self.count_values(list_of_outputs))
max_predicted_val = self.count_values(output_count)
print(max_predicted_val)