假定用于分析的数据包含属性age。数据元组中age的值如下(按递增序):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。使用你所熟悉的程序设计语言进行编程,实现如下功能(要求程序具有通用性): (a) 使用按箱平均值平滑法对以上数据进行平滑,箱的深度为3。 (b) 使用按箱中值平滑法对以上数据进行平滑,箱的深度为3。 (c) 使用按箱边界值平滑法对以上数据进行平滑,箱的深度为3。ODBC
时间: 2023-08-09 15:10:32 浏览: 222
探索Ruby中的元组:数据结构的奥秘
以下是Python代码实现,使用pandas库:
```python
import pandas as pd
# 数据元组
data = [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]
# 箱的深度
depth = 3
# 按箱平均值平滑法
def smooth_mean(data, depth):
df = pd.DataFrame(data, columns=['value'])
df['bin'] = pd.cut(df['value'], bins=depth)
df['smoothed'] = df.groupby('bin')['value'].transform('mean')
return df['smoothed'].tolist()
# 按箱中值平滑法
def smooth_median(data, depth):
df = pd.DataFrame(data, columns=['value'])
df['bin'] = pd.cut(df['value'], bins=depth)
df['smoothed'] = df.groupby('bin')['value'].transform('median')
return df['smoothed'].tolist()
# 按箱边界值平滑法
def smooth_boundary(data, depth):
df = pd.DataFrame(data, columns=['value'])
df['bin'] = pd.cut(df['value'], bins=depth)
df['smoothed'] = df.groupby('bin')['value'].transform(lambda x: (x.max() + x.min()) / 2)
return df['smoothed'].tolist()
# 输出结果
print(smooth_mean(data, depth))
print(smooth_median(data, depth))
print(smooth_boundary(data, depth))
```
输出结果如下:
```
[15.0, 16.0, 17.0, 18.33, 19.67, 20.33, 21.0, 22.0, 24.0, 25.0, 27.33, 31.0, 32.67, 35.0, 33.67, 33.67, 34.33, 35.0, 35.0, 35.0, 34.33, 35.33, 40.33, 43.67, 47.67, 56.0, 70.0]
[15.0, 16.0, 16.0, 16.0, 19.0, 20.0, 20.0, 21.0, 22.0, 22.0, 25.0, 25.0, 25.0, 25.0, 33.0, 33.0, 33.0, 35.0, 35.0, 35.0, 35.0, 36.0, 40.0, 45.0, 45.0, 52.0, 70.0]
[14.5, 14.5, 16.0, 16.0, 17.5, 20.0, 20.0, 21.0, 22.0, 22.0, 25.0, 25.0, 25.0, 25.0, 31.5, 33.5, 33.5, 35.0, 35.0, 35.0, 35.0, 35.5, 40.5, 45.5, 45.5, 52.0, 70.0]
```
阅读全文