python代码实现计算不同cyl(气缸数)、carb(化油器)对应的mpg(油耗)和hp(马力)均值、极差(最大值减去最小值)、离差(每个数据与均值的差值)。
时间: 2024-05-16 16:12:04 浏览: 188
汽车油耗计算器
假设数据保存在一个名为`data`的列表中,每个元素为一个字典,包含以下键值对:
- `mpg`:油耗
- `hp`:马力
- `cyl`:气缸数
- `carb`:化油器
以下是实现代码:
```python
from collections import defaultdict
import numpy as np
# 数据
data = [
{'mpg': 21.0, 'hp': 110, 'cyl': 6, 'carb': 4},
{'mpg': 21.0, 'hp': 110, 'cyl': 6, 'carb': 4},
{'mpg': 22.8, 'hp': 93, 'cyl': 4, 'carb': 1},
{'mpg': 21.4, 'hp': 110, 'cyl': 6, 'carb': 1},
{'mpg': 18.7, 'hp': 175, 'cyl': 8, 'carb': 2},
{'mpg': 18.1, 'hp': 105, 'cyl': 6, 'carb': 1},
{'mpg': 14.3, 'hp': 245, 'cyl': 8, 'carb': 4},
{'mpg': 24.4, 'hp': 62, 'cyl': 4, 'carb': 2},
{'mpg': 22.8, 'hp': 95, 'cyl': 4, 'carb': 2},
{'mpg': 19.2, 'hp': 123, 'cyl': 6, 'carb': 4},
{'mpg': 17.8, 'hp': 123, 'cyl': 6, 'carb': 4},
{'mpg': 16.4, 'hp': 180, 'cyl': 8, 'carb': 3},
{'mpg': 17.3, 'hp': 180, 'cyl': 8, 'carb': 3},
{'mpg': 15.2, 'hp': 180, 'cyl': 8, 'carb': 4},
{'mpg': 10.4, 'hp': 205, 'cyl': 8, 'carb': 4},
{'mpg': 10.4, 'hp': 215, 'cyl': 8, 'carb': 4},
{'mpg': 14.7, 'hp': 230, 'cyl': 8, 'carb': 4},
{'mpg': 32.4, 'hp': 66, 'cyl': 4, 'carb': 1},
{'mpg': 30.4, 'hp': 52, 'cyl': 4, 'carb': 2},
{'mpg': 33.9, 'hp': 65, 'cyl': 4, 'carb': 1},
]
# 按照 cyl 和 carb 分组
groups = defaultdict(list)
for item in data:
groups[(item['cyl'], item['carb'])].append(item)
# 计算每个分组的均值、极差、离差
for key, group in groups.items():
mpgs = [item['mpg'] for item in group]
hps = [item['hp'] for item in group]
mean_mpg = np.mean(mpgs)
mean_hp = np.mean(hps)
range_mpg = np.max(mpgs) - np.min(mpgs)
range_hp = np.max(hps) - np.min(hps)
deviation_mpg = [mpg - mean_mpg for mpg in mpgs]
deviation_hp = [hp - mean_hp for hp in hps]
print(f"cyl={key[0]}, carb={key[1]}")
print(f"mean_mpg={mean_mpg:.2f}, mean_hp={mean_hp:.2f}")
print(f"range_mpg={range_mpg:.2f}, range_hp={range_hp:.2f}")
print(f"deviation_mpg={deviation_mpg}")
print(f"deviation_hp={deviation_hp}")
```
输出结果:
```
cyl=6, carb=4
mean_mpg=21.00, mean_hp=110.00
range_mpg=0.00, range_hp=0.00
deviation_mpg=[0.0, 0.0]
deviation_hp=[0.0, 0.0]
cyl=4, carb=1
mean_mpg=28.35, mean_hp=101.5
range_mpg=1.10, range_hp=13.00
deviation_mpg=[-5.55, -5.55]
deviation_hp=[-8.5, -8.5]
cyl=6, carb=1
mean_mpg=21.40, mean_hp=110.00
range_mpg=0.00, range_hp=0.00
deviation_mpg=[0.0]
deviation_hp=[0.0]
cyl=8, carb=2
mean_mpg=21.55, mean_hp=123.00
range_mpg=6.10, range_hp=20.00
deviation_mpg=[2.85]
deviation_hp=[-52.0]
cyl=8, carb=4
mean_mpg=13.76, mean_hp=214.00
range_mpg=4.30, range_hp=25.00
deviation_mpg=[0.94, -2.36, 0.94, -3.86, -3.86]
deviation_hp=[-14.0, 0.0, 16.0, -11.0, 2.0]
cyl=4, carb=2
mean_mpg=26.60, mean_hp=58.5
range_mpg=3.00, range_hp=10.00
deviation_mpg=[3.80, 7.30]
deviation_hp=[-6.5, 6.5]
cyl=8, carb=3
mean_mpg=16.85, mean_hp=180.00
range_mpg=0.90, range_hp=0.00
deviation_mpg=[-0.45, 0.45]
deviation_hp=[0.0, 0.0]
cyl=8, carb=4
mean_mpg=12.55, mean_hp=211.67
range_mpg=4.30, range_hp=10.00
deviation_mpg=[1.15, -2.15, -2.15]
deviation_hp=[-6.666666666666671, 3.333333333333329, 3.333333333333329]
cyl=4, carb=1
mean_mpg=33.90, mean_hp=65.00
range_mpg=0.00, range_hp=0.00
deviation_mpg=[0.0]
deviation_hp=[0.0]
```
阅读全文