【阿旭机器学习实战】【34】使用SVM检测蘑菇是否有毒--支持向量机

【阿旭机器学习实战】系列文章主要介绍机器学习的各种算法模型及其实战案例,欢迎点赞,关注共同学习交流。

本文依据蘑菇的一些特征参数,使用SVM训练模型,用于检测蘑菇是否有毒( p )或者可以吃(e). 为两个类别的分类问题。

1. 导入并查看数据

关注GZH:阿旭算法与机器学习,回复:“ML34”即可获取本文数据集、源码与项目文档

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

# 导入数据
mush_df = pd.read_csv('mushrooms.csv')
mush_df.head()
classcap-shapecap-surfacecap-colorbruisesodorgill-attachmentgill-spacinggill-sizegill-color...stalk-surface-below-ringstalk-color-above-ringstalk-color-below-ringveil-typeveil-colorring-numberring-typespore-print-colorpopulationhabitat
0pxsntpfcnk...swwpwopksu
1exsytafcbk...swwpwopnng
2ebswtlfcbn...swwpwopnnm
3pxywtpfcnn...swwpwopksu
4exsgfnfwbk...swwpwoenag

5 rows × 23 columns

每个特征的含义如下:

  1. cap-shape: bell=b,conical=c,convex=x,flat=f, knobbed=k,sunken=s
  2. cap-surface: fibrous=f,grooves=g,scaly=y,smooth=s
  3. cap-color: brown=n,buff=b,cinnamon=c,gray=g,green=r, pink=p,purple=u,red=e,white=w,yellow=y
  4. bruises?: bruises=t,no=f
  5. odor: almond=a,anise=l,creosote=c,fishy=y,foul=f, musty=m,none=n,pungent=p,spicy=s
  6. gill-attachment: attached=a,descending=d,free=f,notched=n
  7. gill-spacing: close=c,crowded=w,distant=d
  8. gill-size: broad=b,narrow=n
  9. 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
  10. stalk-shape: enlarging=e,tapering=t
  11. stalk-root: bulbous=b,club=c,cup=u,equal=e, rhizomorphs=z,rooted=r,missing=?
  12. stalk-surface-above-ring: fibrous=f,scaly=y,silky=k,smooth=s
  13. stalk-surface-below-ring: fibrous=f,scaly=y,silky=k,smooth=s
  14. stalk-color-above-ring: brown=n,buff=b,cinnamon=c,gray=g,orange=o, pink=p,red=e,white=w,yellow=y
  15. stalk-color-below-ring: brown=n,buff=b,cinnamon=c,gray=g,orange=o, pink=p,red=e,white=w,yellow=y
  16. veil-type: partial=p,universal=u
  17. veil-color: brown=n,orange=o,white=w,yellow=y
  18. ring-number: none=n,one=o,two=t
  19. ring-type: cobwebby=c,evanescent=e,flaring=f,large=l, none=n,pendant=p,sheathing=s,zone=z
  20. spore-print-color: black=k,brown=n,buff=b,chocolate=h,green=r, orange=o,purple=u,white=w,yellow=y
  21. population: abundant=a,clustered=c,numerous=n, scattered=s,several=v,solitary=y
  22. habitat: grasses=g,leaves=l,meadows=m,paths=p, urban=u,waste=w,woods=d
  23. class:表示分类,p有毒,e没毒

1.1 将特征转为One-Hot编码

# 将值从字母转换为数字-onehot编码
mush_df_encoded = pd.get_dummies(mush_df)
mush_df_encoded.head()
class_eclass_pcap-shape_bcap-shape_ccap-shape_fcap-shape_kcap-shape_scap-shape_xcap-surface_fcap-surface_g...population_spopulation_vpopulation_yhabitat_dhabitat_ghabitat_lhabitat_mhabitat_phabitat_uhabitat_w
00100000100...1000000010
11000000100...0000100000
21010000000...0000001000
30100000100...1000000010
41000000100...0000100000

5 rows × 119 columns

1.2 分离特征数据与标签数据

# 将特征和类别标签分布赋值给 X 和 y
X_mush = mush_df_encoded.iloc[:,2:]
y_mush = mush_df_encoded.iloc[:,1]  #0表示没毒,1表示有毒

2. 训练SVM模型

建立pipeline训练管道

from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.pipeline import make_pipeline

#  先进行特征降维,然后再建立模型
pca = PCA(n_components=0.9, whiten=True, random_state=42)  # 保证降维后的数据保持90%的信息
svc = SVC(kernel='linear', class_weight='balanced')
model = make_pipeline(pca, svc)

将数据分为训练和测试数据

from sklearn.model_selection import train_test_split
Xtrain, Xtest, ytrain, ytest = train_test_split(X_mush, y_mush,random_state=41)   

调参:通过交叉验证寻找最佳的 C (控制间隔的大小)

from sklearn.model_selection import GridSearchCV

param_grid = {'svc__C': [1, 5, 10, 50]}
grid = GridSearchCV(model, param_grid)

%time grid.fit(Xtrain, ytrain)
print(grid.best_params_)
Wall time: 11.3 s
{'svc__C': 50}

使用训练好的SVM做预测

model = grid.best_estimator_
yfit = model.predict(Xtest)

生成性能报告

from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, Xtest, yfit, cv=5)
print(scores.mean())
0.9965517241379309
from sklearn.metrics import classification_report
print(classification_report(ytest, yfit,
                            target_names=['p','e']))
              precision    recall  f1-score   support

           p       1.00      1.00      1.00      1047
           e       1.00      0.99      1.00       984

    accuracy                           1.00      2031
   macro avg       1.00      1.00      1.00      2031
weighted avg       1.00      1.00      1.00      2031
from sklearn.metrics import confusion_matrix
mat = confusion_matrix(ytest, yfit)
sns.heatmap(mat.T, square=True, annot=True, fmt='d', cbar=False,
            xticklabels=['p','e'],
            yticklabels=['p','e'])
plt.xlabel('true label')
plt.ylabel('predicted label')
Text(113.9222222222222, 0.5, 'predicted label')

在这里插入图片描述

如果文章对你有帮助,感谢点赞+关注!

关注下方GZH:阿旭算法与机器学习,回复:“ML34”即可获取本文数据集、源码与项目文档,欢迎共同学习交流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿_旭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值