写在前面
在写这篇文章之前,首先安利下jupyter,简直是神作,既可以用来写文章,又可以用来写代码,文章和代码并存,简直就是写代码/文章/教程的利器。
安装很简单:pip install jupyter
使用很简单: 当前面目录下shift+右键呼出在此处打开命令窗口,输入jupyter notebook召唤神龙。
上面这段文字在jupyter中是这样的(markdown格式):
本文介绍
基于iris数据集进行数据分析。
iris数据集是常用的分类实验数据集,由Fisher,1936收集整理。iris也称鸢尾花卉数据集,是一类多重变量分析的数据集。数据集包含150个数据样本,分为3类,每类50个数据,每个数据包含4个属性。可通过花萼长度,花萼宽度,花瓣长度,花瓣宽度4个属性预测鸢尾花卉属于(Setosa,Versicolour,Virginica)三个种类中的哪一类。(来自百度百科)
数据预处理
首先使用padas相关的库进行数据读取,处理和预分析。
pandas的可视化user guide参见:
https://pandas.pydata.org/pandas-docs/stable/user_guide/visualization.html
首先读取信息,并查看数据的基本信息:可以看到数据的字段,数量,数据类型和大小。%matplotlib notebookimport pandas as pdimport matplotlib.pyplot as plt# 读取数据iris = pd.read_csv('iris.data.csv')
RangeIndex: 150 entries, 0 to 149
Data columns (total 5 columns):
Sepal.Length 150 non-null float64
Sepal.Width 150 non-null float64
Petal.Length 150 non-null float64
Petal.Width 150 non-null float64
type 150 non-null object
dtypes: float64(4), object(1)
memory usage: 5.9+ KB# 前5个数据iris.head()
Sepal.LengthSepal.WidthPetal.LengthPetal.Widthtype05.13.51.40.2Iris-setosa
14.93.01.40.2Iris-setosa
24.73.21.30.2Iris-setosa
34.63.11.50.2Iris-setosa
45.03.61.40.2Iris-setosa# 数据描述iris.describe()
Sepal.LengthSepal.WidthPetal.LengthPetal.Widthcount150.000000150.000000150.000000150.000000
mean5.8433333.0540003.7586671.198667
std0.8280660.4335941.7644200.763161
min4.3000002.0000001.0000000.100000
25%5.1000002.8000001.6000000.300000
50%5.8000003.0000004.3500001.300000
75%6.4000003.3000005.1000001.800000
max7.9000004.4000006.9000002.500000
箱线图描述了数据的分布情况,包括:上下界,上下四分位数和中位数,可以简单的查看数据的分布情况。
比如:上下四分位数相隔较远的话,一般可以很容易分为2类。
在《深入浅出统计分析》一书中,一个平均年龄17岁的游泳班,可能是父母带着婴儿的早教班,这种情况在箱线图上就能够清楚的反映出来。# 箱线图iris.plot(kind='box', subplots=True, layout=(2,2), sharex=False, sharey=False)Sepal.Length AxesSubplot(0.125,0.536818;0.352273x0.343182)
Sepal.Width AxesSubplot(0.547727,0.536818;0.352273x0.343182)
Petal.Length AxesSubplot(0.125,0.125;0.352273x0.343182)
Petal.Width AxesSubplot(0.547727,0.125;0.352273x0.343182)
dtype: objectarray([[,
],
[,
]],
dtype=object)
径向可视化是多维数据降维的可视化方法,不管是数据分析还是机器学习,降维是最基础的方法之一,通过降维,可以有效的减少复杂度。
径向坐标可视化是基于弹簧张力最小化算法。
它把数据集的特征映射成二维目标空间单位圆中的一个点,点的位置由系在点上的特征决定。把实例投入圆的中心,特征会朝圆中此实例位置(实例对应的归一化数值)“拉”实例。ax = pd.plotting.radviz(iris, 'type', colormap = 'brg')# radviz的源码中Circle未设置edgecolor,画圆需要自己处理ax.add_artist(plt.Circle((0,0), 1, color='r', fill = False))# 平行坐标可以看到数据中的类别以及从视觉上估计其他的统计量。# 使用平行坐标时,每个点用线段联接,每个垂直的线代表一个属性,# 一组联接的线段表示一个数据点。可能是一类的数据点会更加接近。pd.plotting.parallel_coordinates(iris, 'type', colormap = 'brg')# scatter matrixcolors = {'Iris-setosa': 'blue', 'Iris-versicolor': 'green', 'Iris-virginica': 'red'}array([[,
,
,
],
[,
,
,
],
[,
,
,
],
[,
,
,
]],
dtype=object)# 相关系数的热力图import seaborn as sea# pandas_profiling这个库可以对数据集进行初步预览,并进行报告,很不错,安装方式 pip install pandas_profiling# 运行略# import pandas_profiling as pp# pp.ProfileReport(iris)# 数据分类import sklearn as skfrom sklearn import preprocessingfrom sklearn import model_selection# 预处理X = iris[['Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width']]y = iris['type']encoder = preprocessing.LabelEncoder()y = encoder.fit_transform(y)[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2]from sklearn import metricsdef model_fit_show(model, model_name, X, y, test_size = 0.3, cluster = False):# logisticfrom sklearn import linear_model(105, 4) (45, 4) (105,) (45,)
[1 2 2 0 2 0 2 1 0 1 2 1 0 0 0 2 2 1 2 0 2 0 1 2 2 1 0 1 2 1 2 1 0 2 0 1 0
1 0 0 1 2 2 0 0]
accuracy of LogisticRegression is: 0.9111111111111111
[0](105, 4) (45, 4) (105,) (45,)
[2 0 1 0 0 0 1 2 0 0 1 0 2 0 2 1 1 0 2 0 2 0 0 1 1 2 0 2 0 1 2 1 1 1 1 2 1
1 2 1 1 2 2 2 0]
accuracy of DecisionTreeClassifier is: 0.9111111111111111
[0]#SVMfrom sklearn import svm(105, 4) (45, 4) (105,) (45,)
[2 0 0 2 0 1 0 2 1 2 2 0 1 1 0 1 1 2 1 0 2 2 2 1 0 2 2 1 1 1 0 1 0 0 2 0 0
2 0 0 1 2 1 0 0]
accuracy of svm.svc is: 0.9111111111111111
[0]
py:193: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.
"avoid this warning.", FutureWarning)# KNNfrom sklearn import neighbors(105, 4) (45, 4) (105,) (45,)
[1 2 2 2 0 0 0 1 2 2 1 2 1 1 1 2 0 2 0 0 1 1 0 0 1 0 2 2 0 0 2 2 1 1 0 1 1
0 1 1 2 1 1 0 0]
accuracy of neighbors.KNeighborsClassifier is: 0.9555555555555556
[0]# Kmeanfrom sklearn import cluster(105, 4) (45, 4) (105,) (45,)
[1 2 1 2 2 2 2 2 0 2 2 0 0 2 0 2 0 2 2 1 0 2 1 0 2 2 0 2 0 1 2 2 0 0 2 0 1
1 1 2 0 1 2 1 0]
[2]# naive bayes# https://scikit-learn.org/dev/modules/classes.html#module-sklearn.naive_bayes# 分别是GaussianNB,MultinomialNB和BernoulliNB。# GaussianNB:先验为高斯分布的朴素贝叶斯,一般应用于连续值# MultinomialNB:先验为多项式分布的朴素贝叶斯,离散多元值分类# BernoulliNB:先验为伯努利分布的朴素贝叶斯,离散二值分类# ComplementNB:对MultinomialNB的补充,适用于非平衡数据from sklearn import naive_bayes(105, 4) (45, 4) (105,) (45,)
[2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2]
accuracy of naive_bayes.BernoulliNB is: 0.24444444444444444
[2](105, 4) (45, 4) (105,) (45,)
[1 0 1 0 0 1 0 0 0 0 0 0 1 1 1 2 1 0 1 1 2 0 0 2 2 1 2 0 0 1 0 2 0 1 0 2 1
2 2 2 0 2 0 2 2]
accuracy of naive_bayes.GaussianNB is: 0.9333333333333333
[0](105, 4) (45, 4) (105,) (45,)
[0 2 2 1 2 1 2 0 2 1 2 0 2 0 1 1 2 1 0 2 2 2 0 1 1 1 0 2 2 1 1 2 1 0 0 1 0
0 1 1 2 2 1 2 1]
accuracy of naive_bayes.MultinomialNB is: 0.9555555555555556
[0](105, 4) (45, 4) (105,) (45,)
[2 2 2 2 2 2 2 2 2 2 2 0 2 2 2 0 2 2 2 2 0 0 0 2 2 2 0 0 2 2 2 0 2 2 2 0 0
2 0 2 2 2 2 0 2]
accuracy of naive_bayes.ComplementNB is: 0.6
[0]