python - 如何在 python 中自动化列的 bin?

标签 python pandas automation

背景信息:我有一个数据框“test1”,列名为“y”,其中包含原始值。我应用了一些模型,并使用“y”对列名“Yhat”进行了预测。我需要修改我的“Yhat”,因此,我将“y”和“Yhat”都存储在桶中。对于特定的“yhat”桶,有相应的“y”桶。

现在,如果我有 3 点提前预测,即“yhat”,那么我可以提供相应的“y”桶类别。例如,请参阅数据帧,即“test2”和代码。

主要查询:为了避免手动创建存储桶值,我想自动化整个过程。自动化的原因是,随着样本空间的增加,相应的桶值也会发生变化。

test1

y   Yhat
1   1
2   1
6   5
2   3
3   4
1   2
4   2
3   4
7   6
5   8

def catY(r):
    if((r['y']>=1) & (r['y']<3)):
        return 'Y_cat_1'
    elif((r['y']>=3) & (r['y']<6)):
        return 'Y_cat_2'
    elif((r['y']>=6)):
        return 'Y_cat_3'
test1['Actual_Y'] = test1.apply(catY,axis=1)

def cat(r):
    if((r['Yhat']>=1) & (r['Yhat']<3)):
        return 'Yhat_cat_1'
    elif((r['Yhat']>=3) & (r['Yhat']<6)):
        return 'Yhat_cat_2'
    elif((r['Yhat']>=6)):
        return 'Yhat_cat_3'

test1['yhat_cat'] = test1.apply(cat,axis=1)

test1.groupby('yhat_cat')['Actual_Y'].value_counts(normalize=True)
yhat_cat    Actual_Y
Yhat_cat_1  Y_cat_1    0.75
            Y_cat_2    0.25
Yhat_cat_2  Y_cat_2    0.50
            Y_cat_1    0.25
            Y_cat_3    0.25
Yhat_cat_3  Y_cat_2    0.50
            Y_cat_3    0.50

测试2

y   Yhat
1   1
2   1
6   5
2   3
3   4
1   2
4   2
3   4
7   6
5   8
    2
    5
    1

filter_method1 = lambda x: '0.75' if ( x >=1 and x <3) else '0.25' if (x >=3 and x <6) else '0' if  x >=6 else None
test2['Y_cat_1'] = test2['Yhat'].apply(filter_method1)

filter_method2 = lambda x: '0.25' if ( x >=1 and x <3) else '0.50' if (x >=3 and x <6) else '0.50' if  x >=6 else None
test2['Y_cat_2'] = test2['Yhat'].apply(filter_method2)

filter_method3 = lambda x: '0' if ( x >=1 and x <3) else '0.25' if (x >=3 and x <6) else '0.50' if  x >=6 else None
test2['Y_cat_3'] = test2['Yhat'].apply(filter_method3)



print(test2)
      y  Yhat Y_cat_1 Y_cat_2 Y_cat_3
0  1.00     1    0.75    0.25       0
1  2.00     1    0.75    0.25       0
2  6.00     5    0.25    0.50    0.25
3  2.00     3    0.25    0.50    0.25
4  3.00     4    0.25    0.50    0.25
5  1.00     2    0.75    0.25       0
6  4.00     2    0.75    0.25       0
7  3.00     4    0.25    0.50    0.25
8  7.00     6       0    0.50    0.50
9  5.00     8       0    0.50    0.50
10  nan     2    0.75    0.25       0
11  nan     5    0.25    0.50    0.25
12  nan     1    0.75    0.25       0

最佳答案

您可以使用cut :

bins = [1,3,6,np.inf]
labels1 = [f'Y_cat_{x}' for x in range(1, len(bins))]
labels2 = [f'Yhat_cat_{x}' for x in range(1, len(bins))]

test1['Actual_Y'] = pd.cut(test1['y'], bins=bins, labels=labels1, right=False)
test1['yhat_cat'] = pd.cut(test1['Yhat'], bins=bins, labels=labels2, right=False)
print (test1)
   y  Yhat Actual_Y    yhat_cat
0  1     1  Y_cat_1  Yhat_cat_1
1  2     1  Y_cat_1  Yhat_cat_1
2  6     5  Y_cat_3  Yhat_cat_2
3  2     3  Y_cat_1  Yhat_cat_2
4  3     4  Y_cat_2  Yhat_cat_2
5  1     2  Y_cat_1  Yhat_cat_1
6  4     2  Y_cat_2  Yhat_cat_1
7  3     4  Y_cat_2  Yhat_cat_2
8  7     6  Y_cat_3  Yhat_cat_3
9  5     8  Y_cat_2  Yhat_cat_3

然后通过 Series.unstack 将标准化百分比转换为 DataFrame :

df = test1.groupby('yhat_cat')['Actual_Y'].value_counts(normalize=True).unstack(fill_value=0)
print (df)
Actual_Y    Y_cat_1  Y_cat_2  Y_cat_3
yhat_cat                             
Yhat_cat_1     0.75     0.25     0.00
Yhat_cat_2     0.25     0.50     0.25
Yhat_cat_3     0.00     0.50     0.50

按列循环并通过 test2['Yhat'] 动态创建新列:

for c in df.columns:
    #https://stackoverflow.com/a/48447871
    test2[c] = df[c].values[pd.cut(test2['Yhat'], bins=bins, labels=False, right=False)]
print (test2)
      y  Yhat  Y_cat_1  Y_cat_2  Y_cat_3
0   1.0     1     0.75     0.25     0.00
1   2.0     1     0.75     0.25     0.00
2   6.0     5     0.25     0.50     0.25
3   2.0     3     0.25     0.50     0.25
4   3.0     4     0.25     0.50     0.25
5   1.0     2     0.75     0.25     0.00
6   4.0     2     0.75     0.25     0.00
7   3.0     4     0.25     0.50     0.25
8   7.0     6     0.00     0.50     0.50
9   5.0     8     0.00     0.50     0.50
10  NaN     2     0.75     0.25     0.00
11  NaN     5     0.25     0.50     0.25
12  NaN     1     0.75     0.25     0.00

关于python - 如何在 python 中自动化列的 bin?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55604710/

相关文章:

python - Perfplot 比较具有多个参数(不止一个)的多个函数的性能

python - 为什么这个 for 循环在 Python 中占用太多时间和空间

python - 在 Pandas 中操作子索引

python - 如何根据复杂的列条件集聚合重复行

java - 如何在java程序中转换sikuli脚本

python - 收货数量及表字段列表

python - 更新时 Opencv imshow() 卡住

python - Pandas:合并数据框中的重复字符串

azure - 在 Azure 中自动执行 Jupyter 笔记本

automation - 为任何程序创建自动安装程序