-
Notifications
You must be signed in to change notification settings - Fork 24
/
voting.py
85 lines (61 loc) · 2.83 KB
/
voting.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
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import VotingClassifier
from sklearn.metrics import accuracy_score
class Ensemble:
def __init__(self):
self.x_train = None
self.x_test = None
self.y_train = None
self.y_test = None
def load_data(self):
x, y = load_breast_cancer(return_X_y=True)
self.x_train, self.x_test, self.y_train, self.y_test = train_test_split(x, y, test_size=0.25, random_state=23)
@staticmethod
def __Classifiers__(name=None):
# See for reproducibility
random_state = 23
if name == 'decision_tree':
return DecisionTreeClassifier(random_state=random_state)
if name == 'kneighbors':
return KNeighborsClassifier()
if name == 'logistic_regression':
return LogisticRegression(random_state=random_state, solver='liblinear')
def __DecisionTreeClassifier__(self):
# Decision Tree Classifier
decision_tree = Ensemble.__Classifiers__(name='decision_tree')
# Train Decision Tree
decision_tree.fit(self.x_train, self.y_train)
def __KNearestNeighborsClassifier__(self):
# K-Nearest Neighbors Classifier
knn = Ensemble.__Classifiers__(name='kneighbors')
# Train K-Nearest Neighbos
knn.fit(self.x_train, self.y_train)
def __LogisticRegression__(self):
# Decision Tree Classifier
logistic_regression = Ensemble.__Classifiers__(name='logistic_regression')
# Init Grid Search
logistic_regression.fit(self.x_train, self.y_train)
def __VotingClassifier__(self):
# Instantiate classifiers
decision_tree = Ensemble.__Classifiers__(name='decision_tree')
knn = Ensemble.__Classifiers__(name='kneighbors')
logistic_regression = Ensemble.__Classifiers__(name='logistic_regression')
# Voting Classifier initialization
vc = VotingClassifier(estimators=[('decision_tree', decision_tree),
('knn', knn), ('logistic_regression',
logistic_regression)], voting='soft')
# Fitting the vc model
vc.fit(self.x_train, self.y_train)
# Getting train and test accuracies from meta_model
y_pred_train = vc.predict(self.x_train)
y_pred = vc.predict(self.x_test)
print(f"Train accuracy: {accuracy_score(self.y_train, y_pred_train)}")
print(f"Test accuracy: {accuracy_score(self.y_test, y_pred)}")
if __name__ == "__main__":
ensemble = Ensemble()
ensemble.load_data()
ensemble.__VotingClassifier__()