Kaggle competition: Home Credit Default Risk
<SOS> metric: AUC Kaggle data source: home-credit-default-risk<compute_test_metric>
%matplotlib inline
plt.style.use('ggplot')
 seed =420
pd.options.display.max_rows = 100
intervalo_valores(df_train[index] )<define_variables>
nrows = 100000
path = '.. /input/'
app_train = pd.read_csv(path+'application_train.csv', nrows= nrows)
 app_test = pd.read_csv(path+'application_test.csv', nrows= None)
 bureau_balance = pd.read_csv(path+'bureau_balance.csv', nrows=nrows)
 bureau = pd.read_csv(path+'bureau.csv', nrows=nrows)
 installments_payments = pd.read_csv(path+ 'installments_payments.csv', nrows= nrows)
 POS_CASH_balance = pd.read_csv(path+ 'POS_CASH_balance.csv', nrows= nrows)
 credit_card_balance = pd.read_csv(path+'credit_card_balance.csv',nrows=nrows)
 previous_application = pd.read_csv(path+ 'previous_application.csv', nrows= nrows)
 gc.collect()
test_index = app_test['SK_ID_CURR']
index = 'Weather Normalized Site Electricity(kWh)'<feature_engineering>
def basic_details(df):

 k = pd.DataFrame()
k['missing_value'] = df.isnull().sum()
k['%missing_value'] = round(df.isnull().sum() *100/df.shape[0],2)
 k['dtypes'] = df.dtypes
k['N unique'] = df.nunique()
return k
df_train[index] = df_train[index].str.replace('Not Available', '0')
 df_test[index] = df_test[index].str.replace('Not Available', '0' )<data_type_conversions>
def missing_value_fill(df ,columns, mean_or_mode='mode'):

 for i in columns:
if(df[i].isnull().sum() >0)and(mean_or_mode =='mode'):
df[i].fillna(df[i].mode() [0], inplace=True)
 elif(df[i].isnull().sum() >0)and(mean_or_mode =='mean'):
df[i].fillna(df[i].mean() , inplace=True )
df_train[index] = df_train[index].astype('float64')
 df_test[index] = df_test[index].astype('float64' )<compute_test_metric>
def replace_XNA_XAP(df):
"Replace XNA,XAP"
df.replace(to_replace = {'XNA':np.nan,'XAP':np.nan},inplace=True,value= None)
 return df
intervalo_valores(df_train[index] )<define_variables>
def one_hot_encoding(df,columns,nan_as_category = True):

 print('*'*5,'One hot encoding of categorical variable','*'*5)
 print('Original shape',df.shape)
 original_columns = df.columns
df = pd.get_dummies(df, columns= columns,drop_first=True,dummy_na=nan_as_category)
 new_columns = [i for i in df.columns if i not in original_columns]
print('After OHE',df.shape)
 return df,new_columns
index = 'Total GHG Emissions(Metric Tons CO2e)'<feature_engineering>
def descriptive_stat_feat(df,columns):

 print('*'*5,'Descriptive statistics feature','*'*5)
 print('Before',df.shape)
 mean = df[columns].mean()
median = df[columns].median()
Q1 = np.percentile(df[columns], 25, axis=0)
 Q3 = np.percentile(df[columns], 75, axis=0)
 for i,j in enumerate(columns):
df['mean_'+j] =(df[j] < mean[i] ).astype('int8')
 df['median_'+j] =(df[j] > median[i] ).astype('int8')
 df['Q1'+j] =(df[j] < Q1[i] ).astype('int8')
 df['Q3'+j] =(df[j] > Q3[i] ).astype('int8')
 print('After ',df.shape )
df_train[index] = df_train[index].str.replace('Not Available', '0')
 df_test[index] = df_test[index].str.replace('Not Available', '0' )<data_type_conversions>
def binary_encoding(df,columns):

 print('*'*5,'Binary encoding','*'*5)
 lb = LabelBinarizer()
print('Original shape:',df.shape)
 original_col = df.columns
for i in columns:
if df[i].nunique() >2:
result = lb.fit_transform(df[i].fillna(df[i].mode() [0],axis=0))
col = ['BIN_'+ str(i)+'_'+str(c)for c in lb.classes_]
result1 = pd.DataFrame(result, columns=col)
 df = df.join(result1)
 print('After:',df.shape)
 new_col = [c for c in df.columns if c not in original_col]
return df, new_col
df_train[index] = df_train[index].astype('float64')
 df_test[index] = df_test[index].astype('float64' )<compute_test_metric>
test_index = app_test['SK_ID_CURR']
app_train_col_drop = []
intervalo_valores(df_train[index] )<define_variables>
print('Count
',app_train['TARGET'].value_counts())
 print('%
',app_train['TARGET'].value_counts() *100/app_train.shape[0] )
index = 'Direct GHG Emissions(Metric Tons CO2e)'<feature_engineering>
app_train[['NAME_CONTRACT_TYPE', 'CODE_GENDER','FLAG_OWN_CAR', 'FLAG_OWN_REALTY']] = app_train[
['NAME_CONTRACT_TYPE', 'CODE_GENDER','FLAG_OWN_CAR', 'FLAG_OWN_REALTY']].astype('object')
 app_test[['NAME_CONTRACT_TYPE', 'CODE_GENDER','FLAG_OWN_CAR', 'FLAG_OWN_REALTY']] = app_test[
['NAME_CONTRACT_TYPE', 'CODE_GENDER','FLAG_OWN_CAR', 'FLAG_OWN_REALTY']].astype('object')
df_train[index] = df_train[index].str.replace('Not Available', '0')
 df_test[index] = df_test[index].str.replace('Not Available', '0' )<data_type_conversions>
tmp = app_train['CNT_CHILDREN'].value_counts().to_frame()
tmp['%'] =(app_train['CNT_CHILDREN'].value_counts() *100 / app_train.shape[0])
 tmp
df_train[index] = df_train[index].astype('float64')
 df_test[index] = df_test[index].astype('float64' )<compute_test_metric>
app_train['CNT_CHILDREN'] = app_train['CNT_CHILDREN'].astype('object')
 app_test['CNT_CHILDREN'] = app_test['CNT_CHILDREN'].astype('object' )
intervalo_valores(df_train[index] )<define_variables>
print('Default',app_train[app_train['AMT_INCOME_TOTAL'] >0.2e8]['AMT_INCOME_TOTAL'])
 app_train['AMT_INCOME_TOTAL'] = np.log(app_train['AMT_INCOME_TOTAL'])
 app_test['AMT_INCOME_TOTAL'] = np.log(app_test['AMT_INCOME_TOTAL'] )
index = 'Indirect GHG Emissions(Metric Tons CO2e)'<feature_engineering>
app_train['AMT_CREDIT'] = np.sqrt(app_train['AMT_CREDIT'])
 app_test['AMT_CREDIT'] = np.sqrt(app_test['AMT_CREDIT'] )
df_train[index] = df_train[index].str.replace('Not Available', '0')
 df_test[index] = df_test[index].str.replace('Not Available', '0' )<data_type_conversions>
app_train['AMT_ANNUITY'] = np.log(app_train['AMT_ANNUITY'])
 app_test['AMT_ANNUITY'] = np.log(app_test['AMT_ANNUITY'] )
df_train[index] = df_train[index].astype('float64')
 df_test[index] = df_test[index].astype('float64' )<compute_test_metric>
app_train[['NAME_TYPE_SUITE', 'NAME_INCOME_TYPE', 'NAME_EDUCATION_TYPE', 'NAME_FAMILY_STATUS',
'NAME_HOUSING_TYPE']] = app_train[['NAME_TYPE_SUITE', 'NAME_INCOME_TYPE', 'NAME_EDUCATION_TYPE',
'NAME_FAMILY_STATUS', 'NAME_HOUSING_TYPE']].astype('object')
 app_test[['NAME_TYPE_SUITE', 'NAME_INCOME_TYPE', 'NAME_EDUCATION_TYPE', 'NAME_FAMILY_STATUS',
'NAME_HOUSING_TYPE']] = app_test[['NAME_TYPE_SUITE', 'NAME_INCOME_TYPE', 'NAME_EDUCATION_TYPE',
'NAME_FAMILY_STATUS', 'NAME_HOUSING_TYPE']].astype('object' )
intervalo_valores(df_train[index] )<define_variables>
((app_train['DAYS_EMPLOYED']/-365)[(app_train['DAYS_EMPLOYED']/-365)<0][:5],
app_train['DAYS_EMPLOYED'][app_train['DAYS_EMPLOYED']>0][:5],
app_test['DAYS_EMPLOYED'][app_test['DAYS_EMPLOYED']>0][:5] )
index = 'Property GFA - Self-Reported(ft²)'<compute_test_metric>
app_train['DAYS_EMPLOYED'].replace({365243:np.nan},inplace=True)
 app_test['DAYS_EMPLOYED'].replace({365243:np.nan},inplace=True )
intervalo_valores(df_train[index] )<define_variables>
app_train['OWN_CAR_AGE'] = app_train['OWN_CAR_AGE'].astype('object')
 app_test['OWN_CAR_AGE'] = app_test['OWN_CAR_AGE'].astype('object' )
index = 'Water Use(All Water Sources )(kgal)'<feature_engineering>
app_train_col_drop.append('FLAG_MOBIL')
 app_train_col_drop.append('FLAG_CONT_MOBILE')
 app_train_col_drop.append('FLAG_EMAIL')
 app_train[['FLAG_EMP_PHONE','FLAG_WORK_PHONE','FLAG_PHONE']] = app_train[[
'FLAG_EMP_PHONE','FLAG_WORK_PHONE','FLAG_PHONE']].astype('object')
 app_test[['FLAG_EMP_PHONE','FLAG_WORK_PHONE','FLAG_PHONE']] = app_test[[
'FLAG_EMP_PHONE','FLAG_WORK_PHONE','FLAG_PHONE']].astype('object' )
df_train[index] = df_train[index].str.replace('Not Available', '0')
 df_test[index] = df_test[index].str.replace('Not Available', '0' )<data_type_conversions>
app_train['CNT_FAM_MEMBERS'] = app_train['CNT_FAM_MEMBERS'].astype('object')
 app_test['CNT_FAM_MEMBERS'] = app_test['CNT_FAM_MEMBERS'].astype('object' )
df_train[index] = df_train[index].astype('float64')
 df_test[index] = df_test[index].astype('float64' )<compute_test_metric>
app_train[['REGION_RATING_CLIENT','REGION_RATING_CLIENT_W_CITY']] = app_train[[
'REGION_RATING_CLIENT','REGION_RATING_CLIENT_W_CITY']].astype('object')
 app_test[['REGION_RATING_CLIENT','REGION_RATING_CLIENT_W_CITY']] = app_test[[
'REGION_RATING_CLIENT','REGION_RATING_CLIENT_W_CITY']].astype('object' )
intervalo_valores(df_train[index] )<define_variables>
app_train['WEEKDAY_APPR_PROCESS_START'] = app_train['WEEKDAY_APPR_PROCESS_START'].astype('object')
 app_test['WEEKDAY_APPR_PROCESS_START'] = app_test['WEEKDAY_APPR_PROCESS_START'].astype('object' )
index = 'Water Intensity(All Water Sources )(gal/ft²)'<feature_engineering>
app_train['HOUR_APPR_PROCESS_START'] = app_train['HOUR_APPR_PROCESS_START'].astype('object')
 app_test['HOUR_APPR_PROCESS_START'] = app_test['HOUR_APPR_PROCESS_START'].astype('object' )
df_train[index] = df_train[index].str.replace('Not Available', '0')
 df_test[index] = df_test[index].str.replace('Not Available', '0' )<data_type_conversions>
app_train_col_drop.append('REG_REGION_NOT_LIVE_REGION')
 app_train[['REG_REGION_NOT_WORK_REGION','LIVE_REGION_NOT_WORK_REGION', 'REG_CITY_NOT_LIVE_CITY',
'REG_CITY_NOT_WORK_CITY', 'LIVE_CITY_NOT_WORK_CITY']] = app_train[['REG_REGION_NOT_WORK_REGION','LIVE_REGION_NOT_WORK_REGION', 'REG_CITY_NOT_LIVE_CITY',
'REG_CITY_NOT_WORK_CITY', 'LIVE_CITY_NOT_WORK_CITY']].astype('object')
 app_test[['REG_REGION_NOT_WORK_REGION','LIVE_REGION_NOT_WORK_REGION', 'REG_CITY_NOT_LIVE_CITY',
'REG_CITY_NOT_WORK_CITY', 'LIVE_CITY_NOT_WORK_CITY']] = app_test[['REG_REGION_NOT_WORK_REGION','LIVE_REGION_NOT_WORK_REGION', 'REG_CITY_NOT_LIVE_CITY',
'REG_CITY_NOT_WORK_CITY', 'LIVE_CITY_NOT_WORK_CITY']].astype('object' )
df_train[index] = df_train[index].astype('float64')
 df_test[index] = df_test[index].astype('float64' )<compute_test_metric>
app_train['ORGANIZATION_TYPE'].replace('XNA',np.nan,inplace=True)
 app_test['ORGANIZATION_TYPE'].replace('XNA',np.nan,inplace=True)
 app_train['ORGANIZATION_TYPE'] = app_train['ORGANIZATION_TYPE'].astype('object')
 app_test['ORGANIZATION_TYPE'] = app_test['ORGANIZATION_TYPE'].astype('object' )
intervalo_valores(df_train[index] )<define_variables>
app_train[['FONDKAPREMONT_MODE', 'HOUSETYPE_MODE', 'WALLSMATERIAL_MODE', 'EMERGENCYSTATE_MODE']] = app_train[['FONDKAPREMONT_MODE', 'HOUSETYPE_MODE',
'WALLSMATERIAL_MODE', 'EMERGENCYSTATE_MODE']].astype('object')
 app_test[['FONDKAPREMONT_MODE', 'HOUSETYPE_MODE', 'WALLSMATERIAL_MODE', 'EMERGENCYSTATE_MODE']] = app_test[['FONDKAPREMONT_MODE', 'HOUSETYPE_MODE',
'WALLSMATERIAL_MODE', 'EMERGENCYSTATE_MODE']].astype('object' )
index = 'Source EUI(kBtu/ft²)'<compute_test_metric>
app_train[['OBS_30_CNT_SOCIAL_CIRCLE', 'DEF_30_CNT_SOCIAL_CIRCLE',
'OBS_60_CNT_SOCIAL_CIRCLE', 'DEF_60_CNT_SOCIAL_CIRCLE',]] = app_train[['OBS_30_CNT_SOCIAL_CIRCLE', 'DEF_30_CNT_SOCIAL_CIRCLE',
'OBS_60_CNT_SOCIAL_CIRCLE', 'DEF_60_CNT_SOCIAL_CIRCLE',]].astype('object')
 app_test[['OBS_30_CNT_SOCIAL_CIRCLE', 'DEF_30_CNT_SOCIAL_CIRCLE',
'OBS_60_CNT_SOCIAL_CIRCLE', 'DEF_60_CNT_SOCIAL_CIRCLE',]] = app_test[['OBS_30_CNT_SOCIAL_CIRCLE', 'DEF_30_CNT_SOCIAL_CIRCLE',
'OBS_60_CNT_SOCIAL_CIRCLE', 'DEF_60_CNT_SOCIAL_CIRCLE',]].astype('object' )
intervalo_valores(df_train[index] )<define_variables>
app_train[['FLAG_DOCUMENT_2', 'FLAG_DOCUMENT_3','FLAG_DOCUMENT_4', 'FLAG_DOCUMENT_5', 'FLAG_DOCUMENT_6',
'FLAG_DOCUMENT_7', 'FLAG_DOCUMENT_8', 'FLAG_DOCUMENT_9', 'FLAG_DOCUMENT_10', 'FLAG_DOCUMENT_11',
'FLAG_DOCUMENT_12','FLAG_DOCUMENT_13', 'FLAG_DOCUMENT_14', 'FLAG_DOCUMENT_15','FLAG_DOCUMENT_16',
'FLAG_DOCUMENT_17', 'FLAG_DOCUMENT_18','FLAG_DOCUMENT_19', 'FLAG_DOCUMENT_20', 'FLAG_DOCUMENT_21',]] = app_train[['FLAG_DOCUMENT_2', 'FLAG_DOCUMENT_3','FLAG_DOCUMENT_4', 'FLAG_DOCUMENT_5', 'FLAG_DOCUMENT_6',
'FLAG_DOCUMENT_7', 'FLAG_DOCUMENT_8', 'FLAG_DOCUMENT_9', 'FLAG_DOCUMENT_10', 'FLAG_DOCUMENT_11',
'FLAG_DOCUMENT_12','FLAG_DOCUMENT_13', 'FLAG_DOCUMENT_14', 'FLAG_DOCUMENT_15','FLAG_DOCUMENT_16',
'FLAG_DOCUMENT_17', 'FLAG_DOCUMENT_18','FLAG_DOCUMENT_19', 'FLAG_DOCUMENT_20', 'FLAG_DOCUMENT_21',]].astype('object')
 app_test[['FLAG_DOCUMENT_2', 'FLAG_DOCUMENT_3','FLAG_DOCUMENT_4', 'FLAG_DOCUMENT_5', 'FLAG_DOCUMENT_6',
'FLAG_DOCUMENT_7', 'FLAG_DOCUMENT_8', 'FLAG_DOCUMENT_9', 'FLAG_DOCUMENT_10', 'FLAG_DOCUMENT_11',
'FLAG_DOCUMENT_12','FLAG_DOCUMENT_13', 'FLAG_DOCUMENT_14', 'FLAG_DOCUMENT_15','FLAG_DOCUMENT_16',
'FLAG_DOCUMENT_17', 'FLAG_DOCUMENT_18','FLAG_DOCUMENT_19', 'FLAG_DOCUMENT_20', 'FLAG_DOCUMENT_21',]] = app_test[['FLAG_DOCUMENT_2', 'FLAG_DOCUMENT_3','FLAG_DOCUMENT_4', 'FLAG_DOCUMENT_5', 'FLAG_DOCUMENT_6',
'FLAG_DOCUMENT_7', 'FLAG_DOCUMENT_8', 'FLAG_DOCUMENT_9', 'FLAG_DOCUMENT_10', 'FLAG_DOCUMENT_11',
'FLAG_DOCUMENT_12','FLAG_DOCUMENT_13', 'FLAG_DOCUMENT_14', 'FLAG_DOCUMENT_15','FLAG_DOCUMENT_16',
'FLAG_DOCUMENT_17', 'FLAG_DOCUMENT_18','FLAG_DOCUMENT_19', 'FLAG_DOCUMENT_20', 'FLAG_DOCUMENT_21',]].astype('object')
 app_train_col_drop.extend(['FLAG_DOCUMENT_2', 'FLAG_DOCUMENT_4', 'FLAG_DOCUMENT_5', 'FLAG_DOCUMENT_7', 'FLAG_DOCUMENT_9',
'FLAG_DOCUMENT_10', 'FLAG_DOCUMENT_11', 'FLAG_DOCUMENT_12','FLAG_DOCUMENT_13', 'FLAG_DOCUMENT_14',
'FLAG_DOCUMENT_15', 'FLAG_DOCUMENT_16', 'FLAG_DOCUMENT_17', 'FLAG_DOCUMENT_18',
'FLAG_DOCUMENT_19', 'FLAG_DOCUMENT_20', 'FLAG_DOCUMENT_21'] )
index = 'Release Date'<drop_column>
app_train[['AMT_REQ_CREDIT_BUREAU_HOUR', 'AMT_REQ_CREDIT_BUREAU_DAY','AMT_REQ_CREDIT_BUREAU_WEEK',
'AMT_REQ_CREDIT_BUREAU_MON','AMT_REQ_CREDIT_BUREAU_QRT', 'AMT_REQ_CREDIT_BUREAU_YEAR',]] = app_train[['AMT_REQ_CREDIT_BUREAU_HOUR', 'AMT_REQ_CREDIT_BUREAU_DAY','AMT_REQ_CREDIT_BUREAU_WEEK',
'AMT_REQ_CREDIT_BUREAU_MON','AMT_REQ_CREDIT_BUREAU_QRT', 'AMT_REQ_CREDIT_BUREAU_YEAR',]].astype('object')
 app_test[['AMT_REQ_CREDIT_BUREAU_HOUR', 'AMT_REQ_CREDIT_BUREAU_DAY','AMT_REQ_CREDIT_BUREAU_WEEK',
'AMT_REQ_CREDIT_BUREAU_MON','AMT_REQ_CREDIT_BUREAU_QRT', 'AMT_REQ_CREDIT_BUREAU_YEAR',]] = app_test[['AMT_REQ_CREDIT_BUREAU_HOUR', 'AMT_REQ_CREDIT_BUREAU_DAY','AMT_REQ_CREDIT_BUREAU_WEEK',
'AMT_REQ_CREDIT_BUREAU_MON','AMT_REQ_CREDIT_BUREAU_QRT', 'AMT_REQ_CREDIT_BUREAU_YEAR',]].astype('object')
del df_train[index]
del df_test[index]<define_variables>
gc.collect()
index = 'Water Required?'<data_type_conversions>
train_test = train_test.drop(list(set(app_train_col_drop)) , axis=1)
 categorical_col = train_test.select_dtypes('object' ).columns
numeric_col = train_test.select_dtypes('number' ).columns
numeric_col = numeric_col.drop('TARGET')
 gc.collect()
df_train[index] = df_train[index].astype('category' ).cat.codes
df_test[index] = df_test[index].astype('category' ).cat.codes<filter>
train_test = replace_XNA_XAP(train_test)
 train_test,_ = binary_encoding(train_test, categorical_col)
 train_test,_ = one_hot_encoding(train_test,categorical_col,nan_as_category=True)
 descriptive_stat_feat(train_test,numeric_col)
 del app_train,app_test
reduce_memory_usage(train_test)
 gc.collect()
df_train = df_train[df_train[index] > -1]<compute_test_metric>
bureau_balance = replace_XNA_XAP(bureau_balance)
 bb_bin =[]
bureau_balance, bb_cat = one_hot_encoding(bureau_balance,['STATUS'],nan_as_category=False)
 bb_aggregations = {'MONTHS_BALANCE':['min','max','mean']}
for col in bb_cat+bb_bin:
bb_aggregations[col] =['sum','mean']
bb_agg = bureau_balance.groupby('SK_ID_BUREAU' ).agg(bb_aggregations)
 bb_agg.columns = [e[0]+ "_" +e[1].upper() for e in bb_agg.columns.tolist() ]
bb_agg.head(2 )
intervalo_valores(df_train[index] )<define_variables>
bureau[['CREDIT_TYPE','CNT_CREDIT_PROLONG']] = bureau[['CREDIT_TYPE','CNT_CREDIT_PROLONG']].astype('object')
index = 'DOF Benchmarking Submission Status'<drop_column>
bureau['AMT_CREDIT_SUM'] = bureau['AMT_CREDIT_SUM'].apply(np.log1p )
del df_train[index]
del df_test[index]<define_variables>
bureau['AMT_CREDIT_SUM_DEBT'] = np.log1p(bureau['AMT_CREDIT_SUM_DEBT'] )
index = 'Latitude'<drop_column>
bureau['AMT_ANNUITY'] = np.log1p(bureau['AMT_ANNUITY'] )
del df_train[index]
del df_test[index]<define_variables>
bureau = bureau.drop(bureau_drop_col, axis=1)
 bureau = replace_XNA_XAP(bureau)
 bureau_cat_col = bureau.select_dtypes('object' ).columns
bureau_numeric_col = bureau.select_dtypes('number' ).columns
bureau_numeric_col = bureau_numeric_col.drop(['SK_ID_CURR', 'SK_ID_BUREAU'])
 bureau_bin =[]
bureau, bureau_bin = binary_encoding(bureau, bureau_cat_col)
 bureau,bureau_cat = one_hot_encoding(bureau,bureau_cat_col,nan_as_category=False)
 bureau = bureau.join(bb_agg,how='left',on ='SK_ID_BUREAU')
 bureau.drop('SK_ID_BUREAU',axis = 1,inplace=True)
 num_aggregators = {
'DAYS_CREDIT': ['min','max','var'],
'DAYS_CREDIT_ENDDATE': ['min','max','mean'],
'DAYS_ENDDATE_FACT': ['mean'],
'CREDIT_DAY_OVERDUE':['max','mean'],
'AMT_CREDIT_SUM':['mean','sum'],
'AMT_CREDIT_SUM_DEBT':['sum','max','mean'],
'AMT_CREDIT_SUM_LIMIT':['sum','mean'],
'AMT_CREDIT_SUM_OVERDUE':['mean'],
'AMT_ANNUITY':['mean','max'],
'MONTHS_BALANCE_MIN':['min'],
'MONTHS_BALANCE_MAX':['max'],
'MONTHS_BALANCE_MEAN':['mean']
}
cat_aggregates = {}
for col in bureau_cat+bureau_bin:
cat_aggregates[col] = ['sum','mean']
for col in bb_cat:
cat_aggregates[col+'_MEAN'] = ['mean']
cat_aggregates[col+'_SUM'] = ['sum']
bureau_agg = bureau.groupby('SK_ID_CURR' ).agg({**num_aggregators, **cat_aggregates})
 bureau_agg.columns = pd.Index(['BUREAU_'+e[0]+'_'+e[1].upper() for e in bureau_agg.columns.tolist() ])
 bureau_agg = bureau_agg.reset_index()
active = bureau[bureau['CREDIT_ACTIVE_Closed'] ==0]
active_agg = active.groupby('SK_ID_CURR' ).agg(num_aggregators)
 active_agg.columns = pd.Index(['ACTIVE_'+ e[0]+'_'+e[1].upper() for e in active_agg.columns.tolist() ])
 bureau_agg = bureau_agg.join(active_agg, on ='SK_ID_CURR', how='left')
 del active, active_agg
closed = bureau[bureau['CREDIT_ACTIVE_Closed'] ==1]
closed_agg = bureau.groupby('SK_ID_CURR' ).agg(num_aggregators)
 closed_agg.columns = pd.Index(['CLOSED_'+ e[0]+'_'+e[1].upper() for e in closed_agg.columns.tolist() ])
 bureau_agg = bureau_agg.join(closed_agg, on = 'SK_ID_CURR', how='left')
 del closed_agg, closed
del bureau,bureau_balance,bb_agg
gc.collect()
bureau_agg = bureau_agg.set_index('SK_ID_CURR')
 bureau_agg.head()
index = 'Longitude'<drop_column>
POS_CASH_balance = replace_XNA_XAP(POS_CASH_balance)
 pos_bin =[]
POS_CASH_balance,pos_bin = binary_encoding(POS_CASH_balance,columns=['NAME_CONTRACT_STATUS'])
 POS_CASH_balance,pos_cat = one_hot_encoding(POS_CASH_balance,columns=['NAME_CONTRACT_STATUS'], nan_as_category=True)
 pos_aggregate ={
'MONTHS_BALANCE':['mean','min','max'],
'CNT_INSTALMENT':['mean','min','max'],
'CNT_INSTALMENT_FUTURE':['mean','min','max'],
'SK_DPD':['min','max','mean'],
'SK_DPD_DEF':['min','max','mean']
}
for col in pos_cat+pos_bin:
pos_aggregate[col] =['sum','mean']
pos_agg = POS_CASH_balance.groupby('SK_ID_CURR' ).agg(pos_aggregate)
 pos_agg.columns = pd.Index(['POS_'+ e[0]+ '_'+ e[1].upper() for e in pos_agg.columns.tolist() ])
 pos_agg['POS_COUNT'] = POS_CASH_balance.groupby('SK_ID_CURR' ).size()
del POS_CASH_balance
pos_agg.head(2 )
del df_train[index]
del df_test[index]<define_variables>
gc.collect()
reduce_memory_usage(pos_agg )
index = 'Community Board'<drop_column>
credit_card_balance['AMT_CREDIT_LIMIT_ACTUAL'] = np.sqrt(credit_card_balance['AMT_CREDIT_LIMIT_ACTUAL'] )
del df_train[index]
del df_test[index]<define_variables>
credit_card_balance['CNT_DRAWINGS_CURRENT'] = np.sqrt(credit_card_balance['CNT_DRAWINGS_CURRENT'] )
index = 'Council District'<drop_column>
credit_card_balance['CNT_DRAWINGS_OTHER_CURRENT'] = credit_card_balance['CNT_DRAWINGS_OTHER_CURRENT'].astype('object' )
del df_train[index]
del df_test[index]<define_variables>
credit_card_balance['CNT_DRAWINGS_POS_CURRENT'] = np.sqrt(credit_card_balance['CNT_DRAWINGS_POS_CURRENT'] )
index = 'Census Tract'<drop_column>
credit_card_balance['CNT_INS'] = np.sqrt(credit_card_balance['CNT_INSTALMENT_MATURE_CUM'] )
del df_train[index]
del df_test[index]<define_variables>
credit_card_balance['NAME_CONTRACT_STATUS'] = credit_card_balance['NAME_CONTRACT_STATUS'].astype('object' )
index = 'NTA'<drop_column>
credit_card_balance = replace_XNA_XAP(credit_card_balance)
 credit_object_col = credit_card_balance.select_dtypes('object' ).columns
credit_bin =[]
credit_card_balance,credit_bin = binary_encoding(credit_card_balance,credit_object_col)
 credit_card_balance,credit_cat = one_hot_encoding(credit_card_balance,credit_object_col, nan_as_category=False)
 credit_aggregation = {
'MONTHS_BALANCE':['mean','min','max'],
'AMT_BALANCE':['mean','min'],
'AMT_CREDIT_LIMIT_ACTUAL':['mean','min'],
'AMT_DRAWINGS_ATM_CURRENT':['mean','min','max'],
'AMT_DRAWINGS_CURRENT':['mean','min','max'],
'AMT_DRAWINGS_OTHER_CURRENT':['mean','min','max'],
'AMT_DRAWINGS_POS_CURRENT':['mean','min','max'],
'AMT_INST_MIN_REGULARITY':['mean','min','max'],
'AMT_PAYMENT_CURRENT':['mean','min','max'],
'AMT_PAYMENT_TOTAL_CURRENT':['mean','min','max'],
'AMT_RECEIVABLE_PRINCIPAL':['mean','min','max'],
'AMT_RECIVABLE':['mean','min','max'],
'AMT_TOTAL_RECEIVABLE':['mean','min','max'],
'CNT_DRAWINGS_ATM_CURRENT':['mean','min','max'],
'CNT_DRAWINGS_CURRENT':['mean','min','max'],
'CNT_DRAWINGS_POS_CURRENT':['mean','min','max'],
'CNT_INSTALMENT_MATURE_CUM':['mean','min','max'],
'SK_DPD':['mean','min','max'],
'SK_DPD_DEF':['mean','min','max'],
}
for col in credit_cat+credit_bin:
credit_aggregation[col] = ['mean','sum']
credit_agg = credit_card_balance.groupby('SK_ID_CURR' ).agg(credit_aggregation)
 credit_agg.columns = pd.Index(['CREDIT_'+e[0]+'_'+ e[1].upper() for e in credit_agg.columns.tolist() ])
 credit_agg['CREDIT_COUNT'] = credit_card_balance.groupby('SK_ID_CURR' ).size()
del credit_card_balance
credit_agg.head(2 )
del df_train[index]
del df_test[index]<define_variables>
prev_cat_col = previous_application.select_dtypes(include='object' ).columns
int_col = [i for i in previous_application.columns.values if i not in prev_cat_col]
index = 'ENERGY STAR Score'<compute_test_metric>
previous_application['AMT_ANNUITY'] = np.log1p(previous_application['AMT_ANNUITY'] )
intervalo_valores(df_train[index] )<count_missing_values>
previous_application['AMT_APPLICATION'] = np.sqrt(previous_application['AMT_APPLICATION'] )
df_train.isnull().any()<count_missing_values>
previous_application['AMT_CREDIT'] = np.sqrt(previous_application['AMT_CREDIT'] )
df_test.isnull().any()<count_missing_values>
previous_application['AMT_DOWN_PAYMENT'] = np.log1p(previous_application['AMT_DOWN_PAYMENT'] )
df_train.isnull().sum()<count_missing_values>
previous_application['AMT_GOODS_PRICE'] = np.log1p(previous_application['AMT_GOODS_PRICE'] )
df_test.isnull().sum()<drop_column>
previous_application[['HOUR_APPR_PROCESS_START','NFLAG_INSURED_ON_APPROVAL']] = previous_application[['HOUR_APPR_PROCESS_START','NFLAG_INSURED_ON_APPROVAL']].astype('object' )
dataset = df_train.drop(['ENERGY STAR Score'], axis=1)
 dataset['ENERGY STAR Score'] = df_train['ENERGY STAR Score']<prepare_x_and_y>
previous_application[previous_application['SELLERPLACE_AREA']>500000]
X = dataset.iloc[:,:-1]
y = dataset['ENERGY STAR Score'].values<set_options>
previous_application['DAYS_FIRST_DRAWING'].replace({365243:np.nan},inplace=True)
 previous_application['DAYS_FIRST_DUE'].replace({365243:np.nan},inplace=True)
 previous_application['DAYS_LAST_DUE_1ST_VERSION'].replace({365243:np.nan},inplace=True)
 previous_application['DAYS_LAST_DUE'].replace({365243:np.nan},inplace=True)
 previous_application['DAYS_TERMINATION'].replace({365243:np.nan},inplace=True)
warnings.filterwarnings('ignore')
 %matplotlib inline<import_modules>
previous_application = replace_XNA_XAP(previous_application)
 prev_bin =[]
previous_application,prev_bin = binary_encoding(previous_application,prev_cat_col)
 previous_application,prev_cat = one_hot_encoding(previous_application,columns= prev_cat_col,nan_as_category=True)
 prev_aggregate = {
'AMT_ANNUITY':['mean','sum','min'],
'AMT_APPLICATION':['mean'],
'AMT_CREDIT':['mean','min','max'],
'AMT_DOWN_PAYMENT':['mean','min'],
'AMT_GOODS_PRICE':['mean','sum','max'],
'HOUR_APPR_PROCESS_START':['mean','min'],
'NFLAG_LAST_APPL_IN_DAY': ['mean'],
'RATE_DOWN_PAYMENT': ['mean'],
'RATE_INTEREST_PRIMARY':['mean','min','max'],
'RATE_INTEREST_PRIVILEGED':['mean','min'],
'DAYS_DECISION':['mean'],
'SELLERPLACE_AREA':['mean'],
'CNT_PAYMENT':['mean','sum'],
'DAYS_FIRST_DRAWING':['mean','min'],
'DAYS_FIRST_DUE':['mean','min'],
'DAYS_LAST_DUE_1ST_VERSION':['mean','min'],
'DAYS_LAST_DUE':['mean','max'],
'DAYS_TERMINATION':['mean','max'],
'NFLAG_INSURED_ON_APPROVAL' : ['mean'],
}
cat_prev_aggregate = {}
for col in prev_cat+prev_bin:
cat_prev_aggregate[col] =['mean','sum']
prev_agg = previous_application.groupby('SK_ID_CURR' ).agg({**prev_aggregate,**cat_prev_aggregate})
 prev_agg.columns = pd.Index(['PREV_'+e[0]+ '_'+ e[1].upper() for e in prev_agg.columns.tolist() ])
 prev_agg = prev_agg.reset_index()
refused = previous_application[previous_application['NAME_CONTRACT_STATUS_Refused'] ==1]
refused_agg = refused.groupby('SK_ID_CURR' ).agg(prev_aggregate)
 refused_agg.columns = pd.Index(['REFUSE_'+e[0]+'_'+ e[1].upper() for e in refused_agg.columns.tolist() ])
 prev_agg = prev_agg.join(refused_agg, on='SK_ID_CURR', how='left')
 del refused, refused_agg
canceled = previous_application[previous_application['NAME_CONTRACT_STATUS_Canceled']==1]
canceled_agg = canceled.groupby('SK_ID_CURR' ).agg(prev_aggregate)
 canceled_agg.columns = pd.Index(['CANC_'+ e[0]+ '_'+ e[1].upper() for e in canceled_agg.columns.tolist() ])
 prev_agg = prev_agg.join(canceled_agg, on='SK_ID_CURR', how='left')
 del canceled, canceled_agg
prev_agg['PREV_COUNT'] = previous_application.groupby('SK_ID_CURR' ).size()
prev_agg = prev_agg.set_index('SK_ID_CURR')
 del previous_application
prev_agg.head()
from sklearn import linear_model
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import r2_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import MinMaxScaler
import statsmodels.api as sm<choose_model_class>
inst_aggregator = {
'NUM_INSTALMENT_VERSION':['mean'],
'NUM_INSTALMENT_NUMBER':['mean','max'],
'DAYS_INSTALMENT':['min','mean'],
'AMT_INSTALMENT':['mean'],
'AMT_PAYMENT':['mean']
}
inst_agg = installments_payments.groupby('SK_ID_CURR' ).agg(inst_aggregator)
 inst_agg.columns = pd.Index(['INST_'+e[0]+ '_'+ e[1].upper() for e in inst_agg.columns.tolist() ])
 inst_agg['INST_COUNT'] = installments_payments.groupby('SK_ID_CURR' ).size()
del installments_payments
inst_agg.head()
modelo = GradientBoostingRegressor()<train_model>
train_test = train_test.join(bureau_agg,how='left',on='SK_ID_CURR')
 del bureau_agg
train_test = train_test.join(pos_agg, how='left',on='SK_ID_CURR')
 del pos_agg
train_test = train_test.join(inst_agg,how='left',on='SK_ID_CURR')
 del inst_agg
train_test = train_test.join(credit_agg, how='left',on='SK_ID_CURR')
 del credit_agg
train_test = train_test.join(prev_agg, how='left',on='SK_ID_CURR')
 del prev_agg
reduce_memory_usage(train_test )
Stand_coef_linear_reg.fit(X,y)
 for feature_importances_, var in sorted(zip(map(abs, Stand_coef_linear_reg.steps[1][1].feature_importances_), dataset.columns[:-1]), reverse = True):
print("%6.3f %s" %(feature_importances_,var))<define_variables>
col_drop = ['TARGET','SK_ID_CURR']
X = train_test[train_test['TARGET'].notnull() ].drop(col_drop, axis=1)
 y = train_test[train_test['TARGET'].notnull() ]['TARGET']
test_new = train_test[train_test['TARGET'].isnull() ].drop(col_drop, axis=1)
cols_irr = [
'Property GFA - Self-Reported(ft²)',
'Weather Normalized Site EUI(kBtu/ft²)',
'Water Use(All Water Sources )(kgal)',
'Water Intensity(All Water Sources )(gal/ft²)',
'Electricity Use - Grid Purchase(kBtu)',
'Borough',
'Natural Gas Use(kBtu)',
'Fuel Oil
'Indirect GHG Emissions(Metric Tons CO2e)',
'Total GHG Emissions(Metric Tons CO2e)',
'Direct GHG Emissions(Metric Tons CO2e)',
'Occupancy',
'Weather Normalized Site Natural Gas Intensity(therms/ft²)',
'3rd Largest Property Use Type - Gross Floor Area(ft²)',
'Weather Normalized Site Natural Gas Use(therms)',
'Weather Normalized Site Electricity(kWh)',
'Number of Buildings - Self-reported',
'Fuel Oil
'Water Required?',
'Metered Areas(Energy)',
'Metered Areas(Water)',
'Fuel Oil
'Fuel Oil
'District Steam Use(kBtu)',
'Diesel
'3rd Largest Property Use Type'
]<drop_column>
def model(X_train, X_valid, y_train, y_valid,test_new,random_seed):
lg_param = {}
lg_param['learning_rate'] = 0.02
lg_param['n_estimators'] = 10000
lg_param['max_depth'] = 8
lg_param['num_leaves'] = 34
lg_param['boosting_type'] = 'gbdt'
lg_param['feature_fraction'] = 0.9
lg_param['bagging_fraction'] = 0.9
lg_param['min_child_samples'] = 30
lg_param['lambda_l1'] = 0.04
lg_param['lambda_l2'] = 0.08
lg_param['silent'] = -1
lg_param['verbose'] = -1
lg_param['nthread'] = 4
lg_param['seed'] = random_seed
lgb_model = lgb.LGBMClassifier(**lg_param)
 print('-'*10,'*'*20,'-'*10)
 lgb_model.fit(X_train,y_train,eval_set=[(X_train,y_train),(X_valid,y_valid)],
eval_metric ='auc', verbose =100, early_stopping_rounds=200)
 y_pred = lgb_model.predict_proba(X_valid)[:,1]
print('roc_auc_score',roc_auc_score(y_valid,y_pred),'-'*30,i+1)
 y_pred_new = lgb_model.predict_proba(test_new)[:,1]
return y_pred,y_pred_new,lgb_model
X = X.drop(columns = cols_irr )<normalization>
kf = KFold(n_splits=3, shuffle=True, random_state=seed)
 y_pred_new = 0
for i,(train_index, valid_index)in enumerate(kf.split(X,y)) :
X_train, X_valid = X.loc[train_index], X.loc[valid_index]
y_train, y_valid = y[train_index], y[valid_index]
print('
{} fold of {} KFold'.format(i+1,kf.n_splits))
y_pred,y_pred2,lgb_model = model(X_train, X_valid, y_train, y_valid,test_new,random_seed = i)
 y_pred_new += y_pred2
scaler = MinMaxScaler()
cols = X.columns
X[cols] = scaler.fit_transform(X)
 X.head()<train_model>
feat_impo = pd.DataFrame({'Columns':X.columns,'Importance':lgb_model.feature_importances_})
 feat_impo.sort_values('Importance',ascending=False ).head()
feat_impo.to_csv('feat_impo.csv',index=False )
Xc = sm.add_constant(X)
 modelo_v1 = sm.OLS(y, Xc)
 modelo_v2 = modelo_v1.fit()<choose_model_class>
submit = pd.DataFrame({'SK_ID_CURR':test_index,'TARGET':y_pred_new/kf.n_splits})
 submit.to_csv('home_credit.csv',index=False)
 submit.head()