-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVM(support vector machine) grid randomised ensemble bagging votting.py
281 lines (219 loc) · 9.85 KB
/
SVM(support vector machine) grid randomised ensemble bagging votting.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 12 12:28:45 2019
@author: Chirag
"""
# Machine learning
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.externals import joblib
dataset = pd.read_csv('adult sal.csv', names = ['age',
'workclass',
'fnlwgt',
'education',
'education-num',
'marital-status',
'occupation',
'relationship',
'race',
'gender',
'capital-gain',
'capital-loss',
'hours-per-week',
'native-country',
'salary'],
na_values = ' ?')
X = dataset.iloc[:, 0:14].values
y = dataset.iloc[:, -1].values
test = pd.DataFrame(X[:, [1, 3, 5, 6, 7, 8, 9, 13]])
test[0].value_counts()
test[1].value_counts()
test[2].value_counts()
test[3].value_counts()
test[4].value_counts()
test[5].value_counts()
test[6].value_counts()
test[7].value_counts()
test[0] = test[0].fillna(' Private')
test[1] = test[1].fillna(' HS-grad')
test[2] = test[2].fillna(' Married-civ-spouse')
test[3] = test[3].fillna(' Prof-specialty')
test[4] = test[4].fillna(' Husband')
test[5] = test[5].fillna(' White')
test[6] = test[6].fillna(' Male')
test[7] = test[7].fillna(' United-States')
X[:, [1, 3, 5, 6, 7, 8, 9, 13]] = test
from sklearn.preprocessing import LabelEncoder
lab = LabelEncoder()
X[:, 1] = lab.fit_transform(X[:, 1].astype(str))
X[:, 3] = lab.fit_transform(X[:, 3])
X[:, 5] = lab.fit_transform(X[:, 5])
X[:, 6] = lab.fit_transform(X[:, 6])
X[:, 7] = lab.fit_transform(X[:, 7])
X[:, 8] = lab.fit_transform(X[:, 8])
X[:, 9] = lab.fit_transform(X[:, 9])
X[:, 13] = lab.fit_transform(X[:, 13])
from sklearn.preprocessing import OneHotEncoder
one = OneHotEncoder(categorical_features = [1, 3, 5, 6, 7, 8, 9, 13])
X = one.fit_transform(X)
X = X.toarray()
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X = sc.fit_transform(X)
y = lab.fit_transform(y)
lab.classes_
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y)
# =============================================================================
# from sklearn.svm import SVC
# svm = SVC()
# svm.fit(X_train, y_train)
# joblib.dump(svm, "svm.pkl")
# =============================================================================
svm = joblib.load("svm.pkl")
svm.score(X_train, y_train)#86.3 --- 85.8 --- 86.0
svm.score(X_test, y_test)#84. --- 86.2
svm.score(X, y)#85.9 --- 85.9 No Change
# =============================================================================
# from sklearn.linear_model import LogisticRegression
# log_reg = LogisticRegression()
# log_reg.fit(X_train, y_train)
# joblib.dump(log_reg, "log_reg.pkl")
# =============================================================================
log_reg = joblib.load("log_reg.pkl")
log_reg.score(X_train, y_train)#85.4 --- 85.0 --- 85.2
log_reg.score(X_test, y_test)#84.4 --- 85.6
log_reg.score(X, y)#85.1 --- 85.6
# =============================================================================
# from sklearn.neighbors import KNeighborsClassifier
# knn = KNeighborsClassifier()
# knn.fit(X_train, y_train)
# joblib.dump(knn, "knn.pkl")
# =============================================================================
knn = joblib.load("knn.pkl")
print(knn.score(X_train, y_train))#87.5 --- 86.0 --- 86.2
print(knn.score(X_test, y_test))#82.3 --- 86.8
print(knn.score(X, y))#86.2 --- 86.2 No Change
# =============================================================================
# from sklearn.tree import DecisionTreeClassifier
# dtc = DecisionTreeClassifier()
# dtc.fit(X_train, y_train)
# joblib.dump(dtc, "dtc.pkl")
# =============================================================================
dtc = joblib.load("dtc.pkl")
print(dtc.score(X_train, y_train))#99.9 --- 95.2 --- 95.2
print(dtc.score(X_test, y_test))#80.9 --- 95.2
print(dtc.score(X, y))#95.2 --- 95.2 No Change
# =============================================================================
# from sklearn.naive_bayes import GaussianNB
# n_b = GaussianNB()
# n_b.fit(X_train, y_train)
# joblib.dump(n_b, "naive.pkl")
# =============================================================================
n_b = joblib.load("naive.pkl")
print(n_b.score(X_train, y_train))#51.0 --- 50.7 --- 51.0
print(n_b.score(X_test, y_test))#50.1 --- 51.0
print(n_b.score(X, y))#50.8 --- 50.8 No Change
#----------Ensenbling----------
# #---------Voting---------------
# =============================================================================
# from sklearn.ensemble import VotingClassifier
# vot1 = VotingClassifier([('LR', log_reg),
# ('KNN', knn),
# ('SVM', svm)], voting ='hard')
# vot1.fit(X_train, y_train)
# joblib.dump(vot1, "Votting_LR_KNN_SVM.pkl")
# =============================================================================
vot1 = joblib.load("Votting_LR_KNN_SVM.pkl")
print(vot1.score(X_train, y_train))#86.7 --- 86.3 --- 86.3
print(vot1.score(X_test, y_test))#84.6
print(vot1.score(X, y))#86.2
#---------------------
# =============================================================================
# from sklearn.ensemble import VotingClassifier
# vot = VotingClassifier([('LR', log_reg),
# ('KNN', knn),
# ('NB', n_b)], voting ='soft')
# vot.fit(X_train, y_train)
# joblib.dump(vot, "Votting_LR_KNN_NB.pkl")
# =============================================================================
vot = joblib.load("Votting_LR_KNN_NB.pkl")
print(vot.score(X_train, y_train))#87.0 --- 86.2
print(vot.score(X_test, y_test))#83.5
print(vot.score(X, y))#86.1
#----------Bagging----------
# =============================================================================
# from sklearn.ensemble import BaggingClassifier
# bag = BaggingClassifier(knn, n_estimators = 5)
# bag.fit(X_train, y_train)
# joblib.dump(bag, "bag_knn.pkl")
# =============================================================================
bag = joblib.load("bag_knn.pkl")
print(bag.score(X_train, y_train))#87.4 --- 85.9
print(bag.score(X_test, y_test))#81.6
print(bag.score(X, y))#86
#---------Bagging of Decision Tree--------------Random Forest------------------
# =============================================================================
# from sklearn.ensemble import RandomForestClassifier
# rf = RandomForestClassifier()
# rf.fit(X_train, y_train)
# joblib.dump(rf, "RandomForest.pkl")
# =============================================================================
rf = joblib.load("RandomForest.pkl")
print(rf.score(X_train, y_train))#98.7 --- 95.0
print(rf.score(X_test, y_test))#84.2
print(rf.score(X, y))#95.1
#-------------Grid Search----------------
param_grid = {'n_neighbors' : [1,2,3,4,5,6,7,8,9] } #** [{'n_neighbors' : [1,2,3,4,5,6,7,8,9]}]
param_grid1 = [{'criterion' :['gini','entropy'] },
{'max_depth' :[3,4,5,6,7,8,9] }
]
# =============================================================================
# from sklearn.model_selection import GridSearchCV # cv - count vector (no. of instances)
# grid = GridSearchCV(knn, param_grid)
# %time grid.fit(X, y)#53m 20s --- 3h 54m 7s
# joblib.dump(grid, "GridSearch knn.pkl")
# =============================================================================
grid = joblib.load("GridSearch knn.pkl")
print("Best Estimator:- ",grid.best_estimator_)
print("Best Index:- ",grid.best_index_)
print("Best Parameter:- ",grid.best_params_)
print("Best Score:- ",grid.best_score_)
# =============================================================================
# grid1 = GridSearchCV(dtc, param_grid1)
# %time grid1.fit(X, y)# 7.5s
# joblib.dump(grid1, "GridSearch dtc.pkl")
# =============================================================================
grid1 = joblib.load("GridSearch dtc.pkl")
print("Best Estimator:- ",grid1.best_estimator_)
print("Best Index:- ",grid1.best_index_)
print("Best Parameter:- ",grid1.best_params_)
print("Best Score:- ",grid1.best_score_)
# max-depth = 8
#-----------------Randomized Search-----------------------
# =============================================================================
# from sklearn.model_selection import RandomizedSearchCV
# rand = RandomizedSearchCV(knn, param_distributions = param_grid)
# %time rand.fit(X_train, y_train)#30m 29s
# joblib.dump(rand, "RandomizedSearch knn.pkl")
# =============================================================================
rand = joblib.load("RandomizedSearch knn.pkl")
print(rand.best_estimator_)
print(rand.best_index_)
print(rand.best_params_)
print(rand.best_score_)
param_grid2 = {'criterion' :['gini','entropy'],
'max_depth' :[3,4,5,6,7,8,9]
}
# =============================================================================
# from sklearn.model_selection import RandomizedSearchCV
# rand1 = RandomizedSearchCV(dtc, param_distributions = param_grid2)
# %time rand1.fit(X_train, y_train)#5.2s
# joblib.dump(rand1, "RandomizedSearch dtc.pkl")
# =============================================================================
rand1 = joblib.load("RandomizedSearch dtc.pkl")
print(rand1.best_estimator_)
print(rand1.best_index_)
print(rand1.best_params_)
print(rand1.best_score_)