Iris 鸢尾花数据集是一个经典数据集,在统计学习和机器学习领域都经常被用作示例。数据集内包含 3 类共 150 条记录,每类各 50 个数据,每条记录都有 4 项特征:花萼长度、花萼宽度、花瓣长度、花瓣宽度,可以通过这4个特征预测鸢尾花卉属于(iris-setosa, iris-versicolour, iris-virginica)中的哪一品种。

据说在现实中,这三种花的基本判别依据其实是种子(因为花瓣非常容易枯萎)。

0 准备数据

下面对 iris 进行探索性分析,首先导入相关包和数据集:

# 导入相关包

import numpy as np

import pandas as pd

from pandas import plotting

%matplotlib inline

import matplotlib.pyplot as plt

plt.style.use('seaborn')

import seaborn as sns

sns.set_style("whitegrid")

from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import LabelEncoder

from sklearn.neighbors import KNeighborsClassifier

from sklearn import svm

from sklearn import metrics

from sklearn.tree import DecisionTreeClassifier

# 导入数据集

iris = pd.read_csv('F:\pydata\dataset\kaggle\iris.csv', usecols=[1, 2, 3, 4, 5])

查看数据集信息:

iris.info()

RangeIndex: 150 entries, 0 to 149

Data columns (total 5 columns):

SepalLengthCm 150 non-null float64

SepalWidthCm 150 non-null float64

PetalLengthCm 150 non-null float64

PetalWidthCm 150 non-null float64

Species 150 non-null object

dtypes: float64(4), object(1)

memory usage: 5.9+ KB

查看数据集的头 5 条记录:

iris.head()

1 探索性分析

先查看数据集各特征列的摘要统计信息:

iris.describe()

通过Violinplot 和 Pointplot,分别从数据分布和斜率,观察各特征与品种之间的关系:

# 设置颜色主题

antV = ['#1890FF', '#2FC25B', '#FACC14', '#223273', '#8543E0', '#13C2C2', '#3436c7', '#F04864']

# 绘制 Violinplot

f, axes = plt.subplots(2, 2, figsize=(8, 8), sharex=True)

sns.despine(left=True)

sns.violinplot(x='Species', y='SepalLengthCm', data=iris, palette=antV, ax=axes[0, 0])

sns.violinplot(x='Species', y='SepalWidthCm', data=iris, palette=antV, ax=axes[0, 1])

sns.violinplot(x='Species', y='PetalLengthCm', data=iris, palette=antV, ax=axes[1, 0])

sns.violinplot(x='Species', y='PetalWidthCm', data=iris, palette=antV, ax=axes[1, 1])

plt.show()

# 绘制 pointplot

f, axes = plt.subplots(2, 2, figsize=(8, 8), sharex=True)

sns.despine(left=True)

sns.pointplot(x='Species', y='SepalLengthCm', data=iris, color=antV[0], ax=axes[0, 0])

sns.pointplot(x='Species', y='SepalWidthCm', data=iris, color=antV[0], ax=axes[0, 1])

sns.pointplot(x='Species', y='PetalLengthCm', data=iris, color=antV[0], ax=axes[1, 0])

sns.pointplot(x='Species', y='PetalWidthCm', data=iris, color=antV[0], ax=axes[1, 1])

plt.show()

生成各特征之间关系的矩阵图:

g = sns.pairplot(data=iris, palette=antV, hue= 'Species')

使用 Andrews Curves 将每个多变量观测值转换为曲线并表示傅立叶级数的系数,这对于检测时间序列数据中的异常值很有用。

Andrews Curves 是一种通过将每个观察映射到函数来可视化多维数据的方法。

plt.subplots(figsize = (10,8))

plotting.andrews_curves(iris, 'Species', colormap='cool')

plt.show()

下面分别基于花萼和花瓣做线性回归的可视化:

g = sns.lmplot(data=iris, x='SepalWidthCm', y='SepalLengthCm', palette=antV, hue='Species')

g = sns.lmplot(data=iris, x='PetalWidthCm', y='PetalLengthCm', palette=antV, hue='Species')

最后,通过热图找出数据集中不同特征之间的相关性,高正值或负值表明特征具有高度相关性:

fig=plt.gcf()

fig.set_size_inches(12, 8)

fig=sns.heatmap(iris.corr(), annot=True, cmap='GnBu', linewidths=1, linecolor='k', square=True, mask=False, vmin=-1, vmax=1, cbar_kws={"orientation": "vertical"}, cbar=True)

从热图可看出,花萼的宽度和长度不相关,而花瓣的宽度和长度则高度相关。

2 机器学习

接下来,通过机器学习,以花萼和花瓣的尺寸为根据,预测其品种。

在进行机器学习之前,将数据集拆分为训练和测试数据集。首先,使用标签编码将 3 种鸢尾花的品种名称转换为分类值(0, 1, 2)。

# 载入特征和标签集

X = iris[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]

y = iris['Species']

# 对标签集进行编码

encoder = LabelEncoder()

y = encoder.fit_transform(y)

print(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]

接着,将数据集以 7: 3 的比例,拆分为训练数据和测试数据:

train_X, test_X, train_y, test_y = train_test_split(X, y, test_size = 0.3, random_state = 101)

print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)

(105, 4) (105,) (45, 4) (45,)

检查不同模型的准确性:

# Support Vector Machine

model = svm.SVC()

model.fit(train_X, train_y)

prediction = model.predict(test_X)

print('The accuracy of the SVM is: {0}'.format(metrics.accuracy_score(prediction,test_y)))

The accuracy of the SVM is: 1.0

# Logistic Regression

model = LogisticRegression()

model.fit(train_X, train_y)

prediction = model.predict(test_X)

print('The accuracy of the Logistic Regression is: {0}'.format(metrics.accuracy_score(prediction,test_y)))

The accuracy of the Logistic Regression is: 0.9555555555555556

# Decision Tree

model=DecisionTreeClassifier()

model.fit(train_X, train_y)

prediction = model.predict(test_X)

print('The accuracy of the Decision Tree is: {0}'.format(metrics.accuracy_score(prediction,test_y)))

The accuracy of the Decision Tree is: 0.9555555555555556

# K-Nearest Neighbours

model=KNeighborsClassifier(n_neighbors=3)

model.fit(train_X, train_y)

prediction = model.predict(test_X)

print('The accuracy of the KNN is: {0}'.format(metrics.accuracy_score(prediction,test_y)))

The accuracy of the KNN is: 1.0

上面使用了数据集的所有特征,下面将分别使用花瓣和花萼的尺寸:

petal = iris[['PetalLengthCm', 'PetalWidthCm', 'Species']]

train_p,test_p=train_test_split(petal,test_size=0.3,random_state=0)

train_x_p=train_p[['PetalWidthCm','PetalLengthCm']]

train_y_p=train_p.Species

test_x_p=test_p[['PetalWidthCm','PetalLengthCm']]

test_y_p=test_p.Species

sepal = iris[['SepalLengthCm', 'SepalWidthCm', 'Species']]

train_s,test_s=train_test_split(sepal,test_size=0.3,random_state=0)

train_x_s=train_s[['SepalWidthCm','SepalLengthCm']]

train_y_s=train_s.Species

test_x_s=test_s[['SepalWidthCm','SepalLengthCm']]

test_y_s=test_s.Species

model=svm.SVC()

model.fit(train_x_p,train_y_p)

prediction=model.predict(test_x_p)

print('The accuracy of the SVM using Petals is: {0}'.format(metrics.accuracy_score(prediction,test_y_p)))

model.fit(train_x_s,train_y_s)

prediction=model.predict(test_x_s)

print('The accuracy of the SVM using Sepal is: {0}'.format(metrics.accuracy_score(prediction,test_y_s)))

The accuracy of the SVM using Petals is: 0.9777777777777777

The accuracy of the SVM using Sepal is: 0.8

model = LogisticRegression()

model.fit(train_x_p, train_y_p)

prediction = model.predict(test_x_p)

print('The accuracy of the Logistic Regression using Petals is: {0}'.format(metrics.accuracy_score(prediction,test_y_p)))

model.fit(train_x_s, train_y_s)

prediction = model.predict(test_x_s)

print('The accuracy of the Logistic Regression using Sepals is: {0}'.format(metrics.accuracy_score(prediction,test_y_s)))

The accuracy of the Logistic Regression using Petals is: 0.6888888888888889

The accuracy of the Logistic Regression using Sepals is: 0.6444444444444445

model=DecisionTreeClassifier()

model.fit(train_x_p, train_y_p)

prediction = model.predict(test_x_p)

print('The accuracy of the Decision Tree using Petals is: {0}'.format(metrics.accuracy_score(prediction,test_y_p)))

model.fit(train_x_s, train_y_s)

prediction = model.predict(test_x_s)

print('The accuracy of the Decision Tree using Sepals is: {0}'.format(metrics.accuracy_score(prediction,test_y_s)))

The accuracy of the Decision Tree using Petals is: 0.9555555555555556

The accuracy of the Decision Tree using Sepals is: 0.6666666666666666

model=KNeighborsClassifier(n_neighbors=3)

model.fit(train_x_p, train_y_p)

prediction = model.predict(test_x_p)

print('The accuracy of the KNN using Petals is: {0}'.format(metrics.accuracy_score(prediction,test_y_p)))

model.fit(train_x_s, train_y_s)

prediction = model.predict(test_x_s)

print('The accuracy of the KNN using Sepals is: {0}'.format(metrics.accuracy_score(prediction,test_y_s)))

The accuracy of the KNN using Petals is: 0.9777777777777777

The accuracy of the KNN using Sepals is: 0.7333333333333333

从中不难看出,使用花瓣的尺寸来训练数据较花萼更准确。正如在探索性分析的热图中所看到的那样,花萼的宽度和长度之间的相关性非常低,而花瓣的宽度和长度之间的相关性非常高。

python分析鸢尾花数据_鸢尾花(iris)数据集分析相关推荐

  1. sklearn基础篇(三)-- 鸢尾花(iris)数据集分析和分类

    后面对Sklearn的学习主要以<Python机器学习基础教程>和<机器学习实战基于scikit-learn和tensorflow>,两本互为补充进行学习,下面是开篇的学习内容 ...

  2. python爬虫tableau数据分析_完美!Python爬招聘数据,Tableau做可视化分析

    原标题:完美!Python爬招聘数据,Tableau做可视化分析 交流群预热好久的可视化交互大屏来啦 1.项目背景 随着科技的飞速发展,数据呈现爆发式的增长,任何人都摆脱不了与数据打交道,社会对于&q ...

  3. 用python玩转数据测试答案_MOOC_用Python玩转数据_测试答案

    利息.据测股息.红利所得以每次收入额为应纳税所得额.A:错B:对 利息保障倍数中,试答利息支出:试答A:不包括财务费用中的利息支出B:包括计入固定资产成本的资本化利息C:包括财务费用中的利息支出D:不 ...

  4. 用python玩转数据第一周答案_用Python玩转数据_答案

    用Python玩转数据_答案 答案: 更多相关问题 求由参数方程所确定的函数y=y(x)的二阶导数 已知数列的通项公式,则取最小值时=,此时=. (本小题满分10分)已知是等差数列,其中](1)求的通 ...

  5. 用python玩转数据第四周答案_用Python玩转数据_答案公众号

    用Python玩转数据_答案公众号 更多相关问题 隧道式一次发酵设备投资很少().隔音符号一般加在哪些字母开头的拼音上?隧道洞口工程包括石方开挖.洞口防护与排水工程.洞门建筑的制作.安装.明洞工程.( ...

  6. 鸢尾花(iris)数据集分析

    原文链接:https://www.jianshu.com/p/52b86c774b0b Iris 鸢尾花数据集是一个经典数据集,在统计学习和机器学习领域都经常被用作示例.数据集内包含 3 类共 150 ...

  7. python鸢尾花散点图_鸢尾花数据散点图

    鸢尾花分为三类:山鸢尾.变色鸢尾和维吉尼亚鸢尾 iris为鸢尾花数据集 import warnings # 引用warnings模块 warnings.filterwarnings('ignore') ...

  8. Python数据可视化实战——iris数据集可视化

    首先,这个Python数据可视化实战是在Iris数据集上完成的.所使用的是Python 3环境下的jupyter notebook. 实战中我们需要用到的库包括:pandas , matplotlib ...

  9. python读取游戏数据_用Python抓取并分析了1982场英雄联盟数据,教你开局前预测游戏对局胜负!...

    英雄联盟想必大多数读者不会陌生,这是一款来自拳头,由腾讯代理的大型网络游戏,现在一进网吧,你就能发现一大片玩英雄联盟的人.在2017年中国战队无缘鸟巢的世界总决赛后,一大片人选择了弃游,只是终究没躲过 ...

最新文章

  1. Paper之DL之BP:《Understanding the difficulty of training deep feedforward neural networks》
  2. 订单数据持久化和验证相关解决方案
  3. python简单实践作业答案_python入门实践四:爬取牛客网面试专项练习题及答案
  4. [转载] gamma函数stiriling公式_数学笔记|特殊函数(1):Gamma函数
  5. ckplayer快速入门
  6. 如何找回被删除的文件
  7. iOS音频采集技术解读:如何实现男女变声的音效?
  8. CocosCreator downlevelIteration 允许迭代器进行迭代
  9. 销售型呼叫中心系统特点
  10. idea中创建scala的worksheet第五章
  11. ThinkPHP学生作业管理系统
  12. Leetcode DAY6: 有效的字母异位词 and 两个数组的交集 and 快乐数 and 两数之和
  13. 【知识图谱】知识图谱数据构建的“硬骨头”,阿里工程师如何拿下?深度学习在知识图谱构建中的应用。
  14. 【STM32】HAL库 SPI DMA UART驱动开发
  15. 如何看待 Java 大厂 P6+ 这一岗位能力要求?
  16. 商务洽谈(谈判)步骤及技巧
  17. Android高级控件----AdapterView与Adapter详解
  18. 关闭wps2019的屏保功能
  19. voip(语音电话)
  20. 【考研英语语法】状语从句精讲

热门文章

  1. 联想E4430 蓝屏代码0x0000007B
  2. 关闭ipad和iphone应用图标自动添加阴影和反光等视觉效果
  3. 集算器入门之安装与基本使用
  4. Flink DataStream中join
  5. sumo添加车辆_SUMO仿真快速入门系列三:产生车辆移动模型
  6. 1-时间复杂度空间复杂度
  7. foobar2000 iOS使用,并连接PC的歌曲进行播放
  8. Halcon二值化函数汇总解析
  9. 使用 amMap 轻松添加互动效果的 JavaScript 地图
  10. 上海天旦网络携CrossFlow最新网络性能管理产品参加2012中国国际金融展