公众号:尤而小屋
作者:Peter
编辑:Peter

大家好,我是Peter~

本文是kaggle案例分享的第3篇,赛题的名称是:Mushroom Classification,Safe to eat or deadly poison?

数据来自UCI:https://archive.ics.uci.edu/ml/datasets/mushroom

kaggle源码地址:https://www.kaggle.com/nirajvermafcb/comparing-various-ml-models-roc-curve-comparison

排名

下面是kaggle上针对本题的排名。第一名侧重点是特征选择,没有用到本题的数据,我个人感觉跑偏了;第二名侧重点是基于贝叶斯理论的分类,能力有限,贝叶斯这块学习好了专门再说。

所以,选择了第三名的notebook源码来学习。作者将6种监督学习的方法在本数据集上的建模、模型评估等过程进行了比较。

数据集

这份数据集是UCI捐献给kaggle的。总样本数为8124,其中6513个样本做训练,1611个样本做测试;并且,其中可食用有4208样本,占51.8%;有毒的样本为3916,占48.2%。每个样本描述了蘑菇的22个属性,比如形状、气味等。

误食野生蘑菇中毒事件时有发生,且蘑菇形态千差万别,对于非专业人士,无法从外观、形态、颜色等方面区分有毒蘑菇与可食用蘑菇,没有一个简单的标准能够将有毒蘑菇和可食用蘑菇区分开来。要了解蘑菇是否可食用,必须采集具有不同特征属性的蘑菇是否有毒进行分析。

对蘑菇的22种特征属性进行分析,从而得到蘑菇可使用性模型,更好的预测出蘑菇是否可食用。

下面是UCI显示的具体数据信息:

属性特征的解释:

数据EDA

导入数据

import pandas as pd
import numpy as npimport plotly_express as px
from matplotlib import pyplot as plt
import seaborn as sns# 忽略警告
import warnings
warnings.filterwarnings('ignore')

原始数据有8124条记录,23个属性;并且不存在缺失值

有无毒对比

统计有毒和无毒的数量对比:

可视化分析

菌盖颜色

首先我们讨论下菌盖的颜色:每种菌盖颜色的次数

fig = px.bar(cap,x="color",y="number",color="number",text="number",color_continuous_scale="rainbow")# fig.update_layout(text_position="outside")
fig.show()

到底有毒的蘑菇是哪几种颜色较多了?统计有毒和无毒下的颜色分布:

 fig = px.bar(cap_class,x="color",y="number",color="class",text="number",barmode="group",)fig.show()

小结:颜色n、g、e在有毒p情况是比较多的

菌的气味

统计每种气味的数量:

fig = px.bar(odor,x="odor",y="number",color="number",text="number",color_continuous_scale="rainbow")fig.show()

上面是针对整体数据的情况,下面分有毒和无毒来继续讨论:

 fig = px.bar(odor_class,x="odor",y="number",color="class",text="number",barmode="group",)fig.show()

小结:从上面的两张图中,我们看出来:f这种气味是最容易造成有毒

特征相关性

将特征之间的相关性系数绘制成热力图,查看分布情况:

corr = data.corr()
sns.heatmap(corr)plt.show()

特征工程

特征转换

原数据中的特征都是文本类型,我们将其转成数值型,方便后续分析:

1、转换前

2、实施转换

from sklearn.preprocessing import LabelEncoder  # 类型编码
labelencoder = LabelEncoder()for col in data.columns:data[col] = labelencoder.fit_transform(data[col])# 转换后
data.head()

3、查看部分属性的转换结果

数据分布

查看数据转换编码后的数据分布情况:

ax = sns.boxplot(x='class', y='stalk-color-above-ring',data=data)ax = sns.stripplot(x="class", y='stalk-color-above-ring',data=data, jitter=True,edgecolor="gray")plt.title("Class w.r.t stalkcolor above ring",fontsize=12)plt.show()

分离特征和标签

X = data.iloc[:,1:23]  # 特征
y = data.iloc[:, 0]  # 标签

数据标准化

# 归一化(Normalization)、标准化(Standardization)from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X = scaler.fit_transform(X)
X

主成分分析PCA

PCA过程

原始数据中22个属性可能并不是特征都是有效数据,或者说某些属性本身就存在一定的关系,造成了特征属性的重叠。我们采用主成分分析,先找出关键的特征:

# 1、实施pca
from sklearn.decomposition import PCA
pca = PCA()
pca.fit_transform(X)# 2、得到相关系数
covariance = pca.get_covariance()# 3、得到每个变量对应的方差值
explained_variance=pca.explained_variance_
explained_variance

通过绘图来展示每个主成分的得分关系:

with plt.style.context("dark_background"):  # 背景plt.figure(figsize=(6,4))  # 大小plt.bar(range(22),  # 主成分个数explained_variance,  # 方差值alpha=0.5,  # 透明度align="center",label="individual explained variance"  # 标签)plt.ylabel('Explained variance ratio')  # 轴名称和图例plt.xlabel('Principal components')plt.legend(loc="best")plt.tight_layout()  # 自动调整子图参数

结论:从上面的图形中看出来最后的4个主成分方差之和很小;前面的17个占据了90%以上的方差,可作为主成分。

We can see that the last 4 components has less amount of variance of the data.The 1st 17 components retains more than 90% of the data.

2个主成分下的数据分布

然后我们利用基于2个属性的数据来实施K-means聚类:

1、2个主成分下的原始数据分布

N = data.values
pca = PCA(n_components=2)
x = pca.fit_transform(N)plt.figure(figsize=(5,5))
plt.scatter(x[:,0],x[:,1])
plt.show()

2、实施聚类建模后的分布:

from sklearn.cluster import KMeans
km = KMeans(n_clusters=2,random_state=5)N = data.values  # numpy数组形式
X_clustered = km.fit_predict(N)  # 建模结果0-1label_color_map = {0:"g",  # 分类结果只有0和1,进行打标1:"y"}
label_color = [label_color_map[l] for l in X_clustered]plt.figure(figsize=(5,5))
# x = pca.fit_transform(N)
plt.scatter(x[:,0],x[:,1], c=label_color)
plt.show()

基于17主成分下的建模

这个地方自己也没有看懂:总共是22个属性,上面选取了4个特征,为什么这里是基于17个主成分的分析??

先做了基于17个主成分的转换

数据集的划分:训练集和测试集占比为8-2

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=4)

下面开始是6种监督学习方法的具体过程:

模型1:逻辑回归

from sklearn.linear_model import LogisticRegression  # 逻辑回归(分类)
from sklearn.model_selection import cross_val_score  # 交叉验证得分
from sklearn import metrics  # 模型评价# 建立模型
model_LR = LogisticRegression()
model_LR.fit(X_train, y_train)

查看具体的预测效果:

model_LR.score(X_test,y_pred)# 结果
1.0  # 效果很好

逻辑回归下的混淆矩阵:

confusion_matrix = metrics.confusion_matrix(y_test, y_pred)
confusion_matrix# 结果
array([[815,  30],[ 36, 744]])

具体的auc值:

auc_roc = metrics.roc_auc_score(y_test, y_pred)  # 测试纸和预测值
auc_roc# 结果
0.9591715976331362

真假阳性

from sklearn.metrics import roc_curve, auc
false_positive_rate, true_positive_rate,thresholds = roc_curve(y_test, y_prob)roc_auc = auc(false_positive_rate,true_positive_rate)
roc_auc# 结果
0.9903474434835382

ROC曲线

import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.title("ROC")  # Receiver Operating Characteristic
plt.plot(false_positive_rate,true_positive_rate,color="red",label="AUC = %0.2f"%roc_auc)plt.legend(loc="lower right")
plt.plot([0,1],[0,1],linestyle="--")
plt.axis("tight")
# 真阳性:预测类别为1的positive;预测正确True
plt.ylabel("True Positive Rate")
# 假阳性:预测类别为1的positive;预测错误False
plt.xlabel("False Positive Rate")

下面是对逻辑回归模型进行校正。这里的校正主要就是采取网格搜索的方法来选取最佳的参数,然后进行下一步的建模。网格搜索的过程:

from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn import metrics# 未优化的模型
LR_model= LogisticRegression()
# 待确定的参数
tuned_parameters = {"C":[0.001,0.01,0.1,1,10,100,1000],"penalty":['l1','l2']  # 选择不同的正则方式,防止过拟合}
# 网格搜索模块
from sklearn.model_selection import GridSearchCV
# 加入网格搜索功能
LR = GridSearchCV(LR_model, tuned_parameters,cv=10)
# 搜索之后再建模
LR.fit(X_train, y_train)# 确定参数
print(LR.best_params_){'C': 100, 'penalty': 'l2'}

查看优化后的预测情况:

混淆矩阵和AUC情况:

ROC曲线情况:

from sklearn.metrics import roc_curve, auc
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_prob)#roc_auc = auc(false_positive_rate, true_positive_rate)import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.title("ROC")  # Receiver Operating Characteristic
plt.plot(false_positive_rate,true_positive_rate,color="red",label="AUC = %0.2f"%roc_auc)plt.legend(loc="lower right")
plt.plot([0,1],[0,1],linestyle="--")
plt.axis("tight")
# 真阳性:预测类别为1的positive;预测正确True
plt.ylabel("True Positive Rate")
# 假阳性:预测类别为1的positive;预测错误False
plt.xlabel("False Positive Rate")

模型2:高斯朴素贝叶斯

建模

from sklearn.naive_bayes import GaussianNB
model_naive = GaussianNB()# 建模
model_naive.fit(X_train, y_train)# 预测概率
y_prob = model_naive.predict_proba(X_test)[:,1]
y_pred = np.where(y_prob > 0.5,1,0)
model_naive.score(X_test,y_pred)# 结果
1

预测值和真实值不等的数量:111个

交叉验证

scores = cross_val_score(model_naive,X,y,cv=10,scoring="accuracy")
scores

混淆矩阵和AUC

真假阳性

# 导入评价模块
from sklearn.metrics import roc_curve, auc# 评价指标
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_prob)# roc曲线面积
roc_auc = auc(false_positive_rate, true_positive_rate)
roc_auc# 结果
0.9592201486876043

ROC曲线

AUC的值才0.96

# 绘图
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))plt.title("ROC")
plt.plot(false_positive_rate,true_positive_rate,color="red",label="AUC=%0.2f"%roc_auc)plt.legend(loc="lower right")
plt.plot([0,1],[0,1],linestyle='--')plt.axis("tight")
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()

模型3:支持向量机SVM

默认参数下的支持向量机过程

建模过程

from sklearn.svm import SVC
svm_model = SVC()tuned_parameters = {'C': [1, 10, 100,500, 1000],'kernel': ['linear','rbf'],'C': [1, 10, 100,500, 1000], 'gamma': [1,0.1,0.01,0.001, 0.0001], 'kernel': ['rbf']
}

随机网格搜索-RandomizedSearchCV

from sklearn.model_selection import RandomizedSearchCV# 建立随机搜索模型
model_svm = RandomizedSearchCV(svm_model,  # 待搜索模型tuned_parameters,  # 参数cv=10,  # 10折交叉验证scoring="accuracy",  # 评分标准n_iter=20  # 迭代次数)# 训练模型
model_svm.fit(X_train,y_train)
RandomizedSearchCV(cv=10, estimator=SVC(), n_iter=20,param_distributions={'C': [1, 10, 100, 500, 1000],'gamma': [1, 0.1, 0.01, 0.001, 0.0001],'kernel': ['rbf']},scoring='accuracy')
# 最佳得分效果
print(model_svm.best_score_)
1.0

得分最佳匹配参数:

# 预测
y_pred = model_svm.predict(X_test)# 预测值和原始标签值计算:分类准确率
metrics.accuracy_score(y_pred, y_test)
# 结果
1

混淆矩阵

查看具体的混淆矩阵和预测情况:

ROC曲线

from sklearn.metrics import roc_curve, auc
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_pred)
roc_auc = auc(false_positive_rate, true_positive_rate)import matplotlib.pyplot as pltplt.figure(figsize=(10,10))
plt.title('ROC')plt.plot(false_positive_rate,true_positive_rate, color='red',label = 'AUC = %0.2f' % roc_auc)plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],linestyle='--')plt.axis('tight')
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')

模型5:随机森林

建模拟合

from sklearn.ensemble import RandomForestClassifier# 建模
model_RR = RandomForestClassifier()
# 拟合
model_RR.fit(X_train, y_train)

预测得分

混淆矩阵

ROC曲线

from sklearn.metrics import roc_curve, aucfalse_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_prob)roc_auc = auc(false_positive_rate, true_positive_rate)
roc_auc  # 1import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.title('ROC')plt.plot(false_positive_rate,true_positive_rate, color='red',label = 'AUC = %0.2f' % roc_auc)plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],linestyle='--')plt.axis('tight')
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()

模型6:决策树(CART)

建模

from sklearn.tree import DecisionTreeClassifier# 建模
model_tree = DecisionTreeClassifier()
model_tree.fit(X_train, y_train)# 预测
y_prob = model_tree.predict_proba(X_test)[:,1]# 预测的概率转成0-1分类
y_pred = np.where(y_prob > 0.5, 1, 0)
model_tree.score(X_test, y_pred)
# 结果
1

混淆矩阵

各种评价指标的体现:

ROC曲线

from sklearn.metrics import roc_curve, auc
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_prob)
roc_auc = auc(false_positive_rate, true_positive_rate)
roc_auc  # 1import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))  # 画布
plt.title('ROC')  # 标题plt.plot(false_positive_rate,  # 绘图true_positive_rate, color='red',label = 'AUC = %0.2f' % roc_auc)  plt.legend(loc = 'lower right') #  图例位置
plt.plot([0, 1], [0, 1],linestyle='--')  # 正比例直线plt.axis('tight')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()

模型6:神经网络ANN

建模

混淆矩阵

ROC曲线

 # 真假阳性
from sklearn.metrics import roc_curve, auc
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_prob)
roc_auc = auc(false_positive_rate, true_positive_rate)
roc_auc  # 1# 绘制ROC曲线import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.title('ROC')
plt.plot(false_positive_rate,true_positive_rate, color='red',label = 'AUC = %0.2f' % roc_auc)plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],linestyle='--')plt.axis('tight')
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()

下面对神经网络的参数进行调优:

  • hidden_layer_sizes:隐藏层个数
  • activation:激活函数
  • alpha:学习率
  • max_iter:最大迭代次数

网格搜索

from sklearn.neural_network import MLPClassifier# 实例化
mlp_model = MLPClassifier()
# 待调节参数
tuned_parameters={'hidden_layer_sizes': range(1,200,10),'activation': ['tanh','logistic','relu'],'alpha':[0.0001,0.001,0.01,0.1,1,10],'max_iter': range(50,200,50)
}model_mlp= RandomizedSearchCV(mlp_model,tuned_parameters,cv=10,scoring='accuracy',n_iter=5,n_jobs= -1,random_state=5)
model_mlp.fit(X_train,y_train)

模型属性

调优之后的模型属性情况以及合适的参数:

ROC曲线

from sklearn.metrics import roc_curve, auc
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_prob)
roc_auc = auc(false_positive_rate, true_positive_rate)
roc_auc  # 1import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.title('ROC')plt.plot(false_positive_rate,true_positive_rate, color='red',label = 'AUC = %0.2f' % roc_auc)plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],linestyle='--')plt.axis('tight')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')

混淆矩阵和ROC

这是一篇很好的文章来解释混淆矩阵和ROC:https://www.cnblogs.com/wuliytTaotao/p/9285227.html

1、什么是混淆矩阵?

2、4大指标

TP、FP、TN、FN,第二个字母表示样本被预测的类别,第一个字母表示样本的预测类别与真实类别是否一致。

3、准确率

4、精准率和召回率

5、F_1和F_B

6、ROC曲线

AUC全称为Area Under Curve,表示一条曲线下面的面积,ROC曲线的AUC值可以用来对模型进行评价。ROC曲线如图 1 所示:

总结

看完这篇notebook源码,你需要掌握的知识点:

  • 机器学习建模整体思路:选择模型、建模、网格搜索调参、模型评估、ROC曲线(分类)
  • 特征工程的技术:编码转换、数据标准化、数据集划分
  • 评价指标:混淆矩阵、ROC曲线作为重点,后续有文章专门讲解

预告:后面Peter自己会专门写一篇来对这份数据进行建模分析,纯原创的思路,期待下~

基于6种监督学习(逻辑回归+决策树+随机森林+SVM+朴素贝叶斯+神经网络)的毒蘑菇分类相关推荐

  1. 数据分享|Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户...

    原文链接:http://tecdat.cn/?p=23518 项目背景:银行的主要盈利业务靠的是贷款,这些客户中的大多数是存款大小不等的责任客户(存款人).银行拥有不断增长的客户(点击文末" ...

  2. 基于逻辑回归/决策树/随机森林/多层感知分类器/xgboost/朴素贝叶斯分类的资讯多分类性能对比

    在上一篇(https://blog.csdn.net/baymax_007/article/details/82748544)中,利用逻辑回归实现资讯多分类.本文在之前基础上,又引入决策树.随机森林. ...

  3. 浅谈对机器学习方法(决策树,SVM,knn最近邻,随机森林,朴素贝叶斯、逻辑回归)的理解以及用sklearn工具实现文本分类和回归方法...

    一.决策树 定下一个最初的质点,从该点出发.分叉.(由于最初质点有可能落在边界值上,此时有可能会出现过拟合的问题. 二.SVM  svm是除深度学习在深度学习出现之前最好的分类算法了.它的特征如下: ...

  4. Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户

    最近我们被客户要求撰写关于银行拉新活动的研究报告,包括一些图形和统计输出. 项目背景:银行的主要盈利业务靠的是贷款,这些客户中的大多数是存款大小不等的责任客户(存款人).银行拥有不断增长的客户.该银行 ...

  5. ML之mlxtend:基于iris鸢尾花数据集利用逻辑回归LoR/随机森林RF/支持向量机SVM/集成学习算法结合mlxtend库实现模型可解释性(决策边界可视化)

    ML之mlxtend:基于iris鸢尾花数据集利用逻辑回归LoR/随机森林RF/支持向量机SVM/集成学习算法结合mlxtend库实现模型可解释性(决策边界可视化) 目录 相关文章 ML之mlxten ...

  6. 机器学习:基于朴素贝叶斯(Naive Bayes)的分类预测

    目录 一.简介和环境准备 简介: 环境: 二.实战演练 2.1使用葡萄(Wine)数据集,进行贝叶斯分类 1.数据导入 2.模型训练 3.模型预测 2.2模拟离散数据集–贝叶斯分类 1.数据导入.分析 ...

  7. Python实现基于朴素贝叶斯的垃圾邮件分类 标签: python朴素贝叶斯垃圾邮件分类 2016-04-20 15:09 2750人阅读 评论(1) 收藏 举报 分类: 机器学习(19) 听说

    Python实现基于朴素贝叶斯的垃圾邮件分类 标签: python朴素贝叶斯垃圾邮件分类 2016-04-20 15:09 2750人阅读 评论(1) 收藏 举报  分类: 机器学习(19)  听说朴 ...

  8. python实现随机森林、逻辑回归和朴素贝叶斯的新闻文本分类

    实现本文的文本数据可以在THUCTC下载也可以自己手动爬虫生成, 本文主要参考:https://blog.csdn.net/hao5335156/article/details/82716923 nb ...

  9. 基于朴素贝叶斯的垃圾邮件分类-着重理解拉普拉斯变换

    1. 引言 在正式学习朴素贝叶斯之前,需要明确的是机器学习所要实现的是基于有限的训练样本集尽可能准确地估计出后验概率P(c|x),即根据特征得到所属类别的概率,首先引入两个概念. 判别式模型(disc ...

最新文章

  1. 手把手教你估算深度神经网络的最优学习率(附代码教程)
  2. ReactJS入门之声明周期
  3. *【CodeForces - 280C】Game on Tree(期望模型,期望的线性性)
  4. 用python写一个自动注册脚本_python实现自动化上线脚本的示例
  5. Spring容器,控制反转,依赖注入
  6. 解决Vscode编辑器不能打开多标签页问题
  7. Android平台的通话计时源码
  8. [ExtJS5学习笔记]第22 Extjs5正在使用beforeLabelTpl添加所需的配置选项标注星号标记...
  9. golang map转json的顺序问题
  10. 在win10下安装Linux双系统
  11. 【Linux学习】vim编辑器的使用
  12. Matlab2017a/b激活license.lic文件
  13. 如何开发自己的股票软件105
  14. matlab相对误差均值计算公式,相对误差怎么算_平均相对误差怎么计算公式
  15. 基于C#(Visual Studio 2013)的AutoCAD2016二次开发(二)创建直线
  16. 绝地求生发生错误服务器维护,绝地求生更新时发生错误无法连接服务器解决办法最新版...
  17. 计算机配置高低怎么看,怎么看电脑配置高低
  18. android nfc标签类型,Android NFC标签 开发深度解析 触碰的艺术
  19. Java文件操作大全(包括文件加密,String加密)
  20. 【黑马程序员】C++核心编程2 -类与对象(封装、继承和多态)-this指针-友元-运算重载符-文本操作(附测试用例源码、测试结果图及详细注释)

热门文章

  1. 微信蓝牙设备服务器,微信又更新了 支持连接蓝牙设备
  2. 判断bigdecimal类型是正负数还是0+bigdecimal正负数转换
  3. 浅谈游戏《Cuphead茶杯头》
  4. 国际志愿者日 我们与爱同行
  5. 360FLEX与会者的演讲资源链接
  6. 一文了解加密游戏illuvium新玩法:探索神兽世界
  7. 【MySQL | 进阶篇】05、MySQL 视图、触发器讲解
  8. 智商情商哪个重要_情商和智商,到底哪个更重要?
  9. wildfly软件介绍
  10. 彩色rgb图像拆分为rgb三个通道,并重新合并为彩色图像