/
Classification.py
558 lines (341 loc) · 15.1 KB
/
Classification.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
get_ipython().system('pip install numpy')
get_ipython().system('pip install matplotlib')
get_ipython().system('pip install seaborn')
get_ipython().system('pip install pandas')
get_ipython().system('pip install sklearn')
get_ipython().system('pip install scipy')
get_ipython().system('pip install pydotplus')
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from sklearn.naive_bayes import GaussianNB
import sklearn as skl
from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier
from sklearn.model_selection import train_test_split # Import train_test_split function
from sklearn import metrics #Import scikit-learn metrics module for accuracy calculation
import pydotplus
# # Classification using sklearn
# Scikit-learn (sklearn) is a Python library dedicated to machine learning. It contains classifier and regression algorithm objects which implement an API for training and predicting models. Additionally, it contains some methods for data manipulation and performance metric measuring of predictrive models.
# ### Quick example - Iris dataset
# In[ ]:
iris = sns.load_dataset('iris') #seaborn has some built in datasets
iris.head()
# In[ ]:
sns.pairplot(iris, hue='species', height=1.5);
# ##### Splitting the data set into feature vector X and target variable y
# In[ ]:
X_iris = iris.drop('species', axis=1)
print(X_iris.shape)
y_iris = iris['species']
print(y_iris.shape)
# ##### Splitting the data set into training and test sets. By default, test set size is 25% of data set.
# In[ ]:
#from sklearn.model_selection import train_test_split
Xtrain, Xtest, ytrain, ytest = train_test_split(X_iris, y_iris,
random_state=1)
# ##### Training and predicting using Naive Bayes classifier
# In[ ]:
#from sklearn.naive_bayes import GaussianNB # 1. choose model class
model = GaussianNB() # 2. instantiate model
model.fit(Xtrain, ytrain) # 3. fit model to data
y_model = model.predict(Xtest) # 4. predict on new data (output is numpy array)
ypred = pd.Series(y_model,name="prediction")
predicted = pd.concat([Xtest.reset_index(),ytest.reset_index(),ypred],axis=1)
predicted
# ##### Calculate the accuracy as an average of accuracy per class
# In[ ]:
#from sklearn import metrics
metrics.accuracy_score(ytest, y_model)
# ###### What happens if we select less columns?
# In[ ]:
X_iris = iris.drop(['species','petal_length','petal_width'], axis=1)
y_iris = iris['species']
Xtrain, Xtest, ytrain, ytest = train_test_split(X_iris, y_iris,
random_state=1)
model = GaussianNB() # 2. instantiate model
model.fit(Xtrain, ytrain) # 3. fit model to data
y_model = model.predict(Xtest) # 4. predict on new data (output is numpy array)
ypred = pd.Series(y_model,name="prediction")
predicted = pd.concat([Xtest.reset_index(),ytest.reset_index(),ypred],axis=1)
print(metrics.accuracy_score(ytest, y_model))
predicted
# In[ ]:
X_iris = iris.drop(['species','sepal_length','sepal_width'], axis=1)
y_iris = iris['species']
Xtrain, Xtest, ytrain, ytest = train_test_split(X_iris, y_iris,
random_state=1)
model = GaussianNB() # 2. instantiate model
model.fit(Xtrain, ytrain) # 3. fit model to data
y_model = model.predict(Xtest) # 4. predict on new data (output is numpy array)
ypred = pd.Series(y_model,name="prediction")
predicted = pd.concat([Xtest.reset_index(),ytest.reset_index(),ypred],axis=1)
print(metrics.accuracy_score(ytest, y_model))
predicted
# In[ ]:
def bayes_plot(df,model="gnb",spread=30):
df.dropna()
colors = 'seismic'
col1 = df.columns[0]
col2 = df.columns[1]
target = df.columns[2]
sns.scatterplot(data=df, x=col1, y=col2,hue=target)
plt.show()
y = df[target] # Target variable
X = df.drop(target, axis=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=1) # 70% training and 30% test
clf = GaussianNB()
if (model != "gnb"):
clf = DecisionTreeClassifier(max_depth=model)
clf = clf.fit(X_train, y_train)
# Train Classifer
prob = len(clf.classes_) == 2
# Predict the response for test dataset
y_pred = clf.predict(X_test)
print(metrics.classification_report(y_test, y_pred))
hueorder = clf.classes_
def numify(val):
return np.where(clf.classes_ == val)[0]
Y = y.apply(numify)
x_min, x_max = X.loc[:, col1].min() - 1, X.loc[:, col1].max() + 1
y_min, y_max = X.loc[:, col2].min() - 1, X.loc[:, col2].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.2),
np.arange(y_min, y_max, 0.2))
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])
if prob:
Z = Z[:,1]-Z[:,0]
else:
colors = "Set1"
Z = np.argmax(Z, axis=1)
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=colors, alpha=0.5)
plt.colorbar()
if not prob:
plt.clim(0,len(clf.classes_)+3)
sns.scatterplot(data=df[::spread], x=col1, y=col2, hue=target, hue_order=hueorder,palette=colors)
fig = plt.gcf()
fig.set_size_inches(12, 8)
plt.show()
# In[ ]:
bayes_plot(pd.concat([X_iris,y_iris],axis=1),spread=1)
# In[ ]:
iris_poor = iris.drop(['petal_length','petal_width'], axis=1)
bayes_plot(iris_poor,spread=1)
# In[ ]:
bayes_plot(pd.concat([X_iris,y_iris],axis=1),model=4,spread=1)
# In[ ]:
bayes_plot(pd.concat([X_iris,y_iris],axis=1),model=2,spread=1)
# In[ ]:
from sklearn.datasets import make_blobs
X, y = make_blobs(n_samples=300, centers=4,
random_state=0, cluster_std=1.0)
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='rainbow');
# In[ ]:
blob_df = pd.DataFrame(X,columns=['X','Y'])
blob_series = pd.Series(y,name="target")
print(pd.concat([blob_df,blob_series],axis=1))
bayes_plot(pd.concat([blob_df,blob_series],axis=1),model=3,spread=1)
# In[ ]:
bayes_plot(pd.concat([blob_df,blob_series],axis=1),model=5,spread=1)
# *Note: we have a slight drop in accuracy, perhaps due to overfitting*
# In[ ]:
from io import StringIO
get_ipython().system('conda install -y python-graphviz')
from IPython.display import Image
from sklearn.tree import export_graphviz
# In[ ]:
from sklearn.inspection import permutation_importance
tech = pd.read_csv('dataset-tortuga.csv')
tech.dropna(inplace=True)
X = tech.drop(["PROFILE", "NAME","USER_ID","Unnamed: 0"],axis=1)
y = tech['PROFILE']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=1) # 70% training and 30% test
clf = DecisionTreeClassifier()
clf.fit(X_train,y_train)
result = permutation_importance(clf, X, y, n_repeats=10,random_state=0)
importance = zip(X.columns,result['importances_mean'])
# summarize feature importance
for i,v in importance:
print('Feature: %s, Score: %.5f' % (i,v))
# plot feature importance
print(len(X.columns),[x[1] for x in importance])
plt.bar(range(len(X.columns)), result['importances_mean'])
plt.xticks(ticks=range(len(X.columns)),labels=X.columns, rotation=90)
plt.show()
y_pred = clf.predict(X_test)
print(metrics.classification_report(y_test, y_pred))
dot_data=StringIO()
export_graphviz(clf,out_file=dot_data,filled=True,rounded=True,feature_names=X.columns,class_names=clf.classes_)
graph=pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('Tech.png')
Image(graph.create_png())
# In[ ]:
#highest importance
X_2d = X[["NUM_COURSES_BEGINNER_BACKEND","AVG_SCORE_DATASCIENCE"]]
y_2d = tech['PROFILE']
X_train, X_test, y_train, y_test = train_test_split(X_2d, y_2d, test_size=0.3,random_state=1) # 70% training and 30% test
clf = DecisionTreeClassifier()
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print(metrics.classification_report(y_test, y_pred))
dot_data=StringIO()
export_graphviz(clf,out_file=dot_data,filled=True,rounded=True,feature_names=X_2d.columns,class_names=clf.classes_)
graph=pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('Tech2d.png')
Image(graph.create_png())
# *Note: this does not mean that the top "important" features will always yield the best results. Remember that some split decisions are random and will affect importance.*
# In[ ]:
bayes_plot(pd.concat([X_2d,y_2d],axis=1),model=20,spread=10)
# In[ ]:
#lowest importance
X_2d = X[["NUM_COURSES_ADVANCED_DATASCIENCE","NUM_COURSES_ADVANCED_FRONTEND"]]
y_2d = tech['PROFILE']
X_train, X_test, y_train, y_test = train_test_split(X_2d, y_2d, test_size=0.3,random_state=1) # 70% training and 30% test
clf = DecisionTreeClassifier()
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print(metrics.classification_report(y_test, y_pred))
# In[ ]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=1) # 70% training and 30% test
clf = DecisionTreeClassifier(max_depth=4)
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print(metrics.classification_report(y_test, y_pred))
dot_data=StringIO()
export_graphviz(clf,out_file=dot_data,filled=True,rounded=True,feature_names=X.columns,class_names=clf.classes_)
graph=pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('Tech_depth4.png')
Image(graph.create_png())
# In[ ]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=1) # 70% training and 30% test
clf = DecisionTreeClassifier(max_depth=6)
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print(metrics.classification_report(y_test, y_pred))
dot_data=StringIO()
export_graphviz(clf,out_file=dot_data,filled=True,rounded=True,feature_names=X.columns,class_names=clf.classes_)
graph=pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('Tech_depth6.png')
Image(graph.create_png())
# In[ ]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=1) # 70% training and 30% test
clf = DecisionTreeClassifier(min_impurity_decrease=0.003)
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print(metrics.classification_report(y_test, y_pred))
dot_data=StringIO()
export_graphviz(clf,out_file=dot_data,filled=True,rounded=True,feature_names=X.columns,class_names=clf.classes_)
graph=pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('Tech_impurity.png')
Image(graph.create_png())
# In[ ]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=1) # 70% training and 30% test
clf = DecisionTreeClassifier(max_leaf_nodes =50)
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print(metrics.classification_report(y_test, y_pred))
dot_data=StringIO()
export_graphviz(clf,out_file=dot_data,filled=True,rounded=True,feature_names=X.columns,class_names=clf.classes_)
graph=pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('Tech_maxleaf.png')
Image(graph.create_png())
# In[ ]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=1) # 70% training and 30% test
clf = DecisionTreeClassifier(min_samples_split=0.01)
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print(metrics.classification_report(y_test, y_pred))
dot_data=StringIO()
export_graphviz(clf,out_file=dot_data,filled=True,rounded=True,feature_names=X.columns,class_names=clf.classes_)
graph=pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('Tech_minsamplesplit.png')
Image(graph.create_png())
# In[ ]:
X_cut = X.drop(["NUM_COURSES_ADVANCED_DATASCIENCE","NUM_COURSES_ADVANCED_FRONTEND"],axis=1)
X_train, X_test, y_train, y_test = train_test_split(X_cut, y, test_size=0.3,random_state=1) # 70% training and 30% test
clf = DecisionTreeClassifier(min_samples_split=0.01)
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print(metrics.classification_report(y_test, y_pred))
dot_data=StringIO()
export_graphviz(clf,out_file=dot_data,filled=True,rounded=True,feature_names=X_cut.columns,class_names=clf.classes_)
graph=pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('Tech_minsamplesplit2.png')
Image(graph.create_png())
# #### Filling NaNs
# In[ ]:
tech = pd.read_csv('dataset-tortuga.csv')
def nullify(x):
if np.random.random() < 0.5:
return np.NaN
else:
return x
tech['HOURS_BACKEND'] = tech['HOURS_BACKEND'].apply(nullify)
tech['HOURS_FRONTEND'] = tech['HOURS_FRONTEND'].apply(nullify)
tech['HOURS_DATASCIENCE'] = tech['HOURS_DATASCIENCE'].apply(nullify)
techna = tech.dropna()
X = techna.drop(["PROFILE", "NAME","USER_ID","Unnamed: 0"],axis=1)
y = techna['PROFILE']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=1) # 70% training and 30% test
clf = DecisionTreeClassifier()
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print(metrics.classification_report(y_test, y_pred))
# In[ ]:
techna = tech.copy()
techna['HOURS_BACKEND'].fillna(tech['HOURS_BACKEND'].mean(),inplace=True)
techna['HOURS_FRONTEND'].fillna(tech['HOURS_FRONTEND'].mean(),inplace=True)
techna['HOURS_DATASCIENCE'].fillna(tech['HOURS_DATASCIENCE'].mean(),inplace=True)
techna = techna.dropna()
X = techna.drop(["PROFILE", "NAME","USER_ID","Unnamed: 0"],axis=1)
y = techna['PROFILE']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=1) # 70% training and 30% test
clf = DecisionTreeClassifier()
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print(metrics.classification_report(y_test, y_pred))
# ## Classifying the Titanic Survivors
# In[ ]:
titanic = pd.read_csv('titanic.csv')
titanic
titanic.isna().sum()
# In[ ]:
titanic_data = titanic.drop("PassengerId,Name,Ticket,Fare,Cabin".split(','),axis=1)
titanic_data["Age"].fillna(titanic_data["Age"].mean(),inplace=True)
titanic_data.dropna(inplace=True)
X = titanic_data.drop(["Survived"],axis=1)
X = pd.get_dummies(X)
y = titanic_data['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=1) # 70% training and 30% test
clf = DecisionTreeClassifier()
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print(metrics.classification_report(y_test, y_pred))
result = permutation_importance(clf, X, y, n_repeats=1,random_state=0)
importance = zip(X.columns,result['importances_mean'])
# summarize feature importance
for i,v in importance:
print('Feature: %s, Score: %.5f' % (i,v))
# plot feature importance
print(len(X.columns),[x[1] for x in importance])
plt.bar(range(len(X.columns)), result['importances_mean'])
plt.xticks(ticks=range(len(X.columns)),labels=X.columns, rotation=90)
plt.show()
# In[ ]:
X_2d = X[["Age","Sex_female"]]
y_2d = y
bayes_plot(pd.concat([X_2d,y_2d],axis=1),model=20,spread=10)
# In[ ]:
X_train, X_test, y_train, y_test = train_test_split(X_2d, y, test_size=0.3,random_state=1) # 70% training and 30% test
clf = DecisionTreeClassifier()
clf.fit(X_train,y_train)
dot_data=StringIO()
export_graphviz(clf,out_file=dot_data,filled=True,rounded=True,feature_names=X_2d.columns,class_names=["0","1"])
graph=pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('titanic.png')
Image(graph.create_png())
# In[ ]: