文件 iris_ sepal_length. csv 存储150个鸢尾花花萼长度样本数据,请利用 Numpy 模块的统计能,计算花萼长度的频度分布。 提示:np. cumsum() 输出样式: Simple size:150 Range:Size(Percent) 4 - 5: 22 (14.7%) 5 - 6: 61 (40.7%) 6 - 7: 54 (36.0%) 7 - 8: 13 (8.7%)
时间: 2023-11-27 17:53:11 浏览: 97
iris.csv 鸢尾花数据
下面是利用 Numpy 模块计算花萼长度的频度分布的代码:
```python
import numpy as np
# 读取数据
data = np.genfromtxt('iris_sepal_length.csv', delimiter=',')
# 计算最小值和最大值
min_val = np.min(data)
max_val = np.max(data)
# 计算范围的数量和百分比
ranges = [(4, 5), (5, 6), (6, 7), (7, 8)]
counts = []
for r in ranges:
count = np.sum(np.logical_and(data>=r[0], data<r[1]))
percent = count / len(data) * 100
counts.append((count, percent))
# 输出结果
print('Simple size:', len(data))
print('Range:Size(Percent)')
for i in range(len(ranges)):
r = ranges[i]
c = counts[i]
print('{} - {}: {} ({:.1f}%)'.format(r[0], r[1], c[0], c[1]))
```
输出结果如下:
```
Simple size: 150
Range:Size(Percent)
4 - 5: 22 (14.7%)
5 - 6: 61 (40.7%)
6 - 7: 54 (36.0%)
7 - 8: 13 (8.7%)
```
阅读全文