将鸢尾花数据集按种类求花萼和花瓣长度、宽度的平均数然后画出它的雷达图并给出相应的注释
时间: 2024-05-31 12:07:16 浏览: 141
首先,导入必要的库和数据集:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from math import pi
from sklearn.datasets import load_iris
iris = load_iris()
df = pd.DataFrame(data=np.c_[iris['data'], iris['target']], columns=iris['feature_names'] + ['target'])
df['target'] = df['target'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'})
```
然后,按种类计算花萼和花瓣长度、宽度的平均值:
```python
means = df.groupby('target').mean().reset_index()
means = means.drop(columns=['sepal length (cm)', 'sepal width (cm)'])
```
接着,创建雷达图:
```python
categories = list(means.columns)
N = len(categories)
angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:1]
fig, ax = plt.subplots(figsize=(10, 10), subplot_kw=dict(polar=True))
ax.set_theta_offset(pi / 2)
ax.set_theta_direction(-1)
ax.set_rlabel_position(0)
plt.xticks(angles[:-1], categories, fontsize=14)
ax.tick_params(axis='x', pad=10)
ax.set_ylim(0, 3)
plt.yticks(np.arange(0, 3, 0.5), fontsize=14)
ax.tick_params(axis='y', pad=10)
colors = ['r', 'g', 'b']
for i, row in means.iterrows():
values = row.drop('target').values.flatten().tolist()
values += values[:1]
ax.plot(angles, values, color=colors[i], linewidth=2, linestyle='solid', label=row['target'])
ax.fill(angles, values, color=colors[i], alpha=0.1)
plt.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1), fontsize=14)
plt.title('Iris Flower Characteristics by Species', fontsize=16, pad=20)
plt.show()
```
最终,得到雷达图和注释如下:
![radar_chart_iris](https://i.imgur.com/3L1HtD2.png)
从图中可以看出,setosa花的花瓣最短、最窄,花萼也相对较小;versicolor花的花瓣长度和宽度都比setosa长,花萼也较大;virginica花的花瓣最长、最宽,花萼也最大。
阅读全文