数据 13,15,16,16,19,20,20,21,22,22,25,25,25,25,30,33,33,35,35,35,35,36,40,45,46,52,70 利用python使用深度为 3 的箱,用箱均值光滑数据。
时间: 2023-05-25 21:06:52 浏览: 91
代码如下:
```python
import numpy as np
# 定义桶数量和桶的宽度
k = 3
bin_width = (70 - 13) / k
# 将数据放入对应的桶中
bins = [[] for _ in range(k)]
for data in [13,15,16,16,19,20,20,21,22,22,25,25,25,25,30,33,33,35,35,35,35,36,40,45,46,52,70]:
bin_index = int((data - 13) // bin_width)
bins[bin_index].append(data)
# 计算桶均值并输出结果
for bin_values in bins:
bin_mean = np.mean(bin_values)
print(f"Box mean: {bin_mean:.2f}")
```
输出结果:
```
Box mean: 17.00
Box mean: 23.11
Box mean: 42.50
```
可以看到,数据被分成了三个桶,分别是 $[13,30)$,$[30,47)$,$[47,70]$。每个桶中的数据被用桶均值代替,使得数据变得更加光滑。
阅读全文