suvradeepp commited on
Commit
57176e9
·
verified ·
1 Parent(s): 7d6505b

Create sthelper.py

Browse files
Files changed (1) hide show
  1. sthelper.py +99 -0
sthelper.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sklearn.ensemble import RandomForestClassifier
2
+ from sklearn.ensemble import VotingClassifier
3
+ from sklearn.linear_model import LogisticRegression
4
+ from sklearn.svm import SVC
5
+ from sklearn.neighbors import KNeighborsClassifier
6
+ from sklearn.naive_bayes import GaussianNB
7
+ from sklearn.metrics import accuracy_score
8
+ import matplotlib.pyplot as plt
9
+ import numpy as np
10
+ from sklearn.model_selection import train_test_split
11
+
12
+ class StHelper:
13
+
14
+ def __init__(self,X,y):
15
+ self.X = X
16
+ self.y = y
17
+ # Apply train test split
18
+ self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(self.X, self.y, test_size=0.2, random_state=42)
19
+
20
+ def create_base_estimators(self,estimators,voting_type):
21
+
22
+ algos = []
23
+
24
+ if 'KNN' in estimators:
25
+ knn_clf = KNeighborsClassifier()
26
+ algos.append(('knn', knn_clf))
27
+ if 'Logistic Regression' in estimators:
28
+ log_clf = LogisticRegression(solver="lbfgs", random_state=42)
29
+ algos.append(('lr', log_clf))
30
+ if 'Gaussian Naive Bayes' in estimators:
31
+ gnb_clf = GaussianNB()
32
+ algos.append(('gnb', gnb_clf))
33
+ if 'SVM' in estimators:
34
+ if voting_type == "hard":
35
+ svm_clf = SVC(gamma="scale", random_state=42)
36
+ else:
37
+ svm_clf = SVC(gamma="scale", probability=True, random_state=42)
38
+ algos.append(('svc', svm_clf))
39
+ if 'Random Forest' in estimators:
40
+ rnd_clf = RandomForestClassifier(n_estimators=100, random_state=42)
41
+ algos.append(('rf', rnd_clf))
42
+
43
+ return algos
44
+
45
+ def train_voting_classifier(self,algos, voting_type):
46
+
47
+ voting_clf = VotingClassifier(
48
+ estimators=algos,
49
+ voting=voting_type)
50
+
51
+ voting_clf.fit(self.X_train, self.y_train)
52
+ y_pred = voting_clf.predict(self.X_test)
53
+
54
+ accuracy = accuracy_score(self.y_test, y_pred)
55
+
56
+ return voting_clf, accuracy
57
+
58
+ def draw_main_graph(self,voting_clf,ax):
59
+
60
+ XX, YY, input_array = self.draw_meshgrid()
61
+ labels = voting_clf.predict(input_array)
62
+ ax.contourf(XX, YY, labels.reshape(XX.shape), alpha=0.5, cmap='rainbow')
63
+
64
+ def plot_other_graphs(self,algos):
65
+
66
+ figs = []
67
+ XX, YY, input_array = self.draw_meshgrid()
68
+
69
+ for estimator in algos:
70
+ estimator[1].fit(self.X_train, self.y_train)
71
+ labels = estimator[1].predict(input_array)
72
+ fig1, ax1 = plt.subplots()
73
+ ax1.contourf(XX, YY, labels.reshape(XX.shape), alpha=0.5, cmap='rainbow')
74
+ figs.append(fig1)
75
+
76
+ return figs
77
+
78
+ def calculate_base_model_accuracy(self,algos):
79
+
80
+ accuracy_scores = []
81
+
82
+ for model in algos:
83
+ model[1].fit(self.X_train, self.y_train)
84
+ y_pred = model[1].predict(self.X_test)
85
+ accuracy_scores.append(accuracy_score(self.y_test, y_pred))
86
+
87
+ return accuracy_scores
88
+
89
+ def draw_meshgrid(self):
90
+ a = np.arange(start=self.X[:, 0].min() - 1, stop=self.X[:, 0].max() + 1, step=0.01)
91
+ b = np.arange(start=self.X[:, 1].min() - 1, stop=self.X[:, 1].max() + 1, step=0.01)
92
+
93
+ XX, YY = np.meshgrid(a, b)
94
+
95
+ input_array = np.array([XX.ravel(), YY.ravel()]).T
96
+
97
+ return XX, YY, input_array
98
+
99
+ labels = voting_clf.predict(input_array)