Kaggle competition: Home Credit Default Risk
<SOS> metric: AUC Kaggle data source: home-credit-default-risk<predict_on_test>
warnings.simplefilter(action='ignore', category=FutureWarning)
 py.init_notebook_mode(connected=True)
 %matplotlib inline
cf.go_offline()
prob=xgb.predict_proba(test_np )<train_model>
def one_hot_encoder(df, nan_as_category = True):
original_columns = list(df.columns)
 categorical_columns = [col for col in df.columns if df[col].dtype == 'object']
df = pd.get_dummies(df, columns= categorical_columns, dummy_na= nan_as_category)
 new_columns = [c for c in df.columns if c not in original_columns]
return df, new_columns
clf = RandomForestClassifier(n_estimators=10)
 clf.fit(x_Train,y_Train[:,1])
 score(clf, x_Test,y_Test[:,1] )<train_model>
num_rows = None
nan_as_category = True
ada = AdaBoostClassifier(n_estimators=100, random_state=0)
 ada.fit(x_Train,y_Train[:,1])
 score(ada,x_Test,y_Test[:,1] )<predict_on_test>
print("Start Train Test................." )
prob3 = ada.predict_proba(test_np )<train_model>
df = pd.read_csv('.. /input/application_train.csv', nrows= num_rows)
 test_df = pd.read_csv('.. /input/application_test.csv', nrows= num_rows)
 print("Train samples: {}, test samples: {}".format(len(df), len(test_df)))
 df = df.append(test_df ).reset_index()
del test_df
gc.collect()
gbc = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0,
max_depth=1, random_state=0 ).fit(x_Train, y_Train[:,1])
 score(gbc, x_Test, y_Test[:,1])
<train_model>
for bin_feature in ['CODE_GENDER', 'FLAG_OWN_CAR', 'FLAG_OWN_REALTY']:
df[bin_feature], uniques = pd.factorize(df[bin_feature])
 df, cat_cols = one_hot_encoder(df, nan_as_category)
 df['DAYS_EMPLOYED'].replace(365243, np.nan, inplace= True )
rgl = LogisticRegression(random_state=0, solver='lbfgs',
multi_class='multinomial' ).fit(x_Train, y_Train[:,1])
 score(rgl, x_Test, y_Test[:,1] )<predict_on_test>
df['DAYS_EMPLOYED_PERC'] = df['DAYS_EMPLOYED'] / df['DAYS_BIRTH']
df['INCOME_CREDIT_PERC'] = df['AMT_INCOME_TOTAL'] / df['AMT_CREDIT']
df['INCOME_PER_PERSON'] = df['AMT_INCOME_TOTAL'] / df['CNT_FAM_MEMBERS']
df['ANNUITY_INCOME_PERC'] = df['AMT_ANNUITY'] / df['AMT_INCOME_TOTAL']
prob4 = rgl.predict_proba(test_np )<train_model>
a = df['DAYS_EMPLOYED_PERC'].tolist()
a = [x for x in a if str(x)!= 'nan']
b = df['INCOME_CREDIT_PERC'].tolist()
b = [x for x in b if str(x)!= 'nan']
c = df['INCOME_PER_PERSON'].tolist()
c = [x for x in c if str(x)!= 'nan']
d = df['ANNUITY_INCOME_PERC'].tolist()
d = [x for x in d if str(x)!= 'nan']
BC_model = BaggingClassifier().fit(x_Train, y_Train[:,1])
 score(BC_model, x_Test, y_Test[:,1] )<train_model>
print("End Train Test.................. " )
dtc = DecisionTreeClassifier(random_state=0 ).fit(x_Train, y_Train[:,1])
 score(dtc, x_Test, y_Test[:,1] )<train_model>
print("Start Bureau................ " )
etc = ExtraTreesClassifier(n_estimators=10, max_depth=None,
min_samples_split=2, random_state=0 ).fit(x_Train, y_Train[:,1])
 score(etc, x_Test, y_Test[:,1] )<train_model>
bureau = pd.read_csv('.. /input/bureau.csv', nrows = num_rows )
sgd = SGDClassifier(loss="log", penalty="elasticnet", max_iter=5 ).fit(x_Train, y_Train[:,1])
 score(sgd, x_Test, y_Test[:,1] )<prepare_output>
bb = pd.read_csv('.. /input/bureau_balance.csv', nrows = num_rows)
 bb, bb_cat = one_hot_encoder(bb, nan_as_category)
 bureau, bureau_cat = one_hot_encoder(bureau, nan_as_category )
submission = pd.DataFrame({
"Id": ids.Id,
"Expected": probs[:,1]
})
 submission.head()<save_to_csv>
bb_aggregations = {'MONTHS_BALANCE': ['min', 'max', 'size']}
for col in bb_cat:
bb_aggregations[col] = ['mean']
bb_agg = bb.groupby('SK_ID_BUREAU' ).agg(bb_aggregations)
 bb_agg.columns = pd.Index([e[0] + "_" + e[1].upper() for e in bb_agg.columns.tolist() ])
 bureau = bureau.join(bb_agg, how='left', on='SK_ID_BUREAU')
 bureau.drop(columns= 'SK_ID_BUREAU', inplace= True)
 del bb, bb_agg
gc.collect()
submission.to_csv('sampleSubmission.csv', index=False )<set_options>
num_aggregations = {
'DAYS_CREDIT': ['min', 'max', 'mean', 'var'],
'CREDIT_DAY_OVERDUE': ['max', 'mean'],
'DAYS_CREDIT_ENDDATE': ['min', 'max', 'mean'],
'AMT_CREDIT_MAX_OVERDUE': ['mean'],
'CNT_CREDIT_PROLONG': ['sum'],
'AMT_CREDIT_SUM': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_DEBT': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_OVERDUE': ['mean'],
'AMT_CREDIT_SUM_LIMIT': ['mean', 'sum'],
'DAYS_CREDIT_UPDATE': ['min', 'max', 'mean'],
'AMT_ANNUITY': ['max', 'mean'],
'MONTHS_BALANCE_MIN': ['min'],
'MONTHS_BALANCE_MAX': ['max'],
'MONTHS_BALANCE_SIZE': ['mean', 'sum']
}
plt.style.use('ggplot')
 %matplotlib inline 
<choose_model_class>
cat_aggregations = {}
for cat in bureau_cat: cat_aggregations[cat] = ['mean']
for cat in bb_cat: cat_aggregations[cat + "_MEAN"] = ['mean']
bureau_agg = bureau.groupby('SK_ID_CURR' ).agg({**num_aggregations, **cat_aggregations})
 bureau_agg.columns = pd.Index(['BURO_' + e[0] + "_" + e[1].upper() for e in bureau_agg.columns.tolist() ] )
xgb.XGBClassifier()<load_from_csv>
active = bureau[bureau['CREDIT_ACTIVE_Active'] == 1]
active_agg = active.groupby('SK_ID_CURR' ).agg(num_aggregations)
 active_agg.columns = pd.Index(['ACT_' + e[0] + "_" + e[1].upper() for e in active_agg.columns.tolist() ])
 bureau_agg = bureau_agg.join(active_agg, how='left')
 del active, active_agg
gc.collect()
closed = bureau[bureau['CREDIT_ACTIVE_Closed'] == 1]
closed_agg = closed.groupby('SK_ID_CURR' ).agg(num_aggregations)
 closed_agg.columns = pd.Index(['CLS_' + e[0] + "_" + e[1].upper() for e in closed_agg.columns.tolist() ])
 bureau_agg = bureau_agg.join(closed_agg, how='left')
 del closed, closed_agg, bureau
gc.collect()
df_train = pd.read_csv('.. /input/homework-for-students3/train.csv', index_col=0)
 df_test = pd.read_csv('.. /input/homework-for-students3/test.csv', index_col=0)
 print(len(df_test))
print(len(df_train))<load_from_csv>
print("End Bureau................ " )
gdp=pd.read_csv('.. /input/homework-for-students3/US_GDP_by_State.csv')
 zipdata=pd.read_csv('.. /input/homework-for-students3/free-zipcode-database.csv')
 drop_col = ['WorldRegion',
'Country', 'LocationText', 'Location', 'Decommisioned',
'TaxReturnsFiled', 'EstimatedPopulation', 'TotalWages', 'Notes']
zipdata=zipdata.drop(drop_col,axis=1)
 state=pd.read_csv('.. /input/homework-for-students3/statelatlong.csv')
 spi=pd.read_csv('.. /input/homework-for-students3/spi.csv')
 spi['date']=pd.to_datetime(spi['date'])
 spi= spi.set_index("date")
 spi=spi.asfreq('d', method='ffill')
 spi = spi.reset_index()<data_type_conversions>
print("Start previous_application................ " )
df_train["issue_d"]=pd.to_datetime(df_train["issue_d"])
 df_test["issue_d"]=pd.to_datetime(df_test["issue_d"])
 df_train = df_train[df_train.issue_d.dt.year >= 2015]
df_train = df_train[df_train['annual_inc'] < df_train['annual_inc'].quantile(0.999)]
df_train['IDdami']=df_train.index
df_test['IDdami']=df_test.index<data_type_conversions>
prev = pd.read_csv('.. /input/previous_application.csv', nrows = num_rows )
df_train["earliest_cr_line"]=pd.to_datetime(df_train["earliest_cr_line"])
 df_test["earliest_cr_line"]=pd.to_datetime(df_test["earliest_cr_line"])
 df_train["issue_d_unix"] = df_train["issue_d"].view('int64')// 10**9
df_test["issue_d_unix"] = df_test["issue_d"].view('int64')// 10**9
df_train["earliest_cr_line_unix"] = df_train["earliest_cr_line"].view('int64')// 10**9
df_test["earliest_cr_line_unix"] = df_test["earliest_cr_line"].view('int64')// 10**9
df_train["period"]=df_train["issue_d_unix"]-df_train["earliest_cr_line_unix"]
df_test["period"]=df_test["issue_d_unix"]-df_test["earliest_cr_line_unix"]
df_train["period"]=df_train["period"].fillna(0)
 df_test["period"]=df_test["period"].fillna(0)
 df_train['aaa']=round(df_train['loan_amnt']/df_train['installment'],5)
 df_test['aaa']=round(df_test['loan_amnt']/df_test['installment'],5)
 df_train['bbb']=round(df_train['loan_amnt']/df_train['annual_inc'],5)
 df_test['bbb']=round(df_test['loan_amnt']/df_test['annual_inc'],5)
 df_train['ddd']=round(df_train['revol_bal']/df_train['revol_util'],5)
 df_test['ddd']=round(df_test['revol_bal']/df_test['revol_util'],5)
 df_train['eee']=round(df_train['revol_bal']/df_train['total_acc'],5)
 df_test['eee']=round(df_test['revol_bal']/df_test['total_acc'],5)
 df_train['fff']=round(df_train['revol_util']/df_train['total_acc'],5)
 df_test['fff']=round(df_test['revol_util']/df_test['total_acc'],5)
 df_train['aaa_open_acc']=round(df_train['loan_amnt']/df_train['open_acc'],5)
 df_test['aaa_open_acc']=round(df_test['loan_amnt']/df_test['open_acc'],5)
<merge>
prev, cat_cols = one_hot_encoder(prev, nan_as_category= True)
 prev['DAYS_FIRST_DRAWING'].replace(365243, np.nan, inplace= True)
 prev['DAYS_FIRST_DUE'].replace(365243, np.nan, inplace= True)
 prev['DAYS_LAST_DUE_1ST_VERSION'].replace(365243, np.nan, inplace= True)
 prev['DAYS_LAST_DUE'].replace(365243, np.nan, inplace= True)
 prev['DAYS_TERMINATION'].replace(365243, np.nan, inplace= True )
df_train = df_train.reset_index()
df_test = df_test.reset_index()
kari_df_train=pd.merge(df_train, state, how='left',left_on='addr_state',right_on='State')
 kari_df_test=pd.merge(df_test, state, how='left',left_on='addr_state',right_on='State')
 df_train = kari_df_train.set_index("ID")
 df_test =kari_df_test.set_index("ID")
 df_train=df_train.drop('State',axis=1)
 df_test=df_test.drop('State',axis=1 )<merge>
prev['APP_CREDIT_PERC'] = prev['AMT_APPLICATION'] / prev['AMT_CREDIT']
df_train['dami_year']=df_train.issue_d.dt.year
df_test['dami_year']=int(2015)
 df_train = df_train.reset_index()
df_test = df_test.reset_index()
kari_df_train=pd.merge(df_train, gdp, how='left',left_on=['City','dami_year'],right_on=['State','year'])
 kari_df_test=pd.merge(df_test, gdp, how='left',left_on=['City','dami_year'],right_on=['State','year'])
 df_train = kari_df_train.set_index("ID")
 df_test =kari_df_test.set_index("ID")
 df_train=df_train.drop(['State','dami_year','year'],axis=1)
 df_test=df_test.drop(['State','dami_year','year'],axis=1 )<merge>
num_aggregations = {
'AMT_ANNUITY': ['min', 'max', 'mean'],
'AMT_APPLICATION': ['min', 'max', 'mean'],
'AMT_CREDIT': ['min', 'max', 'mean'],
'APP_CREDIT_PERC': ['min', 'max', 'mean', 'var'],
'AMT_DOWN_PAYMENT': ['min', 'max', 'mean'],
'AMT_GOODS_PRICE': ['min', 'max', 'mean'],
'HOUR_APPR_PROCESS_START': ['min', 'max', 'mean'],
'RATE_DOWN_PAYMENT': ['min', 'max', 'mean'],
'DAYS_DECISION': ['min', 'max', 'mean'],
'CNT_PAYMENT': ['mean', 'sum'],
}
df_train = df_train.reset_index()
df_test = df_test.reset_index()
kari_df_train=pd.merge(df_train, spi, how='left',left_on=['issue_d'],right_on=['date'])
 kari_df_test=pd.merge(df_test, spi, how='left',left_on=['issue_d'],right_on=['date'])
 df_train = kari_df_train.set_index("ID")
 df_test =kari_df_test.set_index("ID")
 df_train=df_train.drop(['date'],axis=1)
 df_test=df_test.drop(['date'],axis=1 )<data_type_conversions>
cat_aggregations = {}
for cat in cat_cols:
cat_aggregations[cat] = ['mean']
prev_agg = prev.groupby('SK_ID_CURR' ).agg({**num_aggregations, **cat_aggregations})
 prev_agg.columns = pd.Index(['PREV_' + e[0] + "_" + e[1].upper() for e in prev_agg.columns.tolist() ] )
zipdata["Zipcode"]=zipdata["Zipcode"].astype(str)
 zipdata["Zipcode"]=zipdata["Zipcode"].str[:3]
zipdata=zipdata[['Zipcode','State','Xaxis', 'Yaxis', 'Zaxis']]
zipdata=zipdata.groupby(['Zipcode','State'],as_index=False ).mean() 
<data_type_conversions>
approved = prev[prev['NAME_CONTRACT_STATUS_Approved'] == 1]
approved_agg = approved.groupby('SK_ID_CURR' ).agg(num_aggregations)
 approved_agg.columns = pd.Index(['APR_' + e[0] + "_" + e[1].upper() for e in approved_agg.columns.tolist() ])
 prev_agg = prev_agg.join(approved_agg, how='left')
 refused = prev[prev['NAME_CONTRACT_STATUS_Refused'] == 1]
refused_agg = refused.groupby('SK_ID_CURR' ).agg(num_aggregations)
 refused_agg.columns = pd.Index(['REF_' + e[0] + "_" + e[1].upper() for e in refused_agg.columns.tolist() ])
 prev_agg = prev_agg.join(refused_agg, how='left')
 del refused, refused_agg, approved, approved_agg, prev
gc.collect()
df_train['zip_code']=df_train['zip_code'].str[:3]
df_test['zip_code']=df_test['zip_code'].str[:3]
df_train["zip_code"]=df_train["zip_code"].astype(str)
 df_test["zip_code"]=df_test["zip_code"].astype(str)
<count_duplicates>
print("End previous_application................ " )
zipdata[zipdata.duplicated() ]<merge>
print("Start POS_CASH_balance................ " )
df_train = df_train.reset_index()
df_test = df_test.reset_index()
kari_df_train=pd.merge(df_train, zipdata, how='left',left_on=['zip_code','addr_state'],right_on=['Zipcode','State'])
 kari_df_test=pd.merge(df_test, zipdata, how='left',left_on=['zip_code','addr_state'],right_on=['Zipcode','State'])
 df_train = kari_df_train.set_index("ID")
 df_test =kari_df_test.set_index("ID")
 df_train=df_train.drop(['Zipcode','State'],axis=1)
 df_test=df_test.drop(['Zipcode','State'],axis=1 )<categorify>
pos = pd.read_csv('.. /input/POS_CASH_balance.csv', nrows = num_rows )
encoder = OrdinalEncoder()
enc_train = encoder.fit_transform(df_train['zip_code'].values)
 enc_test = encoder.transform(df_test['zip_code'].values)
 df_train = df_train.reset_index()
df_test = df_test.reset_index()
df_train['zip_code_la']=enc_train.iloc[:,0]
df_test['zip_code_la']=enc_test.iloc[:,0]
df_train = df_train.set_index("ID")
 df_test =df_test.set_index("ID" )<categorify>
pos, cat_cols = one_hot_encoder(pos, nan_as_category= True)
 aggregations = {
'MONTHS_BALANCE': ['max', 'mean', 'size'],
'SK_DPD': ['max', 'mean'],
'SK_DPD_DEF': ['max', 'mean']
}
zi_cal1='zip_code'
zi_summary1 = df_train[zi_cal1].value_counts()
df_train['zip_code_co'] = df_train[zi_cal1].map(zi_summary1)
 df_test['zip_code_co'] = df_test[zi_cal1].map(zi_summary1 )<categorify>
for cat in cat_cols:
aggregations[cat] = ['mean']
pos_agg = pos.groupby('SK_ID_CURR' ).agg(aggregations)
 pos_agg.columns = pd.Index(['POS_' + e[0] + "_" + e[1].upper() for e in pos_agg.columns.tolist() ])
 pos_agg['POS_COUNT'] = pos.groupby('SK_ID_CURR' ).size()
del pos
gc.collect()
encoder = OrdinalEncoder()
enc_train = encoder.fit_transform(df_train['addr_state'].values)
 enc_test = encoder.transform(df_test['addr_state'].values)
 df_train = df_train.reset_index()
df_test = df_test.reset_index()
df_train['addr_state_la']=enc_train.iloc[:,0]
df_test['addr_state_la']=enc_test.iloc[:,0]
df_train = df_train.set_index("ID")
 df_test =df_test.set_index("ID" )<categorify>
print("Start POS_CASH_balance................ " )
zi_cal2='addr_state'
zi_summary2 = df_train[zi_cal2].value_counts()
df_train['addr_state_co'] = df_train[zi_cal2].map(zi_summary2)
 df_test['addr_state_co'] = df_test[zi_cal2].map(zi_summary2 )<data_type_conversions>
ins = pd.read_csv('.. /input/installments_payments.csv', nrows = num_rows)
 ins, cat_cols = one_hot_encoder(ins, nan_as_category= True )
<data_type_conversions>
ins['PAYMENT_PERC'] = ins['AMT_PAYMENT'] / ins['AMT_INSTALMENT']
ins['PAYMENT_DIFF'] = ins['AMT_INSTALMENT'] - ins['AMT_PAYMENT']
ins['DPD'] = ins['DAYS_ENTRY_PAYMENT'] - ins['DAYS_INSTALMENT']
ins['DBD'] = ins['DAYS_INSTALMENT'] - ins['DAYS_ENTRY_PAYMENT']
ins['DPD'] = ins['DPD'].apply(lambda x: x if x > 0 else 0)
 ins['DBD'] = ins['DBD'].apply(lambda x: x if x > 0 else 0 )
<data_type_conversions>
aggregations = {
'NUM_INSTALMENT_VERSION': ['nunique'],
'DPD': ['max', 'mean', 'sum'],
'DBD': ['max', 'mean', 'sum'],
'PAYMENT_PERC': ['max', 'mean', 'sum', 'var'],
'PAYMENT_DIFF': ['max', 'mean', 'sum', 'var'],
'AMT_INSTALMENT': ['max', 'mean', 'sum'],
'AMT_PAYMENT': ['min', 'max', 'mean', 'sum'],
'DAYS_ENTRY_PAYMENT': ['max', 'mean', 'sum']
}
for cat in cat_cols:
aggregations[cat] = ['mean']
ins_agg = ins.groupby('SK_ID_CURR' ).agg(aggregations)
 ins_agg.columns = pd.Index(['INS_' + e[0] + "_" + e[1].upper() for e in ins_agg.columns.tolist() ])
 ins_agg['INS_COUNT'] = ins.groupby('SK_ID_CURR' ).size()
del ins
gc.collect()
<drop_column>
print("End POS_CASH_balance................ " )
df_train=df_train.drop(['issue_d','earliest_cr_line'],axis=1)
 df_test=df_test.drop(['issue_d','earliest_cr_line'],axis=1)
 drop_col=['City','acc_now_delinq']
df_train=df_train.drop(drop_col,axis=1)
 df_test=df_test.drop(drop_col,axis=1 )<data_type_conversions>
print("Start credit_card_balance................ " )
<data_type_conversions>
cc = pd.read_csv('.. /input/credit_card_balance.csv', nrows = num_rows )
<categorify>
cc, cat_cols = one_hot_encoder(cc, nan_as_category= True)
 cc.drop(columns = ['SK_ID_PREV'], inplace = True)
 cc_agg = cc.groupby('SK_ID_CURR' ).agg(['min', 'max', 'mean', 'sum', 'var'])
 cc_agg.columns = pd.Index(['CC_' + e[0] + "_" + e[1].upper() for e in cc_agg.columns.tolist() ])
 cc_agg['CC_COUNT'] = cc.groupby('SK_ID_CURR' ).size()
del cc
gc.collect()
ce_cal2='initial_list_status'
ce_summary2 = df_train[ce_cal2].value_counts()
df_train['initial_list_status'] = df_train[ce_cal2].map(ce_summary2)
 df_test['initial_list_status'] = df_test[ce_cal2].map(ce_summary2 )<categorify>
print("End credit_card_balance................ " )
ce_cal2='application_type'
ce_summary2 = df_train[ce_cal2].value_counts()
df_train['application_type'] = df_train[ce_cal2].map(ce_summary2)
 df_test['application_type'] = df_test[ce_cal2].map(ce_summary2 )<categorify>
with timer("Process bureau and bureau_balance"):
print("Bureau df shape:", bureau_agg.shape)
 df = df.join(bureau_agg, how='left',on='SK_ID_CURR')
 gc.collect()
with timer("Process previous_applications"):
print("Previous applications df shape:", prev_agg.shape)
 df = df.join(prev_agg, how='left', on='SK_ID_CURR')
 gc.collect()
with timer("Process POS-CASH balance"):
print("Pos-cash balance df shape:", pos_agg.shape)
 df = df.join(pos_agg, how='left', on='SK_ID_CURR')
 gc.collect()
with timer("Process installments payments"):
print("Installments payments df shape:", ins_agg.shape)
 df = df.join(ins_agg, how='left', on='SK_ID_CURR')
 gc.collect()
with timer("Process credit card balance"):
print("Credit card balance df shape:", cc_agg.shape)
 df = df.join(cc_agg, how='left', on='SK_ID_CURR')
 gc.collect()
del bureau_agg,prev_agg,pos_agg,ins_agg,cc_agg
gc.collect()
df_train['grade'].unique()
df_train=df_train.replace({'grade':{'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'G':7}})
 df_test=df_test.replace({'grade':{'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'G':7}})
 df_train["grade"]=df_train["grade"].astype(int)
 df_test["grade"]=df_test["grade"].astype(int )<categorify>
print("Done.;.............. ")
df_train=df_train.replace({'sub_grade':{'A1':1,'A2':2,'A3':3,'A4':4,'A5':5,
'B1':6,'B2':7,'B3':8,'B4':9,'B5':10,
'C1':11,'C2':12,'C3':13,'C4':14,'C5':15,
'D1':16,'D2':17,'D3':18,'D4':19,'D5':20,
'E1':21,'E2':22,'E3':23,'E4':24,'E5':25,
'F1':26,'F2':27,'F3':28,'F4':29,'F5':30,
'G1':31,'G2':32,'G3':33,'G4':34,'G5':35}})
 df_test=df_test.replace({'sub_grade':{'A1':1,'A2':2,'A3':3,'A4':4,'A5':5,
'B1':6,'B2':7,'B3':8,'B4':9,'B5':10,
'C1':11,'C2':12,'C3':13,'C4':14,'C5':15,
'D1':16,'D2':17,'D3':18,'D4':19,'D5':20,
'E1':21,'E2':22,'E3':23,'E4':24,'E5':25,
'F1':26,'F2':27,'F3':28,'F4':29,'F5':30,
'G1':31,'G2':32,'G3':33,'G4':34,'G5':35}})
 df_train["sub_grade"]=df_train["sub_grade"].astype(int)
 df_test["sub_grade"]=df_test["sub_grade"].astype(int )<feature_engineering>
train_df = df[df['TARGET'].notnull() ]
test_df = df[df['TARGET'].isnull() ]
in_0=df_train[df_train.loan_condition==0].installment.median()
df_train['in_0_sa'] =df_train['installment']-in_0
df_test['in_0_sa'] =df_test['installment']-in_0
lo_0=df_train[df_train.loan_condition==0].loan_amnt.median()
df_train['lo_0_sa'] =df_train['loan_amnt']-lo_0
df_test['lo_0_sa'] =df_test['loan_amnt']-lo_0
dti_0=df_train[df_train.loan_condition==0].dti.median()
df_train['dti_0_sa'] =df_train['dti']-dti_0
df_test['dti_0_sa'] =df_test['dti']-dti_0
tot_0=df_train[df_train.loan_condition==0].tot_cur_bal.median()
df_train['tot_0_sa'] =df_train['tot_cur_bal']-tot_0
df_test['tot_0_sa'] =df_test['tot_cur_bal']-tot_0
rev_0=df_train[df_train.loan_condition==0].revol_bal.median()
df_train['rev_0_sa'] =df_train['revol_bal']-rev_0
df_test['rev_0_sa'] =df_test['revol_bal']-rev_0
pe_0=df_train[df_train.loan_condition==0].period.median()
df_train['pe_0_sa'] =df_train['period']-pe_0
df_test['pe_0_sa'] =df_test['period']-pe_0
in_1=df_train[df_train.loan_condition==1].installment.median()
df_train['in_1_sa'] =df_train['installment']-in_1
df_test['in_1_sa'] =df_test['installment']-in_1
lo_1=df_train[df_train.loan_condition==1].loan_amnt.median()
df_train['lo_1_sa'] =df_train['loan_amnt']-lo_1
df_test['lo_1_sa'] =df_test['loan_amnt']-lo_1
dti_1=df_train[df_train.loan_condition==1].dti.median()
df_train['dti_1_sa'] =df_train['dti']-dti_1
df_test['dti_1_sa'] =df_test['dti']-dti_1
tot_1=df_train[df_train.loan_condition==1].tot_cur_bal.median()
df_train['tot_1_sa'] =df_train['tot_cur_bal']-tot_1
df_test['tot_1_sa'] =df_test['tot_cur_bal']-tot_1
rev_1=df_train[df_train.loan_condition==1].revol_bal.median()
df_train['rev_1_sa'] =df_train['revol_bal']-rev_1
df_test['rev_1_sa'] =df_test['revol_bal']-rev_1
pe_1=df_train[df_train.loan_condition==1].period.median()
df_train['pe_0_sa'] =df_train['period']-pe_1
df_test['pe_0_sa'] =df_test['period']-pe_1 
<data_type_conversions>
train_df = train_df.drop(['index'],axis=1)
 test_df = test_df.drop(['index','TARGET'],axis=1)
 train_df = train_df.fillna(0)
 test_df = test_df.fillna(0 )
df_train['home_ownership'].unique()
df_train=df_train.replace({'home_ownership':{'MORTGAGE':3,'RENT':2,'OWN':4,'ANY':1}})
 df_test=df_test.replace({'home_ownership':{'MORTGAGE':3,'RENT':2,'OWN':4,'ANY':1}})
 df_train["home_ownership"]=df_train["home_ownership"].astype(int)
 df_test["home_ownership"]=df_test["home_ownership"].astype(int)
 print(len(df_train.columns))
print(df_test.columns )<categorify>
label = u'TARGET'
a = list(train_df.columns)
 a.remove(label)
 labels = train_df[label]
data_only = train_df[list(a)]
col_name = data_only.columns
X_train, X_test, y_train, y_test = train_test_split(data_only, labels, test_size=0.1,random_state = 42 )
summary = df_train['purpose'].value_counts()
summary
df_train['purpose_co'] = df_train['purpose'].map(summary)
 df_test['purpose_co'] = df_test['purpose'].map(summary )<categorify>
clf_xgBoost = xgb.XGBClassifier(
 learning_rate =0.01, n_estimators=1000, max_depth=4, min_child_weight=4, subsample=0.8, colsample_bytree=0.8, objective= 'binary:logistic',
nthread=4, scale_pos_weight=2, seed=27)
 clf_xgBoost.fit(data_only,labels )
df_train=df_train.replace({'emp_length':{'< 1 year':0.5,'1 year':1,'2 years':2,'3 years':3,
'4 years':4,'5 years':5,'6 years':6,'7 years':7,
'8 years':8,'9 years':9,'10+ years':10}})
 df_test=df_test.replace({'emp_length':{'< 1 year':0.5,'1 year':1,'2 years':2,'3 years':3,
'4 years':4,'5 years':5,'6 years':6,'7 years':7,
'8 years':8,'9 years':9,'10+ years':10}})
 df_train["emp_length"].head()<feature_engineering>
pred = clf_xgBoost.predict_proba(test_df)
 test_df['TARGET'] = pred[:, 0]
<feature_engineering><EOS> 
test_df[['SK_ID_CURR', 'TARGET']].to_csv('submission_clf_xgBoost.csv', index= False )