Joh*_*mov 5 python pandas sklearn-pandas pandas-groupby
我在使用 Pandas 数据框的 python 上遇到了问题,我正在尝试使机器学习模型预测表面。我在火车数据框中有表面列,而在测试数据框中没有它。所以,我会根据火车的表面创建一些特征,比如 .
train['error_cat1'] = abs(train.groupby(train['cat1'])['surface'].transform('mean') - train.surface.mean())
Run Code Online (Sandbox Code Playgroud)
在这里,我通过“cat”特征和 suface 的平均值设置了 grouby 的值。凉爽的
现在我也必须将它添加到测试中。因此,将使用此方法将每个 groupby 的 train 值映射到测试行。
mp = {k: g['error_cat1'].tolist()[0] for k,g in train.groupby('cat1')}
test['error_cat1'] = test['cat1'].map(mp)
Run Code Online (Sandbox Code Playgroud)
所以,远没有问题。现在,我将在 groupby 中使用两列。
train['error_cat1_cat2'] = abs(train.groupby(train[['cat1','cat2']])['surface'].transform('mean') - train.surface.mean())
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将它映射到测试数据框。请你能帮我处理这个问题或给我一些其他的方法,以便我能做到。
谢谢
例如我的火车是
+------+------+-------+
| Cat1 | Cat2 | surface |
+------+------+-------+
| 1 | 3 | 10 |
+------+------+-------+
| 2 | 2 | 12 |
+------+------+-------+
| 3 | 1 | 12 |
+------+------+-------+
| 1 | 3 | 5 |
+------+------+-------+
| 2 | 2 | 10 |
+------+------+-------+
| 3 | 2 | 13 |
+------+------+-------+
Run Code Online (Sandbox Code Playgroud)
我的测试是
+------+------+
| Cat1 | Cat2 |
+------+------+
| 1 | 2 |
+------+------+
| 2 | 1 |
+------+------+
| 3 | 1 |
+------+------+
| 1 | 3 |
+------+------+
| 2 | 3 |
+------+------+
| 3 | 1 |
+------+------+
Run Code Online (Sandbox Code Playgroud)
现在我会在 cat1 和 cat2 上做一个 groupby 平均表面,例如 (cat1,cat2)=(1,3) 上的平均表面是 (10+5)/2 = 7.5
现在,我必须去测试并将这个值映射到 (cat1,cat2)=(1,3) 行上。
我希望你有我。
您可以使用
groupby().means()计算平均值reset_index()Cat1再次将索引转换Cat2为列merge(how='left', )连接两个数据框,例如数据库中的表(LEFT JOINin SQL)。。
headers = ['Cat1', 'Cat2', 'surface']
train_data = [
[1, 3, 10],
[2, 2, 12],
[3, 1, 12],
[1, 3, 5],
[2, 2, 10],
[3, 2, 13],
]
test_data = [
[1, 2],
[2, 1],
[3, 1],
[1, 3],
[2, 3],
[3, 1],
]
import pandas as pd
train = pd.DataFrame(train_data, columns=headers)
test = pd.DataFrame(test_data, columns=headers[:-1])
print('--- train ---')
print(train)
print('--- test ---')
print(test)
print('--- means ---')
means = train.groupby(['Cat1', 'Cat2']).mean()
print(means)
print('--- means (dataframe) ---')
means = means.reset_index(level=['Cat1', 'Cat2'])
print(means)
print('--- result ----')
result = pd.merge(df2, means, on=['Cat1', 'Cat2'], how='left')
print(result)
print('--- result (fillna)---')
result = result.fillna(0)
print(result)
Run Code Online (Sandbox Code Playgroud)
结果:
--- train ---
Cat1 Cat2 surface
0 1 3 10
1 2 2 12
2 3 1 12
3 1 3 5
4 2 2 10
5 3 2 13
--- test ---
Cat1 Cat2
0 1 2
1 2 1
2 3 1
3 1 3
4 2 3
5 3 1
--- means ---
surface
Cat1 Cat2
1 3 7.5
2 2 11.0
3 1 12.0
2 13.0
--- means (dataframe) ---
Cat1 Cat2 surface
0 1 3 7.5
1 2 2 11.0
2 3 1 12.0
3 3 2 13.0
--- result ----
Cat1 Cat2 surface
0 1 2 NaN
1 2 1 NaN
2 3 1 12.0
3 1 3 7.5
4 2 3 NaN
5 3 1 12.0
--- result (fillna)---
Cat1 Cat2 surface
0 1 2 0.0
1 2 1 0.0
2 3 1 12.0
3 1 3 7.5
4 2 3 0.0
5 3 1 12.0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6226 次 |
| 最近记录: |