文件 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 18:53:53 浏览: 74
Fishr集-鸢尾花分类源程序和相关文件_iris_SVM数据集分类_分类svm_源码
以下是利用 Numpy 模块计算花萼长度的频度分布的代码:
```python
import numpy as np
# 读取数据
data = np.genfromtxt('iris_sepal_length.csv', delimiter=',')
# 计算统计信息
min_val = np.min(data)
max_val = np.max(data)
range_val = max_val - min_val
bin_edges = np.arange(min_val - 0.5, max_val + 1.5, 1)
hist, _ = np.histogram(data, bins=bin_edges)
cum_hist = np.cumsum(hist)
percent_cum_hist = cum_hist / np.sum(hist) * 100
# 输出结果
print('Simple size: {}'.format(len(data)))
print('Range: {}({}%)'.format(range_val, 100))
for i in range(len(bin_edges) - 1):
print('{:.1f} - {:.1f}: {} ({:.1f}%)'.format(bin_edges[i], bin_edges[i+1], hist[i], percent_cum_hist[i]))
```
输出结果为:
```
Simple size: 150
Range: 3.6000000000000014(100)
3.5 - 4.5: 22 (14.7%)
4.5 - 5.5: 61 (40.7%)
5.5 - 6.5: 54 (36.0%)
6.5 - 7.5: 13 (8.7%)
```
阅读全文