import sklearn
import numpy as np
import pandas as pd
import scipy
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
model_data = pd.read_csv('model_data.csv',index_col=0)
model_data.head()
|
SeriousDlqin2yrs |
RevolvingUtilizationOfUnsecuredLines |
age |
NumberOfTime30-59DaysPastDueNotWorse |
DebtRatio |
MonthlyIncome |
NumberOfOpenCreditLinesAndLoans |
NumberOfTimes90DaysLate |
NumberRealEstateLoansOrLines |
NumberOfTime60-89DaysPastDueNotWorse |
NumberOfDependents |
0 |
0 |
0.015404 |
53.000000 |
0.000000 |
0.121802 |
4728.0 |
5.000000 |
0.0 |
0.000000 |
0.0 |
0.000000 |
1 |
0 |
0.168311 |
63.000000 |
0.000000 |
0.141964 |
1119.0 |
5.000000 |
0.0 |
0.000000 |
0.0 |
0.000000 |
2 |
1 |
1.063570 |
39.716057 |
1.141971 |
0.417663 |
3500.0 |
5.141971 |
1.0 |
0.141971 |
2.0 |
3.716057 |
3 |
0 |
0.088684 |
73.000000 |
0.000000 |
0.522822 |
5301.0 |
11.000000 |
0.0 |
2.000000 |
0.0 |
0.000000 |
4 |
1 |
0.622999 |
53.636002 |
0.000000 |
0.423650 |
13000.0 |
9.819992 |
0.0 |
2.000000 |
0.0 |
0.181999 |
auto_col_name = ['RevolvingUtilizationOfUnsecuredLines','age','DebtRatio','MonthlyIncome','NumberOfOpenCreditLinesAndLoans']
def get_bins(data,col_name,max_bin_num,min_bin_num=2,target_col_name='SeriousDlqin2yrs',pic=True):
try:
bin_result,updown = pd.qcut(data[col_name],q=max_bin_num,retbins=True)
except:
max_bin_num -= 1
bin_result,updown = pd.qcut(data[col_name],q=max_bin_num,retbins=True)
bin_target = pd.DataFrame(bin_result)
bin_target['target'] = model_data['SeriousDlqin2yrs']
bin_target_count = bin_target.groupby(col_name)['target'].value_counts().unstack().fillna(0)
target0_cnt = sum(bin_target_count[0])
target1_cnt = sum(bin_target_count[1])
box_info = [*zip(updown[:-1],updown[1:],bin_target_count[0],bin_target_count[1])]
while sum(0 in box_info[i][2:] for i in range(len(box_info))):
comb_index = [int(0 in box_info