Kaggle competition: Costa Rican Household Poverty Level Prediction
<SOS> metric: MacroFScore Kaggle data source: costa-rican-household-poverty-level-prediction<groupby>
import pandas as pd
import numpy as np
from scipy import stats
%%time
bur_bal_f=bur_bal.groupby(['SK_ID_BUREAU','STATUS'] ).aggregate({'STATUS':['count'],'MONTHS_BALANCE':['max','min']})
 bur_bal_f.reset_index(inplace=True)
 bur_bal_f.columns=['SK_ID_BUREAU','STATUS','STATUS_count','MONTHS_BALANCE_max','MONTHS_BALANCE_min']<categorify>
import matplotlib.pyplot as plt
import seaborn as sns
dummy=pd.get_dummies(bur_bal_f['STATUS'],prefix='DUM_STATUS' )<concatenate>
sns.set_palette(palette='icefire', n_colors=10)
bur_bal_ff=pd.concat([bur_bal_f.drop(['STATUS'],axis=1),dummy],axis=1 )<feature_engineering>
density = sns.color_palette(palette= 'RdYlGn', n_colors= 10 )
%%time
dummy_col=[x for x in bur_bal_ff.columns if 'DUM_' in x]
for col in dummy_col:
bur_bal_ff[col]=bur_bal_ff.apply(lambda x: x.STATUS_count if x[col]==1 else 0,axis=1 )<drop_column>
in_train = pd.read_csv('.. /input/train.csv')
 in_test = pd.read_csv('.. /input/test.csv' )
bur_bal_ff.drop('STATUS_count',axis=1,inplace=True )<define_variables>
print('Unique households in training set:', in_train['idhogar'].nunique())
 print('Unique households in training set:', in_test['idhogar'].nunique() )
bur_bal_cols=[x for x in list(bur_bal_ff.columns)if x not in ['SK_ID_BUREAU']]
bur_bal_agg={}
bur_bal_name=['SK_ID_BUREAU']
for col in bur_bal_cols:
if 'DUM_' in col:
bur_bal_agg[col]=['sum']
bur_bal_name.append(col)
 elif '_max' in col:
bur_bal_agg[col]=['max']
bur_bal_name.append(col)
 elif '_min' in col:
bur_bal_agg[col]=['min']
bur_bal_name.append(col)
 else:
bur_bal_agg[col]=['sum','mean']
bur_bal_name.append(col+'_'+'sum')
 bur_bal_name.append(col+'_'+'mean' )<groupby>
in_train['Id'].duplicated().sum()
in_test['Id'].duplicated().sum()
%%time
bur_bal_fg=bur_bal_ff.groupby('SK_ID_BUREAU' ).aggregate(bur_bal_agg)
 bur_bal_fg.reset_index(inplace=True)
 bur_bal_fg.columns=bur_bal_name<drop_column>
df = pd.concat([in_train, in_test], axis = 0, sort=False, ignore_index= True)
 df.shape
del bur_bal,bur_bal_f,bur_bal_ff
gc.collect()<merge>
df.isnull().sum() [df.isnull().sum() > 0]
bur_combi=bur_f.join(bur_bal_fg.set_index('SK_ID_BUREAU'),on='SK_ID_BUREAU',lsuffix='_BU', rsuffix='_BUB' )<drop_column>
df[['tipovivi1', 'tipovivi2', 'tipovivi3', 'tipovivi4', 'tipovivi5']][df['v2a1'].isnull() ].sum()
del bur_bal_fg
gc.collect()<define_variables>
df[(df['tipovivi4'] == 1)&(df['v2a1'] > 0)]
bur_combi_cols=[x for x in list(bur_combi.columns)if x not in ['SK_ID_CURR','SK_ID_BUREAU']]
bur_combi_agg={}
bur_combi_name=['SK_ID_CURR','SK_ID_BUREAU']
for col in bur_combi_cols:
if 'DUM_' in col:
bur_combi_agg[col]=['sum']
bur_combi_name.append(col+'_'+'sum')
 elif 'AMT_' in col:
bur_combi_agg[col]=['sum','mean','max','min','var','std']
bur_combi_name.append(col+'_'+'sum')
 bur_combi_name.append(col+'_'+'mean')
 bur_combi_name.append(col+'_'+'max')
 bur_combi_name.append(col+'_'+'min')
 bur_combi_name.append(col+'_'+'var')
 bur_combi_name.append(col+'_'+'std')
 elif 'CNT_' in col:
bur_combi_agg[col]=['sum','max','min','count']
bur_combi_name.append(col+'_'+'sum')
 bur_combi_name.append(col+'_'+'max')
 bur_combi_name.append(col+'_'+'min')
 bur_combi_name.append(col+'_'+'count')
 elif 'DAYS_' in col:
bur_combi_agg[col]=['sum','max','min']
bur_combi_name.append(col+'_'+'sum')
 bur_combi_name.append(col+'_'+'max')
 bur_combi_name.append(col+'_'+'min')
 elif 'CALC_' in col:
bur_combi_agg[col]=['mean']
bur_combi_name.append(col+'_'+'mean')
 else:
bur_combi_agg[col]=['sum']
bur_combi_name.append(col+'_'+'sum')
<groupby>
df[(df['tipovivi5'] == 1)&(df['v2a1'] > 0)]
%%time
bur_combi_f=bur_combi.groupby(['SK_ID_CURR','SK_ID_BUREAU'] ).aggregate(bur_combi_agg)
 bur_combi_f.reset_index(inplace=True)
 bur_combi_f.columns=bur_combi_name<define_variables>
df['v2a1'].fillna(0, inplace = True )
bur_combi_cols=list(bur_combi_f.columns)
 bur_combi_agg={}
bur_combi_name=['SK_ID_CURR']
for col in bur_combi_cols:
if 'SK_ID_CURR'==col:
bur_combi_agg[col]=['count']
bur_combi_name.append('SK_ID_BUREAU_count')
 elif '_sum'==col:
bur_combi_agg[col]=['sum']
bur_combi_name.append(col)
 elif '_mean' in col:
bur_combi_agg[col]=['mean']
bur_combi_name.append(col)
 elif '_max' in col:
bur_combi_agg[col]=['max']
bur_combi_name.append(col)
 elif '_min' in col:
bur_combi_agg[col]=['min']
bur_combi_name.append(col)
 elif '_count' in col:
bur_combi_agg[col]=['sum']
bur_combi_name.append(col)
 elif '_var' in col:
bur_combi_agg[col]=['mean']
bur_combi_name.append(col)
 elif '_std' in col:
bur_combi_agg[col]=['mean']
bur_combi_name.append(col)
 else:
bur_combi_agg[col]=['sum']
bur_combi_name.append(col )<groupby>
df['v18q'][df['v18q1'].isnull() ].value_counts(dropna=False )
%%time
bur_combi_fg=bur_combi_f.groupby(['SK_ID_CURR'] ).aggregate(bur_combi_agg)
 bur_combi_fg.reset_index(inplace=True)
 bur_combi_fg.columns=bur_combi_name<drop_column>
df['v18q1'].fillna(0, inplace = True )
bur_combi_fg.drop(['DUM_CREDIT_TYPE_Car_loan_sum', 'DUM_CREDIT_CURRENCY_currency_2_sum', 'AMT_CREDIT_SUM_OVERDUE_var'],axis=1,inplace=True )<merge>
df['rez_esc'].fillna(0, inplace = True)
 df['Schooling_age'] =(( df['age'] >= 7)&(df['age'] <= 19)) *1
df_final=df_final.join(bur_combi_fg.set_index('SK_ID_CURR'),on='SK_ID_CURR',lsuffix='_AP', rsuffix='_BU' )<drop_column>
df.loc[13069,'rez_esc'] = 0
del bur_combi,bur_combi_f,bur_combi_fg
gc.collect()<load_from_csv>
df[['age', 'escolari', 'meaneduc', 'idhogar', 'parentesco1']][df['meaneduc'].isnull() ].sort_values(by = 'idhogar' )
%%time
prev_appl=pd.read_csv(path1+'previous_application.csv')
 print('previous_application set reading complete...' )<categorify>
for household in no_meanedu:
print(household, df['meaneduc'][df['idhogar'] == household].values )
prev_appl_encoding={'NAME_YIELD_GROUP':{'middle':3,'low_action':1,'high':4,'low_normal':1,'XNA':0},
'WEEKDAY_APPR_PROCESS_START':{'MONDAY':1,'TUESDAY':2,'WEDNESDAY':3,'THURSDAY':4,'FRIDAY':5,'SATURDAY':6,'SUNDAY':7},
'FLAG_LAST_APPL_PER_CONTRACT':{'Y':1,'N':0}}
prev_appl.replace(prev_appl_encoding,inplace=True )<categorify>
mean_educ = {}
for household in no_meanedu:
mean_educ[household] = df['escolari'][(df['idhogar'] == household)&(df['age'] >= 18)].mean()
%%time
days_col=[x for x in prev_appl.columns if 'DAYS_' in x]
for col in days_col:
prev_appl.replace(365243,np.nan,inplace= True )<filter>
df[['escolari', 'age']][df['idhogar'] == 'c49af2e64']
prev_appl_fs[prev_appl_fs.Data_type=='object']<categorify>
for household in no_meanedu:
df.loc[df['idhogar'] == household, 'meaneduc'] = mean_educ[household]
%%time
prev_appl_cf=list(prev_appl_fs[prev_appl_fs.Data_type=='object'].index)
 for col in prev_appl_cf:
prev_appl[col]=prev_appl[col].apply(lambda x: str(x ).replace(" ","_"))
print('previous application categorical features',len(prev_appl_cf))
prev_appl_cf.remove('NAME_CONTRACT_STATUS')
 prev_appl_cat=pd.DataFrame()
for col in prev_appl_cf:
dummy=pd.get_dummies(prev_appl[col],prefix='DUM_'+col)
 prev_appl_cat=pd.concat([prev_appl_cat,dummy],axis=1)
 display(prev_appl_cat.head() )<drop_column>
df['meaneduc'].fillna(0, inplace = True )
prev_appl_cat.drop(['DUM_NAME_CASH_LOAN_PURPOSE_XAP', 'DUM_PRODUCT_COMBINATION_nan'],axis=1,inplace=True )<define_variables>
df['SQBmeaned'] = df['meaneduc'] **2
print('Newly created Dummy features:',len(prev_appl_cat.columns))<concatenate>
ind_indicators = ['Id', 'escolari', 'rez_esc', 'Schooling_age', 'dis', 'male', 'female',
'estadocivil1', 'estadocivil2', 'estadocivil3', 'estadocivil4',
'estadocivil5', 'estadocivil6', 'estadocivil7', 'parentesco1',
'parentesco2', 'parentesco3', 'parentesco4', 'parentesco5', 'parentesco6',
'parentesco7', 'parentesco8', 'parentesco9', 'parentesco10', 'parentesco11',
'parentesco12', 'instlevel1', 'instlevel2', 'instlevel3', 'instlevel4', 'instlevel5',
'instlevel6', 'instlevel7', 'instlevel8', 'instlevel9', 'age', 'SQBescolari', 'SQBage', 'agesq']
prev_appl_f=pd.concat([prev_appl.drop(prev_appl_cf,axis=1),prev_appl_cat],axis=1)
<drop_column>
hh_indicators = [col for col in df.columns if col not in ind_indicators]
del prev_appl,prev_appl_cat,prev_appl_fs
gc.collect()<feature_engineering>
inconsistant_col = {}
for col in ['Target']:
consistant_value = df[df[col].notnull() ].groupby('idhogar')[col].apply(lambda x: x.nunique() == 1)
 if(consistant_value == False ).sum() > 0:
print('{} Inconsistant value for {} within household'.format(( consistant_value == False ).sum() , col))
inconsistant_col[col] = consistant_value[consistant_value == False]
%%time
prev_appl_f['CALC_PERC_ANNUITY']=prev_appl_f['AMT_ANNUITY']/prev_appl_f['AMT_CREDIT']
prev_appl_f['CALC_PERC_AMT_APPLICATION']=prev_appl_f['AMT_APPLICATION']/prev_appl_f['AMT_CREDIT']
prev_appl_f['CALC_PERC_DOWN_PAYMENT']=prev_appl_f['AMT_DOWN_PAYMENT']/prev_appl_f['AMT_CREDIT']
prev_appl_f['CALC_PERC_APPL_DOWN_PAYMENT']=prev_appl_f['AMT_DOWN_PAYMENT']/prev_appl_f['AMT_APPLICATION']
prev_appl_f['CALC_PERC_GOODS_PRICE']=prev_appl_f['AMT_GOODS_PRICE']/prev_appl_f['AMT_CREDIT']
prev_appl_f['CALC_FLAG_PRIVILEGED_CUSTOMER']=(prev_appl_f['RATE_INTEREST_PRIVILEGED']>0 ).astype(int)
 prev_appl_f['CALC_TERMINATED']=(prev_appl_f['DAYS_TERMINATION']<0 ).astype(int)
 prev_appl_f['CALC_AMT_ANNUITY_PER_PAY']=prev_appl_f['AMT_ANNUITY']/prev_appl_f['CNT_PAYMENT']
prev_appl_f['CALC_AMT_APPLICATION_PER_PAY']=prev_appl_f['AMT_APPLICATION']/prev_appl_f['CNT_PAYMENT']
prev_appl_f['CALC_AMT_DOWN_PAYMENT_PER_PAY']=prev_appl_f['AMT_DOWN_PAYMENT']/prev_appl_f['CNT_PAYMENT']
prev_appl_f['CALC_AMT_APPL_DOWN_PAYMENT_PER_PAY']=prev_appl_f['AMT_DOWN_PAYMENT']/prev_appl_f['CNT_PAYMENT']
prev_appl_f['CALC_AMT_GOODS_PRICE_PER_PAY']=prev_appl_f['AMT_GOODS_PRICE']/prev_appl_f['CNT_PAYMENT']
prev_appl_f['CALC_AMT_CREDIT_APP_DIFF']=prev_appl_f['AMT_CREDIT']-prev_appl_f['AMT_APPLICATION']
prev_appl_f['CALC_LAST_DUE_AFTER_APPL']=(prev_appl_f['DAYS_LAST_DUE_1ST_VERSION']>0 ).astype(int )<define_variables>
for household in inconsistant_col['Target'].index:
target_val_for_parentesco1 = df['Target'][(df['idhogar'] == household)&(df['parentesco1'] == 1)]
if target_val_for_parentesco1.notnull().any() :
df.loc[df['idhogar'] == household, 'Target'] = target_val_for_parentesco1.values
prev_cols=[x for x in list(prev_appl_f.columns)if x not in ['SK_ID_CURR','SK_ID_PREV','NAME_CONTRACT_STATUS']]
prev_agg={}
prev_name=['SK_ID_CURR','SK_ID_PREV','NAME_CONTRACT_STATUS']
for col in prev_cols:
if 'DUM_' in col:
prev_agg[col]=['sum','mean','std']
prev_name.append(col+'_'+'sum')
 prev_name.append(col+'_'+'mean')
 prev_name.append(col+'_'+'std')
 elif 'AMT_' in col:
prev_agg[col]=['sum','mean','max','min','var','std']
prev_name.append(col+'_'+'sum')
 prev_name.append(col+'_'+'mean')
 prev_name.append(col+'_'+'max')
 prev_name.append(col+'_'+'min')
 prev_name.append(col+'_'+'var')
 prev_name.append(col+'_'+'std')
 elif 'CNT_' in col:
prev_agg[col]=['sum','max','min','size','count']
prev_name.append(col+'_'+'sum')
 prev_name.append(col+'_'+'max')
 prev_name.append(col+'_'+'min')
 prev_name.append(col+'_'+'size')
 prev_name.append(col+'_'+'count')
 elif 'DAYS_' in col:
prev_agg[col]=['sum','max','min']
prev_name.append(col+'_'+'sum')
 prev_name.append(col+'_'+'max')
 prev_name.append(col+'_'+'min')
 elif 'CALC_FLAG_' in col:
prev_agg[col]=['sum']
prev_name.append(col+'_'+'sum')
 elif 'CALC_' in col:
prev_agg[col]=['mean']
prev_name.append(col+'_'+'mean')
 else:
prev_agg[col]=['sum','mean']
prev_name.append(col+'_'+'sum')
 prev_name.append(col+'_'+'mean')
<groupby>
df['hacdor'][df['Target'].notnull() ].value_counts()
%%time
prev_appl_ff=prev_appl_f.groupby(['SK_ID_CURR','SK_ID_PREV','NAME_CONTRACT_STATUS'] ).aggregate(prev_agg)
 prev_appl_ff.reset_index(inplace=True)
 prev_appl_ff.columns=prev_name<drop_column>
df['Target'][df['hacdor'] == 1].value_counts(normalize = True )
del prev_appl_f
gc.collect()<categorify>
df['hacapo'][df['Target'].notnull() ].value_counts()
dummy=pd.get_dummies(prev_appl_ff['NAME_CONTRACT_STATUS'],prefix='DUM_'+'NAME_CONTRACT_STATUS' )<concatenate>
df['Target'][(df['Target'].notnull())&(df['v18q'] == 1)].value_counts() /df['Target'].value_counts()
prev_appl_fg=pd.concat([prev_appl_ff.iloc[:,:1],dummy,prev_appl_ff.iloc[:,3:]],axis=1 )<drop_column>
df[df['parentesco1'] == 1].pivot_table(values = 'idhogar', columns = 'v18q1', index = 'Target', aggfunc = 'count' )
del prev_appl_ff,dummy
gc.collect()<define_variables>
df['hogar_mid'] = df['hogar_adul'] -df['hogar_mayor']
prev_cols=list(prev_appl_fg.columns)
 prev_agg={}
prev_name=['SK_ID_CURR']
for col in prev_cols:
if 'SK_ID_CURR'==col:
prev_agg[col]=['count']
prev_name.append('SK_ID_PREV_count')
 elif '_sum' in col:
prev_agg[col]=['sum']
prev_name.append(col)
 elif '_mean' in col:
prev_agg[col]=['mean']
prev_name.append(col)
 elif '_max' in col:
prev_agg[col]=['max']
prev_name.append(col)
 elif '_min' in col:
prev_agg[col]=['min']
prev_name.append(col)
 elif '_var' in col:
prev_agg[col]=['mean']
prev_name.append(col)
 elif '_std' in col:
prev_agg[col]=['mean']
prev_name.append(col)
 elif '_size' in col:
prev_agg[col]=['mean']
prev_name.append(col)
 elif '_count' in col:
prev_agg[col]=['sum']
prev_name.append(col)
 else:
prev_agg[col]=['sum']
prev_name.append(col)
<groupby>
df['Target'][df['computer'] == 1].value_counts() /df['Target'].value_counts()
%%time
prev_appl_fgg=prev_appl_fg.groupby(['SK_ID_CURR'] ).aggregate(prev_agg)
 prev_appl_fgg.reset_index(inplace=True)
 prev_appl_fgg.columns=prev_name<drop_column>
df['Target'][df['television'] == 1].value_counts() /df['Target'].value_counts()
del prev_appl_fg
gc.collect()<merge>
df['Target'][df['mobilephone'] == 1].value_counts() /df['Target'].value_counts()
df_final=df_final.join(prev_appl_fgg.set_index('SK_ID_CURR'),on='SK_ID_CURR',lsuffix='_AP', rsuffix='_PV' )<drop_column>
df['dependencyR'] =(df['hogar_nin'] + df['hogar_mayor'])/ df['hogar_mid']
del prev_appl_fgg
gc.collect()<load_from_csv>
df['dependencyR'].value_counts()
%%time
inst_pay=pd.read_csv(path1+'installments_payments.csv')
 print('installments_payments set reading complete...' )<feature_engineering>
df['dependencyR'].fillna(0, inplace = True)
 df['dependencyR'].replace(np.inf, 9, inplace = True )
for col in inst_pay.columns:
if 'DAYS_' in col:
inst_pay[col]=inst_pay[col].abs()<feature_engineering>
df['phonespp'] = df['qmobilephone'] / df['tamviv']
df['tabletspp'] = df['v18q1'] / df['tamviv']
df['roomspp'] = df['rooms'] / df['tamviv']
df['rentpp'] = df['v2a1'] / df['tamviv']
%%time
inst_pay['CALC_DAYS_LATE_PAYMENT']=inst_pay['DAYS_ENTRY_PAYMENT']-inst_pay['DAYS_INSTALMENT']
inst_pay['CALC_PERC_LESS_PAYMENT']=inst_pay['AMT_PAYMENT']/inst_pay['AMT_INSTALMENT']
inst_pay['CALC_PERC_LESS_PAYMENT'].replace(np.inf,0,inplace=True)
 inst_pay['CALC_DIFF_INSTALMENT']=inst_pay['AMT_INSTALMENT']-inst_pay['AMT_PAYMENT']
inst_pay['CALC_PERC_DIFF_INSTALMENT']=np.abs(inst_pay['CALC_DIFF_INSTALMENT'])/inst_pay['AMT_INSTALMENT']
inst_pay['CALC_PERC_DIFF_INSTALMENT'].replace(np.inf,0,inplace=True)
 inst_pay['CALC_INSTAL_PAID_LATE'] =(inst_pay['CALC_DAYS_LATE_PAYMENT'] > 0 ).astype(int)
 inst_pay['CALC_OVERPAID']=(inst_pay['CALC_DIFF_INSTALMENT'] < 0 ).astype(int )<define_variables>
df['median_schooling'] = df['escolari'].groupby(df['idhogar'] ).transform('median')
 df['max_schooling'] = df['escolari'].groupby(df['idhogar'] ).transform('max' )
inst_pay_cols=[x for x in list(inst_pay.columns)if x not in ['SK_ID_CURR','SK_ID_PREV']]
inst_pay_agg={}
inst_pay_name=['SK_ID_CURR','SK_ID_PREV']
for col in inst_pay_cols:
if 'NUM_INSTALMENT_VERSION'==col:
inst_pay_agg[col]=[cnt_unique]
inst_pay_name.append(col+'_'+'unique')
 elif 'NUM_INSTALMENT_NUMBER'==col:
inst_pay_agg[col]=['max','count']
inst_pay_name.append(col+'_'+'max')
 inst_pay_name.append(col+'_'+'count')
 elif 'AMT_' in col:
inst_pay_agg[col]=['sum','mean','max','min','var','std']
inst_pay_name.append(col+'_'+'sum')
 inst_pay_name.append(col+'_'+'mean')
 inst_pay_name.append(col+'_'+'max')
 inst_pay_name.append(col+'_'+'min')
 inst_pay_name.append(col+'_'+'var')
 inst_pay_name.append(col+'_'+'std')
 elif 'CALC_DAYS_' in col:
inst_pay_agg[col]=['sum']
inst_pay_name.append(col+'_'+'sum')
 elif 'DAYS_' in col:
inst_pay_agg[col]=['sum','max','min']
inst_pay_name.append(col+'_'+'sum')
 inst_pay_name.append(col+'_'+'max')
 inst_pay_name.append(col+'_'+'min')
 else:
inst_pay_agg[col]=['mean']
inst_pay_name.append(col+'_'+'mean' )<groupby>
df['eduForHeadofHH'] = 0
df.loc[(df['parentesco1']== 1), 'eduForHeadofHH'] = df['escolari']
%%time
inst_pay_f=inst_pay.groupby(['SK_ID_CURR','SK_ID_PREV'] ).aggregate(inst_pay_agg)
 inst_pay_f.reset_index(inplace=True)
 inst_pay_f.columns=inst_pay_name<feature_engineering>
df['eduForHeadofHH'] = df['eduForHeadofHH'].groupby(df['idhogar'] ).transform('max' )
inst_pay_f['CALC_NUM_INSTALMENT_MISSED']=inst_pay_f['NUM_INSTALMENT_NUMBER_max']-inst_pay_f['NUM_INSTALMENT_NUMBER_count']<define_variables>
df['SecondaryEduLess'] =(( df[['instlevel1','instlevel2', 'instlevel3', 'instlevel4']] == 1 ).any(axis = 1)&(df['age'] > 19)) *1
df['SecondaryEduMore'] =(( df[['instlevel5','instlevel6', 'instlevel7', 'instlevel8', 'instlevel9']] == 1 ).any(axis = 1)&(df['age'] > 19)) *1
inst_pay_cols=[x for x in list(inst_pay_f.columns)if x not in ['SK_ID_PREV']]
inst_pay_agg={}
inst_pay_name=['SK_ID_CURR']
for col in inst_pay_cols:
if 'SK_ID_CURR'==col:
inst_pay_agg[col]=['count']
inst_pay_name.append('SK_ID_PREV_count')
 elif '_unique' in col:
inst_pay_agg[col]=['sum']
inst_pay_name.append(col)
 elif '_mean' in col:
inst_pay_agg[col]=['mean']
inst_pay_name.append(col)
 elif '_max' in col:
inst_pay_agg[col]=['max']
inst_pay_name.append(col)
 elif '_min' in col:
inst_pay_agg[col]=['min']
inst_pay_name.append(col)
 elif '_count' in col:
inst_pay_agg[col]=['sum']
inst_pay_name.append(col)
 else:
inst_pay_agg[col]=['sum']
inst_pay_name.append(col )<groupby>
df['SecondaryEduMore'].value_counts()
%%time
inst_pay_f.drop(['SK_ID_PREV'],axis=1,inplace=True)
 inst_pay_fg=inst_pay_f.groupby(['SK_ID_CURR'] ).aggregate(inst_pay_agg)
 inst_pay_fg.reset_index(inplace=True)
 inst_pay_fg.columns=inst_pay_name<feature_engineering>
df['MembersWithSecEdu'] = df['SecondaryEduMore'].groupby(df['idhogar'] ).transform('sum')
 df['MembersWithPrimEdu'] = df['SecondaryEduLess'].groupby(df['idhogar'] ).transform('sum' )
inst_pay_fg['CALC_CNT_INSTALMENT_VERSION_CHG']=inst_pay_fg['NUM_INSTALMENT_VERSION_unique']-inst_pay_fg['SK_ID_PREV_count']<drop_column>
df['Educated_Ratio'] =(df['MembersWithSecEdu']/df['MembersWithPrimEdu'])
 df['Educated_Ratio'].replace(np.inf, -1, inplace = True)
 df['Educated_Ratio'].fillna(value = 0, inplace = True )
inst_pay_fg.drop(['DAYS_ENTRY_PAYMENT_max', 'AMT_PAYMENT_var', 'DAYS_INSTALMENT_sum'],axis=1,inplace=True )<merge>
df['access_to_tech'] =(( df['v18q1'] >= 1)&(df['qmobilephone'] >= 1)&(df['computer'] >= 0)&(df['television'] >= 0)) *1
df_final=df_final.join(inst_pay_fg.set_index('SK_ID_CURR'),on='SK_ID_CURR',lsuffix='_AP', rsuffix='_INP' )<drop_column>
df['marital_status'] =(((df['estadocivil3'] ==1)|(df['estadocivil4'] == 1)) &(df['parentesco1'] == 1)) *1
df['marital_status'] = df['marital_status'].groupby(df['idhogar'] ).transform('max' )
del inst_pay,inst_pay_f,inst_pay_fg
gc.collect()<load_from_csv>
df['FemaleHousehold'] =(( df['male'] == 0)&(df['parentesco1'] == 1)) *1
df['FemaleHousehold'] = df['FemaleHousehold'].groupby(df['idhogar'] ).transform('max' )
%%time
pos_cash=pd.read_csv(path1+'POS_CASH_balance.csv')
 print('POS_CASH_balance set reading complete...' )<feature_engineering>
df['bedrooms_to_rooms'] = df['bedrooms']/df['rooms']
df['rent_to_rooms'] = df['v2a1']/df['rooms']
df['rooms_to_tamviv'] = df['rooms']/df['tamviv']
df['v2a1_to_r4t3'] = df['v2a1']/df['r4t3']
pos_cash['MONTHS_BALANCE']=pos_cash['MONTHS_BALANCE'].abs()<feature_engineering>
df['female_to_males'] = df['r4m2']/df['r4h2']
df['less12_to_adult'] = df['r4t1']/df['hogar_mid']
df['less12_to_older'] = df['r4t1']/df['r4t2']
pos_cash['CALC_PERC_REMAINING_INSTAL']=pos_cash['CNT_INSTALMENT_FUTURE']/pos_cash['CNT_INSTALMENT']
pos_cash['CALC_CNT_REMAINING_INSTAL']=pos_cash['CNT_INSTALMENT']-pos_cash['CNT_INSTALMENT_FUTURE']
pos_cash['CALC_DAYS_WITHOUT_TOLERANCE']=pos_cash['SK_DPD']-pos_cash['SK_DPD_DEF']<categorify>
( df== np.inf ).sum() [(df== np.inf ).sum() >0]
pos_cash['NAME_CONTRACT_STATUS']=pos_cash['NAME_CONTRACT_STATUS'].apply(lambda x: str(x ).replace(" ","_"))
dummy=pd.get_dummies(pos_cash['NAME_CONTRACT_STATUS'],prefix='DUM_NAME_CONTRACT_STATUS' )<concatenate>
df['less12_to_adult'].fillna(0, inplace = True)
 df['less12_to_adult'].replace(np.inf, -1, inplace = True)
 df['female_to_males'].fillna(0, inplace = True)
 df['female_to_males'].replace(np.inf, -1, inplace = True )
pos_cash_f=pd.concat([pos_cash.drop(['NAME_CONTRACT_STATUS'],axis=1),dummy],axis=1 )<define_variables>
df['NoMalesInHH'] =(df['r4h2'] == 0)*1
df['NoFemalesInHH'] =(df['r4m2'] == 0)*1
pos_cash_cols=[x for x in list(pos_cash_f.columns)if x not in ['SK_ID_CURR']]
pos_cash_agg={}
pos_cash_name=['SK_ID_CURR','SK_ID_PREV']
for col in pos_cash_cols:
if 'SK_ID_PREV'==col:
pos_cash_agg[col]=['count']
pos_cash_name.append(col+'_'+'count')
 elif 'MONTHS_BALANCE'==col:
pos_cash_agg[col]=['max','min','count']
pos_cash_name.append(col+'_'+'max')
 pos_cash_name.append(col+'_'+'min')
 pos_cash_name.append(col+'_'+'count')
 elif 'DUM_' in col:
pos_cash_agg[col]=['sum','mean','max','min']
pos_cash_name.append(col+'_'+'sum')
 pos_cash_name.append(col+'_'+'mean')
 pos_cash_name.append(col+'_'+'max')
 pos_cash_name.append(col+'_'+'min')
 elif 'CNT_' in col:
pos_cash_agg[col]=['max','min','sum','count']
pos_cash_name.append(col+'_'+'max')
 pos_cash_name.append(col+'_'+'min')
 pos_cash_name.append(col+'_'+'sum')
 pos_cash_name.append(col+'_'+'count')
 else:
pos_cash_agg[col]=['sum','mean']
pos_cash_name.append(col+'_'+'sum')
 pos_cash_name.append(col+'_'+'mean' )<groupby>
from sklearn.tree import DecisionTreeClassifier, ExtraTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import cross_val_score, cross_val_predict
from sklearn.model_selection import train_test_split, KFold
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import StandardScaler
%%time
pos_cash_ff=pos_cash_f.groupby(['SK_ID_CURR','SK_ID_PREV'] ).aggregate(pos_cash_agg)
 pos_cash_ff.reset_index(inplace=True)
 pos_cash_ff.columns=pos_cash_name<define_variables>
from sklearn.metrics import f1_score, confusion_matrix
pos_cash_cols=[x for x in list(pos_cash_ff.columns)if x not in ['SK_ID_CURR','SK_ID_PREV']]
pos_cash_agg={}
pos_cash_name=['SK_ID_CURR']
for col in pos_cash_cols:
if '_sum'==col:
pos_cash_agg[col]=['sum']
pos_cash_name.append(col)
 elif '_mean' in col:
pos_cash_agg[col]=['mean']
pos_cash_name.append(col)
 elif '_max' in col:
pos_cash_agg[col]=['max']
pos_cash_name.append(col)
 elif '_min' in col:
pos_cash_agg[col]=['min']
pos_cash_name.append(col)
 elif '_count' in col:
pos_cash_agg[col]=['sum']
pos_cash_name.append(col)
 else:
pos_cash_agg[col]=['sum']
pos_cash_name.append(col )<groupby>
( df == np.inf ).sum() [(df == np.inf ).sum() > 0]
%%time
pos_cash_fg=pos_cash_ff.groupby(['SK_ID_CURR'] ).aggregate(pos_cash_agg)
 pos_cash_fg.reset_index(inplace=True)
 pos_cash_fg.columns=pos_cash_name<merge>
def get_train_test(df):
train = df[df['Target'].notnull() ].copy()
test = df[df['Target'].isnull() ].copy()
test.drop(columns = 'Target', inplace = True)
 return train, test
df_final=df_final.join(pos_cash_fg.set_index('SK_ID_CURR'),on='SK_ID_CURR',lsuffix='_AP', rsuffix='_PC' )<drop_column>
train, test = get_train_test(df )
del pos_cash_fg,pos_cash,pos_cash_f,pos_cash_ff
gc.collect()<load_from_csv>
columnstoDrop = ['idhogar','Id', 'edjefe', 'edjefa','dependency']
train.drop(columns= columnstoDrop, inplace = True)
 test.drop(columns= columnstoDrop, inplace = True)
 X = train.drop(columns= 'Target')
 y = train['Target']
X_col = X.columns
std_scl = StandardScaler()
X= std_scl.fit_transform(X)
 test= std_scl.transform(test )
%%time
cc_bal=pd.read_csv(path1+'credit_card_balance.csv')
 print('credit_card_balance set reading complete...' )<feature_engineering>
kfold = KFold(n_splits= 5, shuffle= True )
cc_bal['MONTHS_BALANCE']=cc_bal['MONTHS_BALANCE'].abs()<feature_engineering>
X_train, X_test, y_train, y_test= train_test_split(X,y )
cc_bal['CALC_PERC_BALANCE']=cc_bal['AMT_BALANCE']/cc_bal['AMT_CREDIT_LIMIT_ACTUAL']
cc_bal['CALC_PERC_DRAWINGS_ATM_CURRENT']=cc_bal['AMT_DRAWINGS_ATM_CURRENT']/cc_bal['AMT_CREDIT_LIMIT_ACTUAL']
cc_bal['CALC_PERC_DRAWINGS_CURRENT']=cc_bal['AMT_DRAWINGS_CURRENT']/cc_bal['AMT_CREDIT_LIMIT_ACTUAL']
cc_bal['CALC_PERC_DRAWINGS_OTHER_CURRENT']=cc_bal['AMT_DRAWINGS_OTHER_CURRENT']/cc_bal['AMT_CREDIT_LIMIT_ACTUAL']
cc_bal['CALC_PERC_DRAWINGS_POS_CURRENT']=cc_bal['AMT_DRAWINGS_POS_CURRENT']/cc_bal['AMT_CREDIT_LIMIT_ACTUAL']
cc_bal['CALC_PERC_INST_MIN_REGULARITY']=cc_bal['AMT_INST_MIN_REGULARITY']/cc_bal['AMT_CREDIT_LIMIT_ACTUAL']
cc_bal['CALC_PERC_PAYMENT_CURRENT']=cc_bal['AMT_PAYMENT_CURRENT']/cc_bal['AMT_CREDIT_LIMIT_ACTUAL']
cc_bal['CALC_PERC_PAYMENT_TOTAL_CURRENT']=cc_bal['AMT_PAYMENT_TOTAL_CURRENT']/cc_bal['AMT_CREDIT_LIMIT_ACTUAL']
cc_bal['CALC_PERC_RECEIVABLE_PRINCIPAL']=cc_bal['AMT_RECEIVABLE_PRINCIPAL']/cc_bal['AMT_CREDIT_LIMIT_ACTUAL']
cc_bal['CALC_PERC_RECIVABLE']=cc_bal['AMT_RECIVABLE']/cc_bal['AMT_CREDIT_LIMIT_ACTUAL']
cc_bal['CALC_DAYS_WITHOUT_TOLERANCE']=cc_bal['SK_DPD']-cc_bal['SK_DPD_DEF']
CNT_DRAWING_LIST=['CNT_DRAWINGS_ATM_CURRENT','CNT_DRAWINGS_CURRENT','CNT_DRAWINGS_OTHER_CURRENT','CNT_DRAWINGS_POS_CURRENT']
cc_bal['CALC_CNT_DRAWINGS_TOTAL']=cc_bal[CNT_DRAWING_LIST].sum(axis=1 )<categorify>
from sklearn.ensemble import RandomForestClassifier
cc_bal['NAME_CONTRACT_STATUS']=cc_bal['NAME_CONTRACT_STATUS'].apply(lambda x: str(x ).replace(" ","_"))
dummy=pd.get_dummies(cc_bal['NAME_CONTRACT_STATUS'],prefix='DUM_NAME_CONTRACT_STATUS' )<concatenate>
rf_clf = RandomForestClassifier(n_estimators= 10, class_weight= 'balanced')
 rf_clf.fit(X,y )
cc_bal_f=pd.concat([cc_bal.drop(['NAME_CONTRACT_STATUS'],axis=1),dummy],axis=1 )<define_variables>
select =rf_clf.feature_importances_ > 0.001
sum(select )
cc_bal_cols=[x for x in list(cc_bal_f.columns)if x not in ['SK_ID_CURR']]
cc_bal_agg={}
cc_bal_name=['SK_ID_CURR','SK_ID_PREV']
for col in cc_bal_cols:
if 'SK_ID_PREV'==col:
cc_bal_agg[col]=['count']
cc_bal_name.append(col+'_'+'count')
 elif 'MONTHS_BALANCE'==col:
cc_bal_agg[col]=['max','min','count']
cc_bal_name.append(col+'_'+'max')
 cc_bal_name.append(col+'_'+'min')
 cc_bal_name.append(col+'_'+'count')
 elif 'AMT_' in col:
cc_bal_agg[col]=['sum','mean','max','min','var','std']
cc_bal_name.append(col+'_'+'sum')
 cc_bal_name.append(col+'_'+'mean')
 cc_bal_name.append(col+'_'+'max')
 cc_bal_name.append(col+'_'+'min')
 cc_bal_name.append(col+'_'+'var')
 cc_bal_name.append(col+'_'+'std')
 elif 'CNT_' in col:
cc_bal_agg[col]=['max','min','sum','count']
cc_bal_name.append(col+'_'+'max')
 cc_bal_name.append(col+'_'+'min')
 cc_bal_name.append(col+'_'+'sum')
 cc_bal_name.append(col+'_'+'count')
 else:
cc_bal_agg[col]=['mean']
cc_bal_name.append(col+'_'+'mean' )<groupby>
clf = LogisticRegression(class_weight = 'balanced', solver= 'liblinear', multi_class= 'ovr' )
%%time
cc_bal_ff=cc_bal_f.groupby(['SK_ID_CURR','SK_ID_PREV'] ).aggregate(cc_bal_agg)
 cc_bal_ff.reset_index(inplace=True)
 cc_bal_ff.columns=cc_bal_name<define_variables>
cross_val_score(clf, , y, scoring = 'f1_macro', cv= kfold )
cc_bal_cols=[x for x in list(cc_bal_ff.columns)if x not in ['SK_ID_CURR','SK_ID_PREV']]
cc_bal_agg={}
cc_bal_name=['SK_ID_CURR']
for col in cc_bal_cols:
if '_sum'==col:
cc_bal_agg[col]=['sum']
cc_bal_name.append(col)
 elif '_var' in col:
cc_bal_agg[col]=['mean']
cc_bal_name.append(col)
 elif '_std' in col:
cc_bal_agg[col]=['mean']
cc_bal_name.append(col)
 elif '_mean' in col:
cc_bal_agg[col]=['mean']
cc_bal_name.append(col)
 elif '_max' in col:
cc_bal_agg[col]=['max']
cc_bal_name.append(col)
 elif '_min' in col:
cc_bal_agg[col]=['min']
cc_bal_name.append(col)
 elif '_count' in col:
cc_bal_agg[col]=['sum']
cc_bal_name.append(col)
 else:
cc_bal_agg[col]=['sum']
cc_bal_name.append(col )<groupby>
for reg in [0.001, 0.01, 0.1, 1, 10, 20, 30]:
clf = LogisticRegression(class_weight = 'balanced', C= reg, solver= 'liblinear', multi_class= 'ovr')
 f1score = cross_val_score(clf, X[:,select], y, scoring = 'f1_macro', cv=kfold ).mean()
print(f1score)
%%time
cc_bal_fg=cc_bal_ff.groupby(['SK_ID_CURR'] ).aggregate(cc_bal_agg)
 cc_bal_fg.reset_index(inplace=True)
 cc_bal_fg.columns=cc_bal_name<merge>
clf = LogisticRegression(class_weight = 'balanced', solver= 'liblinear', multi_class= 'ovr')
 pred = cross_val_predict(clf, X[:,select], y, cv=kfold ).astype(int )
df_final=df_final.join(cc_bal_fg.set_index('SK_ID_CURR'),on='SK_ID_CURR',lsuffix='_AP', rsuffix='_CB' )<drop_column>
clf = LogisticRegression(class_weight = 'balanced', solver= 'liblinear', multi_class= 'ovr', C= 20)
 clf.fit(X[:,select], y)
 prediction = clf.predict(test[:,select] ).astype(int )
del cc_bal_fg,cc_bal,cc_bal_f,cc_bal_ff
gc.collect()<drop_column>
submission = pd.DataFrame({'Id': in_test['Id'], 'idhogar': in_test['idhogar'], 'isHead': in_test['parentesco1'] , 'Target':prediction})
 submission['Target'].value_counts(normalize = True )
df_final.drop(['SK_ID_CURR'],axis=1,inplace=True )<drop_column>
submission.to_csv('./Submission_log_clf.csv', columns=['Id', 'Target'], index= False )
df_final.drop(drop_col,axis=1,inplace=True )<drop_column>
gb_clf = GradientBoostingClassifier()
del corr
gc.collect()<feature_engineering>
gb_clf.fit(X[:,select],y)
 prediction = gb_clf.predict(test[:, select] ).astype(int )
%%time
df_final['the_mean'] = df_final.mean(axis=1)
 print('the_mean calculated...')
 df_final['the_sum'] =df_final.sum(axis=1)
 print('the_sum calculated...')
 df_final['the_std'] = df_final.std(axis=1)
 print('the_std calculated...' )<prepare_x_and_y>
submission = pd.DataFrame({'Id': in_test['Id'], 'idhogar': in_test['idhogar'], 'isHead': in_test['parentesco1'] , 'Target':prediction})
 submission['Target'].value_counts(normalize = True )
X=df_final.iloc[:len(train),:]
y=train['TARGET']
test=df_final.iloc[len(train):,:]<drop_column>
submission.to_csv('./Submission_GradientBoosting_clf.csv', columns=['Id', 'Target'], index= False )
del df_final
gc.collect()<choose_model_class>
tree_clf = DecisionTreeClassifier()
%%time
model=CatBoostClassifier(iterations=1000,
learning_rate=0.05,
depth=7,
l2_leaf_reg=40,
bootstrap_type='Bernoulli',
subsample=0.7,
scale_pos_weight=5,
eval_metric='AUC',
metric_period=50,
od_type='Iter',
od_wait=45,
random_seed=17,
allow_writing_files=False)
 df_preds=pd.DataFrame()
df_preds_x=pd.DataFrame()
k=1
splits=10
avg_score=0
skf = StratifiedKFold(n_splits=splits, shuffle=True, random_state=200)
 print('
Starting KFold iterations...')
 for train_index,test_index in skf.split(X,y):
df_X=X.iloc[train_index,:]
df_y=y.iloc[train_index]
val_X=X.iloc[test_index,:]
val_y=y.iloc[test_index]
model.fit(df_X,df_y)
 col_name='cat_predsx_'+str(k)
 preds_x=pd.Series(model.predict_proba(val_X)[:,1])
 df_preds_x[col_name]=pd.Series(model.predict_proba(X)[:,1])
 acc=roc_auc_score(val_y,preds_x)
 print('Iteration:',k,' roc_auc_score:',acc)
 if k==1:
score=acc
model1=model
preds=pd.Series(model.predict_proba(test)[:,1])
 col_name='cat_preds_'+str(k)
 df_preds[col_name]=preds
else:
preds1=pd.Series(model.predict_proba(test)[:,1])
 preds=preds+preds1
col_name='cat_preds_'+str(k)
 df_preds[col_name]=preds1
if score<acc:
score=acc
model1=model
avg_score=avg_score+acc
k=k+1
print('
Best score:',score,' Avg Score:',avg_score/splits)
 preds=preds/splits<load_from_csv>
sample=pd.read_csv(path1+'sample_submission.csv' )<prepare_output>
from sklearn.ensemble import VotingClassifier
sample['TARGET']=preds<save_to_csv>
voting_clf = VotingClassifier(estimators= [('tree', tree_clf),('gradiantBoost', gb_clf),('logR', clf)], voting= 'hard' )
sample.to_csv('submission.csv',index=False )<save_to_csv>
cross_val_score(voting_clf, X[:, select], y, scoring = 'f1_macro', cv= kfold )
df_preds_x.to_csv('cat_preds_x.csv',index=False)
 df_preds.to_csv('cat_preds.csv',index=False )<feature_engineering>
prediction = cross_val_predict(voting_clf, X[:, select], y, cv=kfold)
 confusion_matrix(y, prediction )
%%time
sample_ens=sample.copy()
df_preds_x.columns=['cat_preds_1','cat_preds_2','cat_preds_3','cat_preds_4','cat_preds_5','cat_preds_6',
'cat_preds_7','cat_preds_8','cat_preds_9','cat_preds_10']
df_final_ens=pd.concat([df_preds_x,df_preds],ignore_index=True)
 df_final_ens['the_mean'] = df_final_ens.mean(axis=1)
 print('the_mean calculated...')
 df_final_ens['the_sum'] =df_final_ens.sum(axis=1)
 print('the_sum calculated...')
 df_final_ens['the_std'] = df_final_ens.std(axis=1)
 print('the_std calculated...')
 df_final_ens['the_kur'] = df_final_ens.kurtosis(axis=1)
 print('the_kur calculated...')
 X=df_final_ens.iloc[:len(train),:]
y=train['TARGET']
test=df_final_ens.iloc[len(train):,:]
model=LogisticRegression()
k=1
splits=10
avg_score=0
skf = StratifiedKFold(n_splits=splits, shuffle=True, random_state=200)
 print('
Starting KFold iterations...')
 for train_index,test_index in skf.split(X,y):
df_X=X.iloc[train_index,:]
df_y=y.iloc[train_index]
val_X=X.iloc[test_index,:]
val_y=y.iloc[test_index]
model.fit(df_X,df_y)
 preds_x=pd.Series(model.predict_proba(val_X)[:,1])
 acc=roc_auc_score(val_y,preds_x)
 print('Iteration:',k,' roc_auc_score:',acc)
 if k==1:
score=acc
model1=model
preds=pd.Series(model.predict_proba(test)[:,1])
 else:
preds=preds+pd.Series(model.predict_proba(test)[:,1])
 if score<acc:
score=acc
model1=model
avg_score=avg_score+acc
k=k+1
print('
Best score:',score,' Avg Score:',avg_score/splits)
 preds=preds/splits
sample_ens['TARGET']=preds
sample_ens.to_csv('submission_ens.csv',index=False )<load_from_csv>
voting_clf.fit(X[:, select],y)
 prediction = voting_clf.predict(test[:, select] ).astype(int )
print('Importing data...')
 data = pd.read_csv('.. /input/home-credit-default-risk/application_train.csv')
 test = pd.read_csv('.. /input/home-credit-default-risk/application_test.csv')
 prev = pd.read_csv('.. /input/home-credit-default-risk/previous_application.csv')
 buro = pd.read_csv('.. /input/home-credit-default-risk/bureau.csv')
 buro_balance = pd.read_csv('.. /input/home-credit-default-risk/bureau_balance.csv')
 credit_card = pd.read_csv('.. /input/home-credit-default-risk/credit_card_balance.csv')
 POS_CASH = pd.read_csv('.. /input/home-credit-default-risk/POS_CASH_balance.csv')
 payments = pd.read_csv('.. /input/home-credit-default-risk/installments_payments.csv')
 lgbm_submission = pd.read_csv('.. /input/home-credit-default-risk/sample_submission.csv')
 y = data['TARGET']
del data['TARGET']
categorical_features = [col for col in data.columns if data[col].dtype == 'object']
one_hot_df = pd.concat([data,test])
 one_hot_df = pd.get_dummies(one_hot_df, columns=categorical_features)
 data = one_hot_df.iloc[:data.shape[0],:]
test = one_hot_df.iloc[data.shape[0]:,]
print('Pre-processing buro_balance...')
 buro_grouped_size = buro_balance.groupby('SK_ID_BUREAU')['MONTHS_BALANCE'].size()
buro_grouped_max = buro_balance.groupby('SK_ID_BUREAU')['MONTHS_BALANCE'].max()
buro_grouped_min = buro_balance.groupby('SK_ID_BUREAU')['MONTHS_BALANCE'].min()
buro_counts = buro_balance.groupby('SK_ID_BUREAU')['STATUS'].value_counts(normalize = False)
 buro_counts_unstacked = buro_counts.unstack('STATUS')
 buro_counts_unstacked.columns = ['STATUS_0', 'STATUS_1','STATUS_2','STATUS_3','STATUS_4','STATUS_5','STATUS_C','STATUS_X',]
buro_counts_unstacked['MONTHS_COUNT'] = buro_grouped_size
buro_counts_unstacked['MONTHS_MIN'] = buro_grouped_min
buro_counts_unstacked['MONTHS_MAX'] = buro_grouped_max
buro = buro.join(buro_counts_unstacked, how='left', on='SK_ID_BUREAU')
 print('Pre-processing previous_application...')
 prev_cat_features = [pcol for pcol in prev.columns if prev[pcol].dtype == 'object']
prev = pd.get_dummies(prev, columns=prev_cat_features)
 avg_prev = prev.groupby('SK_ID_CURR' ).mean()
cnt_prev = prev[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
avg_prev['nb_app'] = cnt_prev['SK_ID_PREV']
del avg_prev['SK_ID_PREV']
print('Pre-processing buro...')
 buro_cat_features = [bcol for bcol in buro.columns if buro[bcol].dtype == 'object']
buro = pd.get_dummies(buro, columns=buro_cat_features)
 avg_buro = buro.groupby('SK_ID_CURR' ).mean()
avg_buro['buro_count'] = buro[['SK_ID_BUREAU', 'SK_ID_CURR']].groupby('SK_ID_CURR' ).count() ['SK_ID_BUREAU']
del avg_buro['SK_ID_BUREAU']
print('Pre-processing POS_CASH...')
 le = LabelEncoder()
POS_CASH['NAME_CONTRACT_STATUS'] = le.fit_transform(POS_CASH['NAME_CONTRACT_STATUS'].astype(str))
nunique_status = POS_CASH[['SK_ID_CURR', 'NAME_CONTRACT_STATUS']].groupby('SK_ID_CURR' ).nunique()
nunique_status2 = POS_CASH[['SK_ID_CURR', 'NAME_CONTRACT_STATUS']].groupby('SK_ID_CURR' ).max()
POS_CASH['NUNIQUE_STATUS'] = nunique_status['NAME_CONTRACT_STATUS']
POS_CASH['NUNIQUE_STATUS2'] = nunique_status2['NAME_CONTRACT_STATUS']
POS_CASH.drop(['SK_ID_PREV', 'NAME_CONTRACT_STATUS'], axis=1, inplace=True)
 print('Pre-processing credit_card...')
 credit_card['NAME_CONTRACT_STATUS'] = le.fit_transform(credit_card['NAME_CONTRACT_STATUS'].astype(str))
nunique_status = credit_card[['SK_ID_CURR', 'NAME_CONTRACT_STATUS']].groupby('SK_ID_CURR' ).nunique()
nunique_status2 = credit_card[['SK_ID_CURR', 'NAME_CONTRACT_STATUS']].groupby('SK_ID_CURR' ).max()
credit_card['NUNIQUE_STATUS'] = nunique_status['NAME_CONTRACT_STATUS']
credit_card['NUNIQUE_STATUS2'] = nunique_status2['NAME_CONTRACT_STATUS']
credit_card.drop(['SK_ID_PREV', 'NAME_CONTRACT_STATUS'], axis=1, inplace=True)
 print('Pre-processing payments...')
 avg_payments = payments.groupby('SK_ID_CURR' ).mean()
avg_payments2 = payments.groupby('SK_ID_CURR' ).max()
avg_payments3 = payments.groupby('SK_ID_CURR' ).min()
del avg_payments['SK_ID_PREV']
print('Joining databases...')
 data = data.merge(right=avg_prev.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_prev.reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(right=avg_buro.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_buro.reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(POS_CASH.groupby('SK_ID_CURR' ).mean().reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(POS_CASH.groupby('SK_ID_CURR' ).mean().reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(credit_card.groupby('SK_ID_CURR' ).mean().reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(credit_card.groupby('SK_ID_CURR' ).mean().reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(right=avg_payments.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_payments.reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(right=avg_payments2.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_payments2.reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(right=avg_payments3.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_payments3.reset_index() , how='left', on='SK_ID_CURR')
 print('Removing features with more than 80% missing...')
<filter>
submission = pd.DataFrame({'Id': in_test['Id'], 'idhogar': in_test['idhogar'], 'isHead': in_test['parentesco1'] , 'Target':prediction})
 submission['Target'].value_counts(normalize = True )
test = test[test.columns[data.isnull().mean() < 0.85]]
data = data[data.columns[data.isnull().mean() < 0.85]]
data.drop(columns=['FLAG_DOCUMENT_2', 'FLAG_DOCUMENT_10', 'FLAG_DOCUMENT_12', 'FLAG_DOCUMENT_13', 'FLAG_DOCUMENT_14',
'FLAG_DOCUMENT_15', 'FLAG_DOCUMENT_16', 'FLAG_DOCUMENT_17', 'FLAG_DOCUMENT_19', 'FLAG_DOCUMENT_20',
'FLAG_DOCUMENT_21'],axis=1,inplace=True )<drop_column>
submission.to_csv('./Submission_voting_clfHardVote.csv', columns=['Id', 'Target'], index= False )
test.drop(columns=['FLAG_DOCUMENT_2', 'FLAG_DOCUMENT_10', 'FLAG_DOCUMENT_12', 'FLAG_DOCUMENT_13', 'FLAG_DOCUMENT_14',
'FLAG_DOCUMENT_15', 'FLAG_DOCUMENT_16', 'FLAG_DOCUMENT_17', 'FLAG_DOCUMENT_19', 'FLAG_DOCUMENT_20',
'FLAG_DOCUMENT_21'],axis=1,inplace=True )<feature_engineering>
voting_clf = VotingClassifier(estimators= [('tree', tree_clf),('gradiantBoost', gb_clf),('logR', clf)], voting= 'soft' )
data['DAYS_EMPLOYED_ANOM'] = data["DAYS_EMPLOYED"] == 365243
data['DAYS_EMPLOYED'].replace({365243: np.nan}, inplace = True)
 test['DAYS_EMPLOYED_ANOM'] = test["DAYS_EMPLOYED"] == 365243
test['DAYS_EMPLOYED'].replace({365243: np.nan}, inplace = True )<categorify>
data['DAYS_LAST_PHONE_CHANGE'].replace(0, np.nan, inplace=True)
 test['DAYS_LAST_PHONE_CHANGE'].replace(0, np.nan, inplace=True )<feature_engineering>
voting_clf.fit(X[:, select],y)
 prediction = voting_clf.predict(test[:, select] ).astype(int )
data['DAYS_CREDIT_ENDDATE'][data['DAYS_CREDIT_ENDDATE'] < -40000] = np.nan
test['DAYS_CREDIT_ENDDATE'][test['DAYS_CREDIT_ENDDATE'] < -40000] = np.nan<feature_engineering>
submission = pd.DataFrame({'Id': in_test['Id'], 'idhogar': in_test['idhogar'], 'isHead': in_test['parentesco1'] , 'Target':prediction})
 submission['Target'].value_counts(normalize = True )
<feature_engineering><EOS> 
submission.to_csv('./Submission_voting_clfSoftVote.csv', columns=['Id', 'Target'], index= False )