python鸢尾花数据集分析
时间: 2023-09-25 22:16:21 浏览: 165
鸢尾花数据集是很经典的机器学习数据集之一,包含了150个样本,每个样本包含4个特征,分别是花萼长度、花萼宽度、花瓣长度和花瓣宽度,以及它所属的3个品种之一:Setosa,Versicolour和Virginica。
下面是一个简单的Python程序,用于加载鸢尾花数据集,并进行基本的数据分析和可视化。
```python
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
# 加载鸢尾花数据集
iris = load_iris()
# 转换为数据框
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['target'] = iris.target
df['class'] = df['target'].apply(lambda x: iris.target_names[x])
# 查看前5行
print(df.head())
# 统计每个类别的样本数
print(df['class'].value_counts())
# 绘制花萼长度和花萼宽度的散点图
sns.scatterplot(x='sepal length (cm)', y='sepal width (cm)', hue='class', data=df)
plt.show()
# 绘制花瓣长度和花瓣宽度的散点图
sns.scatterplot(x='petal length (cm)', y='petal width (cm)', hue='class', data=df)
plt.show()
# 绘制特征之间的相关性热图
sns.heatmap(df.corr(), annot=True, cmap='coolwarm')
plt.show()
```
输出结果:
```
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target class
0 5.1 3.5 1.4 0.2 0 setosa
1 4.9 3.0 1.4 0.2 0 setosa
2 4.7 3.2 1.3 0.2 0 setosa
3 4.6 3.1 1.5 0.2 0 setosa
4 5.0 3.6 1.4 0.2 0 setosa
virginica 50
versicolor 50
setosa 50
Name: class, dtype: int64
```
![image-20210908153949472](https://gitee.com/ffzs/picture_go/raw/master/img/image-20210908153949472.png)
![image-20210908154000403](https://gitee.com/ffzs/picture_go/raw/master/img/image-20210908154000403.png)
![image-20210908154039227](https://gitee.com/ffzs/picture_go/raw/master/img/image-20210908154039227.png)
阅读全文