背景信息:我有一个数据框“ 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


然后通过DataFrame将归一化的百分比转换为Series.unstack

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中的列的箱?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55604710/

10-09 17:10