-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMushroom_Classification.py
More file actions
781 lines (436 loc) · 15.8 KB
/
Mushroom_Classification.py
File metadata and controls
781 lines (436 loc) · 15.8 KB
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#!/usr/bin/env python
# coding: utf-8
# # Competition
# ### Submitted by: Mohammed Ihsan P, Aleena Francis, Rustham Shahan V, Alka Sherine Benny, Reuben M Sunil
# ## Domain-Agriculture
# Mushroom Classification-:
#
# This dataset includes descriptions of hypothetical samples corresponding to 23 species of gilled mushrooms in the Agaricus and Lepiota Family Mushroom drawn from The Audubon Society Field Guide to North American Mushrooms (1981). Each species is identified as definitely edible, definitely poisonous, or of unknown edibility and not recommended. This latter class was combined with the poisonous one.
# ## Dataset Description:
# Attribute Information: (classes: edible=e, poisonous=p)
#
# cap-shape: bell=b,conical=c,convex=x,flat=f, knobbed=k,sunken=s
#
# cap-surface: fibrous=f,grooves=g,scaly=y,smooth=s
#
# cap-color: brown=n,buff=b,cinnamon=c,gray=g,green=r,pink=p,purple=u,red=e,white=w,yellow=y
#
# bruises: bruises=t,no=f
#
# odor: almond=a,anise=l,creosote=c,fishy=y,foul=f,musty=m,none=n,pungent=p,spicy=s
#
# gill-attachment: attached=a,descending=d,free=f,notched=n
#
# gill-spacing: close=c,crowded=w,distant=d
#
# gill-size: broad=b,narrow=n
#
# gill-color: black=k,brown=n,buff=b,chocolate=h,gray=g, green=r,orange=o,pink=p,purple=u,red=e,white=w,yellow=y
#
# stalk-shape: enlarging=e,tapering=t
#
# stalk-root: bulbous=b,club=c,cup=u,equal=e,rhizomorphs=z,rooted=r,missing=?
#
# stalk-surface-above-ring: fibrous=f,scaly=y,silky=k,smooth=s
#
# stalk-surface-below-ring: fibrous=f,scaly=y,silky=k,smooth=s
#
# stalk-color-above-ring: brown=n,buff=b,cinnamon=c,gray=g,orange=o,pink=p,red=e,white=w,yellow=y
#
# stalk-color-below-ring: brown=n,buff=b,cinnamon=c,gray=g,orange=o,pink=p,red=e,white=w,yellow=y
#
# veil-type: partial=p,universal=u
#
# veil-color: brown=n,orange=o,white=w,yellow=y
#
# ring-number: none=n,one=o,two=t
#
# ring-type: cobwebby=c,evanescent=e,flaring=f,large=l,none=n,pendant=p,sheathing=s,zone=z
#
# spore-print-color: black=k,brown=n,buff=b,chocolate=h,green=r,orange=o,purple=u,white=w,yellow=y
#
# population: abundant=a,clustered=c,numerous=n,scattered=s,several=v,solitary=y
#
# habitat: grasses=g,leaves=l,meadows=m,paths=p,urban=u,waste=w,woods=d
# In[1]:
#import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
# In[2]:
data = pd.read_csv('mushrooms.csv')
data.head()
# In[3]:
data.shape
# In[4]:
data.info()
# # --PreProcessing
# ## 1)- Handle missing values-:
# In[5]:
#check values in stalk-root column
data['stalk-root'].value_counts()
# In[6]:
#in stalk-root column the '?' represents missing values. So we have to convert it into null values
data.replace({'?': np.nan}, inplace=True)
# In[7]:
data['stalk-root'].value_counts()
# In[8]:
#check for missing values
data.isna().sum()
# In[9]:
data.dtypes
# In[10]:
data['stalk-shape'].value_counts()
# In[11]:
data['stalk-root'].value_counts()
# In[12]:
#here we will fill missing values of stalk-root column with mode, with respect to stalk-shape categories
data.loc[(data['stalk-root'].isna()) & (data['stalk-shape']=='t'),'stalk-root']=data[data['stalk-shape']=='t']['stalk-root'].mode()[0]
data.loc[(data['stalk-root'].isna()) & (data['stalk-shape']=='e'),'stalk-root']=data[data['stalk-shape']=='e']['stalk-root'].mode()[0]
# In[13]:
data.isna().sum()
# ## 2)-Encoding-:
# In[14]:
#one hot encoding
data1=pd.get_dummies(data[['cap-shape','cap-surface','veil-type','gill-attachment']])
#concat data frames data and data1
data=pd.concat([data,data1], axis=1)
data=data.drop(['cap-shape','cap-surface','veil-type','gill-attachment'],axis=1)
# In[15]:
data.head()
# In[16]:
#label encoding
from sklearn.preprocessing import LabelEncoder
label_en=LabelEncoder()
for i in data[['class','cap-color','bruises','odor','gill-spacing','gill-size','gill-color','stalk-shape','stalk-root','stalk-surface-above-ring','stalk-surface-below-ring','stalk-color-above-ring','stalk-color-below-ring','veil-color','ring-number','ring-type','spore-print-color','population','habitat']]:
data[i]=label_en.fit_transform(data[i])
# In[17]:
data.head()
# In[18]:
data.shape
# ## 3)-Feature Reduction-:
# In[19]:
data.columns
# In[20]:
#heatmap
plt.figure(figsize=(20,20))
sns.heatmap(data.corr(), annot=True , cmap='YlGnBu')
plt.tight_layout()
plt.show()
# gill attachment_f and gill attachment_a has strong correlation with veil color (one is strong positive and other is strong negative). So when we check the correlation between these columns and target column 'class', veil color has more correlation in both cases. Hence we could drop the other two columns.
# Since veil type_P has only one value for all the rows, we could drop this also.
# In[21]:
data=data.drop(['gill-attachment_f','gill-attachment_a','veil-type_p'],axis=1)
# In[22]:
data.head()
# # --Exploratory Data Analysis
# In[23]:
data.head()
# In[24]:
data.tail()
# In[25]:
data.info()
# In[26]:
data.shape
# In[27]:
data.columns
# In[28]:
data.describe()
# In[29]:
#pair plot
slice_data=data[['class','cap-color', 'bruises', 'gill-size','stalk-root','population', 'habitat']]
plt.figure(figsize=(10,10))
sns.pairplot(slice_data)
plt.tight_layout()
plt.show()
# In[30]:
#scatter plot
plt.figure(figsize=(5,3))
sns.scatterplot(x=data['ring-type'],y=data['bruises'])
plt.title('Scatterplot between Ring type and bruises')
plt.xlabel('ring-type')
plt.tight_layout()
plt.show()
# In[31]:
#scatter plot
plt.figure(figsize=(7,5))
sns.scatterplot(x=data['population'],y=data['habitat'], hue=data['class'])
plt.title('Scatterplot between population and habitat with respect to class')
plt.xlabel('population')
plt.tight_layout()
plt.show()
# In[32]:
#violin plot
sns.violinplot(data['population'])
plt.title('Violin Plot of Population')
plt.show()
# In[33]:
#boxplot
sns.boxplot(data['habitat'])
plt.title('Box Plot of Habitat')
plt.show()
# In[34]:
#countplot
sns.countplot(data['stalk-shape'])
plt.title('Countplot of Stalk-shape')
plt.show()
# Here 0 corresponds to enlarging shape and 1 corresponds to tapering shape.
# In[35]:
#heatmap
plt.figure(figsize=(20,20))
sns.heatmap(data.corr(), annot=True , cmap='YlGnBu')
plt.tight_layout()
plt.show()
# Insights-:
#
# * There is high positive correlation between ring-type and bruises.
#
# * There is high negative correlation between cap-shape_f and cap-shape_x.
#
# * There is high positive correlation between ring-type and gill color.
#
# * There is high negative correlation between gill-spacing and population.
#
# * There is high positive correlation between spore-print-color and gill-size.
# # --Modelling
# ## Split the dataset
# In[36]:
#split the data set into target and features
y = data['class']
x = data.drop(['class'], axis=1)
# In[37]:
#split the data set into train and test
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=42, test_size=0.3)
# In[38]:
#check how many data points are there in the training set
x_train.shape
# ## 1-Logistic Regression
# In[39]:
from sklearn.linear_model import LogisticRegression
logit_model = LogisticRegression()
logit_model = logit_model.fit(x_train, y_train)
y_predict = logit_model.predict(x_test)
# In[40]:
#check the performance of the model
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
# In[41]:
#check the performance of the model
print('Accuracy is :', accuracy_score(y_test,y_predict))
print('Precision is :', precision_score(y_test,y_predict))
print('Recall is :', recall_score(y_test,y_predict))
print('f1 Score is :', f1_score(y_test,y_predict))
# In[42]:
#check confusion matrix
confusion_matrix(y_test, y_predict)
# There are 98 misclassifications, we have to deal with.
# ## 2)-kNN
# In[43]:
from sklearn.neighbors import KNeighborsClassifier
#create model with varied k values
acc_values = []
#take 3 to 15 random values for k
neighbors = np.arange(3,15)
#loop to create kNN model for each k values
for k in neighbors:
classifier = KNeighborsClassifier(n_neighbors=k, metric='minkowski')
classifier.fit(x_train, y_train)
y_predict = classifier.predict(x_test)
acc = accuracy_score(y_test, y_predict)
acc_values.append(acc)
# In[44]:
print(acc_values)
# In[45]:
plt.plot(neighbors, acc_values, 'o-')
plt.xlabel('k-values')
plt.ylabel('Accuracy')
plt.show()
# We will take k value as 6
# In[46]:
#replace k as 6
classifier = KNeighborsClassifier(n_neighbors=6, metric='minkowski')
classifier.fit(x_train, y_train)
y_predict = classifier.predict(x_test)
# In[47]:
#check the performance of the model
print('Accuracy is :', accuracy_score(y_test,y_predict))
print('Precision is :', precision_score(y_test,y_predict))
print('Recall is :', recall_score(y_test,y_predict))
print('f1 Score is :', f1_score(y_test,y_predict))
# ## 3)-SVM
# ### Linear SVM
# In[48]:
#import library
from sklearn.svm import SVC
#create an instance of the model
svm_linear = SVC(kernel='linear')
svm_linear.fit(x_train, y_train)
y_predict = svm_linear.predict(x_test)
# In[49]:
#check the performance of the model
print('Accuracy is :', accuracy_score(y_test,y_predict))
print('Precision is :', precision_score(y_test,y_predict))
print('Recall is :', recall_score(y_test,y_predict))
print('f1 Score is :', f1_score(y_test,y_predict))
# ### Radial Based Function SVM
# In[50]:
#import library
from sklearn.svm import SVC
#create an instance of the model
svm_radial = SVC(kernel='rbf')
svm_radial.fit(x_train, y_train)
y_predict = svm_radial.predict(x_test)
# In[51]:
#check the performance of the model
print('Accuracy is :', accuracy_score(y_test,y_predict))
print('Precision is :', precision_score(y_test,y_predict))
print('Recall is :', recall_score(y_test,y_predict))
print('f1 Score is :', f1_score(y_test,y_predict))
# In[52]:
#check confusion matrix
confusion_matrix(y_test, y_predict)
# There are 33 misclassifications we have to deal with.
# ## 4)-Desicion Tree Classifier
# In[53]:
#import library
from sklearn.tree import DecisionTreeClassifier
#create an instance of the model
dt_model = DecisionTreeClassifier()
dt_model.fit(x_train, y_train)
y_predict = dt_model.predict(x_test)
# In[54]:
#check the performance of the model
print('Accuracy is :', accuracy_score(y_test,y_predict))
print('Precision is :', precision_score(y_test,y_predict))
print('Recall is :', recall_score(y_test,y_predict))
print('f1 Score is :', f1_score(y_test,y_predict))
# ## 5)-Random Forest Classifier
# In[55]:
#import libraries
from sklearn.ensemble import RandomForestClassifier
#create the instance of the model
rf=RandomForestClassifier()
#train the data
rf.fit(x_train,y_train)
#predict x_test
y_predict=rf.predict(x_test)
# In[56]:
#check the performance of the model
print('Accuracy is :', accuracy_score(y_test,y_predict))
print('Precision is :', precision_score(y_test,y_predict))
print('Recall is :', recall_score(y_test,y_predict))
print('f1 Score is :', f1_score(y_test,y_predict))
# ## 6)-Gradient Boosting Classifier
# In[57]:
from sklearn.ensemble import GradientBoostingClassifier
gb=GradientBoostingClassifier()
gb.fit(x_train,y_train)
#predict the y
y_predict=gb.predict(x_test)
# In[58]:
#check the performance of the model
print('Accuracy is :', accuracy_score(y_test,y_predict))
print('Precision is :', precision_score(y_test,y_predict))
print('Recall is :', recall_score(y_test,y_predict))
print('f1 Score is :', f1_score(y_test,y_predict))
# # --Model Fine Tuning
# Among the above models, kNN, Decision Tree and Random forest models have 100% accuracy, so there is no need of fine tuning. Hence we will fine tune the remaining models.
# ## Logistic Regression
# In[59]:
from sklearn.linear_model import LogisticRegression
logit_model = LogisticRegression(penalty='l2' ,C=5, solver='sag', max_iter=200, multi_class='auto', verbose=0, warm_start=True, n_jobs=None)
logit_model = logit_model.fit(x_train, y_train)
y_predict = logit_model.predict(x_test)
# In[60]:
#check the performance of the model
print('Accuracy is :', accuracy_score(y_test,y_predict))
print('Precision is :', precision_score(y_test,y_predict))
print('Recall is :', recall_score(y_test,y_predict))
print('f1 Score is :', f1_score(y_test,y_predict))
# By changing the hyper parameters of the model, we increased the performance of the model
# ## SVM
# ### Linear SVM
# In[61]:
#create an instance of the model
svm_linear = SVC(kernel='linear', C=2.0, max_iter=- 1 )
svm_linear.fit(x_train, y_train)
y_predict = svm_linear.predict(x_test)
# In[62]:
#check the performance of the model
print('Accuracy is :', accuracy_score(y_test,y_predict))
print('Precision is :', precision_score(y_test,y_predict))
print('Recall is :', recall_score(y_test,y_predict))
print('f1 Score is :', f1_score(y_test,y_predict))
# By changing the hyper parameters of the model, we increased the performance of the model
# ### Radial Based Function SVM
# In[63]:
#create an instance of the model
svm_radial = SVC(kernel='rbf',C=2.0, max_iter=- 1, gamma='auto')
svm_radial.fit(x_train, y_train)
y_predict = svm_radial.predict(x_test)
# In[64]:
#check the performance of the model
print('Accuracy is :', accuracy_score(y_test,y_predict))
print('Precision is :', precision_score(y_test,y_predict))
print('Recall is :', recall_score(y_test,y_predict))
print('f1 Score is :', f1_score(y_test,y_predict))
# By changing the hyper parameters of the model, we increased the performance of the model
# ## Gradient Boosting Classifier
# In[65]:
gb=GradientBoostingClassifier(n_estimators=150, subsample=1, criterion='friedman_mse', min_samples_split=3)
gb.fit(x_train,y_train)
#predict the y
y_predict=gb.predict(x_test)
# In[66]:
#check the performance of the model
print('Accuracy is :', accuracy_score(y_test,y_predict))
print('Precision is :', precision_score(y_test,y_predict))
print('Recall is :', recall_score(y_test,y_predict))
print('f1 Score is :', f1_score(y_test,y_predict))
# By changing the hyper parameters of the model, we increased the performance of the model
# In[ ]:
# # Feature importance-
# In[67]:
pd.Series(rf.feature_importances_, index=x.columns).sort_values(ascending=False)*100
# In[68]:
features_list = x.columns.values
feature_importance = rf.feature_importances_
sorted_idx = np.argsort(feature_importance)
plt.figure(figsize=(8,7))
plt.barh(range(len(sorted_idx)), feature_importance[sorted_idx], align='center', color ="red")
plt.yticks(range(len(sorted_idx)), features_list[sorted_idx])
plt.xlabel('Importance')
plt.title('Feature importance')
plt.draw()
#plt.savefig("featureimp.png", format='png', dpi=500, bbox_inches='tight')
plt.show()
# In[69]:
#drop the features having lower feature importance.
x.drop(['stalk-color-above-ring','stalk-shape','stalk-color-below-ring','ring-number','cap-color','cap-surface_f','cap-surface_s','veil-color','cap-shape_b','cap-shape_x','cap-surface_y','cap-shape_f','cap-shape_s','cap-shape_k','cap-shape_c','cap-surface_g'], axis=1, inplace=True)
# In[70]:
#split the data into train and test
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y, random_state=42, test_size=0.3)
# In[71]:
#import libraries
from sklearn.ensemble import RandomForestClassifier
#create the instance of the model
rf=RandomForestClassifier()
#train the data
rf.fit(x_train,y_train)
#predict x_test
y_predict=rf.predict(x_test)
# In[72]:
#check the performance of the model
print('Accuracy is :', accuracy_score(y_test,y_predict))
print('Precision is :', precision_score(y_test,y_predict))
print('Recall is :', recall_score(y_test,y_predict))
print('f1 Score is :', f1_score(y_test,y_predict))
# In[74]:
# save the model to disk
import pickle
pickle.dump(rf,open('model.pkl','wb'))
# In[ ]: