鸢尾花(iris)数据集分析

article/2025/11/10 23:07:22

原文链接:https://www.jianshu.com/p/52b86c774b0b

 

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()
<class 'pandas.core.frame.DataFrame'>
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 00 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 11 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 22 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 22 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.Speciessepal = 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

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


 


http://chatgpt.dhexx.cn/article/6cp4ipNb.shtml

相关文章

鸢尾花(iris)数据集

鸢尾花&#xff08;iris&#xff09;数据集 更新时间&#xff1a;2021-03-21 01:01:09标签&#xff1a;数据集 鸢尾花 说明 机器学习教程 正在计划编写中&#xff0c;欢迎大家加微信 sinbam 提供意见、建议、纠错、催更。 鸢【音&#xff1a;yuān】尾花&#xff08;Iris&a…

数据分析——鸢尾花数据集

鸢尾花数据集 Iris 鸢尾花数据集内包含 3 类分别为山鸢尾&#xff08;Iris-setosa&#xff09;、变色鸢尾&#xff08;Iris-versicolor&#xff09;和维吉尼亚鸢尾&#xff08;Iris-virginica&#xff09;&#xff0c;共 150 条记录&#xff0c;每类各 50 个数据&#xff0c;每…

机器学习--鸢尾花数据集实战

Iris数据集实战 本次主要围绕Iris数据集进行一个简单的数据分析, 另外在数据的可视化部分进行了重点介绍. 环境 win8, python3.7, jupyter notebook 目录 1. 项目背景 2. 数据概览 3. 特征工程 4. 构建模型 正文 1. 项目背景 鸢尾属(拉丁学名&#xff1a;Iris L.), …

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

后面对Sklearn的学习主要以《Python机器学习基础教程》和《机器学习实战基于scikit-learn和tensorflow》&#xff0c;两本互为补充进行学习&#xff0c;下面是开篇的学习内容。 1 初识数据 iris数据集的中文名是安德森鸢尾花卉数据集&#xff0c;英文全称是Anderson’s Iris d…

机器学习——鸢尾花数据集

机器学习——鸢尾花数据集 数据集简介导入数据集可视化主成分分析 鸢尾花数据集即iris iris数据集文件&#xff1a; https://pan.baidu.com/s/1saL_4Q9PbFJluU4htAgFdQ .提取码&#xff1a;1234 数据集简介 数据集包含150个样本&#xff08;数据集的行&#xff09;数据集包含…

实验一:鸢尾花数据集分类

实验一&#xff1a;鸢尾花数据集分类 一、问题描述 利用机器学习算法构建模型&#xff0c;根据鸢尾花的花萼和花瓣大小&#xff0c;区分鸢尾花的品种。实现一个基础的三分类问题。 二、数据集分析 Iris 鸢尾花数据集内包含 3 种类别&#xff0c;分别为山鸢尾&#xff08;Iris…

C++优化之使用emplace

在C开发过程中&#xff0c;我们经常会用STL的各种容器&#xff0c;比如vector&#xff0c;map&#xff0c;set等&#xff0c;这些容器极大的方便了我们的开发。在使用这些容器的过程中&#xff0c;我们会大量用到的操作就是插入操作&#xff0c;比如vector的push_back&#xff…

C++ emplace_back

概述 在C11中&#xff0c;在引入右值的升级后&#xff0c;调用push_back变的更为高效&#xff0c;原本需要调用构造函数构造这个临时对象&#xff0c;然后调用拷贝构造函数将这个临时对象放入容器中。在C11升级后&#xff0c;只需要调用构造函数&#xff0c;然后调用移动拷贝函…

list容器下的 emplace_front() splice() 函数

目录 emplace_front()splice()作者的坑时间复杂度注意点&#xff1a;疑惑处 emplace_front() emplace中文为安置&#xff0c;那么这个函数就是安置到什么什么前面。 void emplace_front(value_type val) ;时间复杂度&#xff1a;O(1) splice() splice译为粘接&#xff0c;作用…

C++优化之使用emplace、emplace_back

在C开发过程中&#xff0c;我们经常会用STL的各种容器&#xff0c;比如vector&#xff0c;map&#xff0c;set等。在使用这些容器的过程中&#xff0c;我们会大量用到的操作就是插入操作&#xff0c;比如vector的push_back&#xff0c;map的insert&#xff0c;set的insert。这些…

emplace_back深度剖析

一&#xff0c;emplace_back和push_back 1&#xff0c;直接插入对象&#xff1a;emplace_back和push_back无区别 ①当传递已经存在的对象时&#xff0c;是无区别的 #include <iostream> #include <vector>using namespace std;/* C11 STL 容器 push/inser…

push_back和emplace_back区别

在使用vector容器时&#xff0c;往容器里添加元素时&#xff0c;有push_back和emplace_back两种方法&#xff0c;一般用得最多得是push_back&#xff0c;下面看看这两种方法得区别&#xff1a; push_back源码&#xff0c;有重载得左值和右值&#xff0c;关于左值和右值可以查看…

C++11之emplace_back

在之前的学习中&#xff0c;了解到在STL中&#xff0c;进行插入元素的时候&#xff0c;有insert和push两种选择方式&#xff0c;而在有了右值引用和移动语义的时候&#xff0c;就提出了更高效的插入方法&#xff1a;emplace_back&#xff0c;下面来介绍一下C11新特性中的emplac…

C++的emplace

一、背景 在C开发过程中&#xff0c;我们经常会用STL的各种容器&#xff0c;比如vector&#xff0c;map&#xff0c;set等&#xff0c;这些容器极大的方便了我们的开发。在使用这些容器的过程中&#xff0c;我们会大量用到的操作就是插入操作&#xff0c;比如vector的push_bac…

C++ emplace_back用法介绍

C 11对容器的push_back, push_front, insert 增加了新的用法&#xff0c;与之对应的是emplace_back&#xff0c;emplace_front, emplace. 它们的作用是在操作容器时可以调用对应类型的构造数&#xff0c;例如下面的代码&#xff1a; #include <iostream> #include <v…

C++ STL中的 emplace

英文释义&#xff08;以前还真的很少用到这个单词&#xff0c;但是经常在键入empty()函数的时候冒出来&#xff09;&#xff1a; emplace 英 [ɪmpleɪs] 美 [ɪmpleɪs] v. 放列&#xff0c;安置&#xff0c;安放; 相对于insert、push、push_back系列先构造临时变量再复制…

stl之emplace函数的使用

c11新标准引入了三个新成员-------emplace_front,emplace和emplace_back,这些操作构造而不是拷贝元素&#xff0c;因此相比push_back等函数能更好地避免内存的拷贝与移动&#xff0c;使容器插入元素的性能得到进一步提升。这些操作分别对应push_front,insert和push_back&#x…

数字分解算法的优化

http://bbs.csdn.net/topics/90040267 以上是讨论的论坛 下面是一个算法&#xff1a; //数字为n&#xff0c;开始分解第k个数字void decompose(int n,int k){int i,j;//j用来表示数字是否分解完毕for(jn;j>1;j--){a[k]j;if(jn){for(int temp1;temp<k;temp)cout<<…

整数分解(java)

public class demo4 {public static void main(String[] args) {System.out.println("请输入一个数&#xff1a;");Scanner in new Scanner(System.in);int number in.nextInt();int result 0;do {int digit number % 10;result result * 10 digit;System.out.…

C语言程序——分解三位整数的各位数字

文章目录 前言一、分解三位整数二、程序实例1.程序代码2.运行结果3.结果分析 三、拓展应用总结 前言 程序设计中用到的整型数据和实际中的整数一样&#xff0c;也分为个位、十位和百位。 一、分解三位整数 取余运算可以得到数据的个位数。因此对于实际分解过程进行模拟&#…