分箱合并的标准

数据如下图所示,当bad_rate为0时,与最小的非0的bad_rate行合并。即home_ownership 取NONE和取MORTGAGE的情况。

 分箱合并的标准_第1张图片

regroup = regroup.sort_values(by='bad_rate',ascending=False)# 先进行排序
regroup.index = range(regroup.shape[0])#重新调整序号。这个方法有意思,免去index_reset.
col_regroup = [[i] for i in regroup['home_ownership']] # 每个取值
del_index = []
for i in range(regroup.shape[0]-1):
    col_regroup[i+1] = col_regroup[i] + col_regroup[i+1]
    del_index.append(i)
    if regroup['bad_rate'][i+1] > 0:
       break
    col_regroup2 = [col_regroup[i] for i in range(len(col_regroup)) if i not in del_index]
 newGroup = {}
 for i in range(len(col_regroup2)):
     for g2 in col_regroup2[i]:
        newGroup[g2] = 'Bin '+str(i) #为每个组分类创造条件
 merge_bin_dict['home_ownership'] = newGroup
 newVar = 'home_ownership' + '_Bin'
 trainData[newVar] = trainData[col].map(newGroup)# 注意这种方式
 var_bin_list.append(newVar)

 

你可能感兴趣的:(python,学习笔记)