Kaggle competition: Tabular Playground Series - Jan 2021
def get_dataset() :
app_train = pd.read_csv('.. /input/home-credit-default-risk/application_train.csv')
 app_test = pd.read_csv('.. /input/home-credit-default-risk/application_test.csv')
 apps = pd.concat([app_train, app_test])
 prev = pd.read_csv('.. /input/home-credit-default-risk/previous_application.csv')
 return apps, prev<categorify>
input_path = Path('/kaggle/input/tabular-playground-series-jan-2021/' )
apps, prev = get_dataset()
apps_all = get_apps_all_with_prev_agg(apps, prev)
 apps_all = get_apps_all_encoded(apps_all)
 apps_all_train, apps_all_test = get_apps_all_train_test(apps_all)
 clf = train_apps_all(apps_all_train )<save_to_csv>
train = pd.read_csv(input_path / 'train.csv', index_col='id')
 display(train.head() )
preds = clf.predict_proba(apps_all_test.drop(['SK_ID_CURR'], axis=1)) [:, 1 ]
apps_all_test['TARGET'] = preds
apps_all_test[['SK_ID_CURR', 'TARGET']].to_csv('prev_baseline_03.csv', index=False )<load_from_csv>
test = pd.read_csv(input_path / 'test.csv', index_col='id')
 display(test.head() )
bureau = pd.read_csv('.. /input/home-credit-default-risk/bureau.csv')
 bureau_bal = pd.read_csv('.. /input/home-credit-default-risk/bureau_balance.csv' )<merge>
submission = pd.read_csv(input_path / 'sample_submission.csv', index_col='id')
 display(submission.head() )
bureau_app = bureau.merge(app_train[['SK_ID_CURR', 'TARGET']], left_on='SK_ID_CURR', right_on='SK_ID_CURR', how='inner')
 bureau_app.shape<define_variables>
features = [f'cont{x}'for x in range(1,15)]
data= train[features]
train.isnull().sum()
num_columns = bureau_app.dtypes[bureau_app.dtypes != 'object'].index.tolist()
num_columns = [column for column in num_columns if column not in['SK_ID_BUREAU', 'SK_ID_CURR', 'TARGET']]
num_columns<data_type_conversions>
train[train['target'] == 0]
object_columns = bureau.dtypes[bureau.dtypes=='object'].index.tolist()
object_columns<groupby>
def replace_outliers(data):
for col in data.columns:
Q1 = data[col].quantile(0.25)
 Q3 = data[col].quantile(0.75)
 IQR = Q3 - Q1
median_ = data[col].median()
data.loc[(( data[col] < Q1 - 1.5*IQR)|(data[col] > Q3 + 1.5*IQR)) , col] = median_
return data
show_category_by_target(bureau_app, object_columns )<feature_engineering>
train = replace_outliers(train )
def get_bureau_processed(bureau):
bureau['BUREAU_ENDDATE_FACT_DIFF'] = bureau['DAYS_CREDIT_ENDDATE'] - bureau['DAYS_ENDDATE_FACT']
bureau['BUREAU_CREDIT_FACT_DIFF'] = bureau['DAYS_CREDIT'] - bureau['DAYS_ENDDATE_FACT']
bureau['BUREAU_CREDIT_ENDDATE_DIFF'] = bureau['DAYS_CREDIT'] - bureau['DAYS_CREDIT_ENDDATE']
bureau['BUREAU_CREDIT_DEBT_RATIO']=bureau['AMT_CREDIT_SUM_DEBT']/bureau['AMT_CREDIT_SUM']
bureau['BUREAU_CREDIT_DEBT_DIFF'] = bureau['AMT_CREDIT_SUM_DEBT'] - bureau['AMT_CREDIT_SUM']
bureau['BUREAU_IS_DPD'] = bureau['CREDIT_DAY_OVERDUE'].apply(lambda x: 1 if x > 0 else 0)
 bureau['BUREAU_IS_DPD_OVER120'] = bureau['CREDIT_DAY_OVERDUE'].apply(lambda x: 1 if x >120 else 0)
 return bureau<drop_column>
corr_feature = ['cont6','cont7','cont8','cont9','cont10','cont11','cont12','cont13']
corr_in = train[corr_feature]
def get_bureau_day_amt_agg(bureau):
bureau_agg_dict = {
'SK_ID_BUREAU':['count'],
'DAYS_CREDIT':['min', 'max', 'mean'],
'CREDIT_DAY_OVERDUE':['min', 'max', 'mean'],
'DAYS_CREDIT_ENDDATE':['min', 'max', 'mean'],
'DAYS_ENDDATE_FACT':['min', 'max', 'mean'],
'AMT_CREDIT_MAX_OVERDUE': ['max', 'mean'],
'AMT_CREDIT_SUM': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_DEBT': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_OVERDUE': ['max', 'mean', 'sum'],
'AMT_ANNUITY': ['max', 'mean', 'sum'],
'BUREAU_ENDDATE_FACT_DIFF':['min', 'max', 'mean'],
'BUREAU_CREDIT_FACT_DIFF':['min', 'max', 'mean'],
'BUREAU_CREDIT_ENDDATE_DIFF':['min', 'max', 'mean'],
'BUREAU_CREDIT_DEBT_RATIO':['min', 'max', 'mean'],
'BUREAU_CREDIT_DEBT_DIFF':['min', 'max', 'mean'],
'BUREAU_IS_DPD':['mean', 'sum'],
'BUREAU_IS_DPD_OVER120':['mean', 'sum']
}
bureau_grp = bureau.groupby('SK_ID_CURR')
 bureau_day_amt_agg = bureau_grp.agg(bureau_agg_dict)
 bureau_day_amt_agg.columns = ['BUREAU_'+('_' ).join(column ).upper() for column in bureau_day_amt_agg.columns.ravel() ]
bureau_day_amt_agg = bureau_day_amt_agg.reset_index()
print('bureau_day_amt_agg shape:', bureau_day_amt_agg.shape)
 return bureau_day_amt_agg<groupby>
train = train.drop(284103 )
def get_bureau_active_agg(bureau):
cond_active = bureau['CREDIT_ACTIVE'] == 'Active'
bureau_active_grp = bureau[cond_active].groupby(['SK_ID_CURR'])
 bureau_agg_dict = {
'SK_ID_BUREAU':['count'],
'DAYS_CREDIT':['min', 'max', 'mean'],
'CREDIT_DAY_OVERDUE':['min', 'max', 'mean'],
'DAYS_CREDIT_ENDDATE':['min', 'max', 'mean'],
'DAYS_ENDDATE_FACT':['min', 'max', 'mean'],
'AMT_CREDIT_MAX_OVERDUE': ['max', 'mean'],
'AMT_CREDIT_SUM': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_DEBT': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_OVERDUE': ['max', 'mean', 'sum'],
'AMT_ANNUITY': ['max', 'mean', 'sum'],
'BUREAU_ENDDATE_FACT_DIFF':['min', 'max', 'mean'],
'BUREAU_CREDIT_FACT_DIFF':['min', 'max', 'mean'],
'BUREAU_CREDIT_ENDDATE_DIFF':['min', 'max', 'mean'],
'BUREAU_CREDIT_DEBT_RATIO':['min', 'max', 'mean'],
'BUREAU_CREDIT_DEBT_DIFF':['min', 'max', 'mean'],
'BUREAU_IS_DPD':['mean', 'sum'],
'BUREAU_IS_DPD_OVER120':['mean', 'sum']
}
bureau_active_agg = bureau_active_grp.agg(bureau_agg_dict)
 bureau_active_agg.columns = ['BUREAU_ACT_'+('_' ).join(column ).upper() for column in bureau_active_agg.columns.ravel() ]
bureau_active_agg = bureau_active_agg.reset_index()
print('bureau_active_agg shape:', bureau_active_agg.shape)
 return bureau_active_agg<merge>
target = train.pop('target')
 X_train, X_test, y_train, y_test = train_test_split(train, target, train_size=0.60 )
def get_bureau_bal_agg(bureau, bureau_bal):
bureau_bal = bureau_bal.merge(bureau[['SK_ID_CURR', 'SK_ID_BUREAU']], on='SK_ID_BUREAU', how='left')
 bureau_bal['BUREAU_BAL_IS_DPD'] = bureau_bal['STATUS'].apply(lambda x: 1 if x in['1','2','3','4','5'] else 0)
 bureau_bal['BUREAU_BAL_IS_DPD_OVER120'] = bureau_bal['STATUS'].apply(lambda x: 1 if x =='5' else 0)
 bureau_bal_grp = bureau_bal.groupby('SK_ID_CURR')
 bureau_bal_agg_dict = {
'SK_ID_CURR':['count'],
'MONTHS_BALANCE':['min', 'max', 'mean'],
'BUREAU_BAL_IS_DPD':['mean', 'sum'],
'BUREAU_BAL_IS_DPD_OVER120':['mean', 'sum']
}
bureau_bal_agg = bureau_bal_grp.agg(bureau_bal_agg_dict)
 bureau_bal_agg.columns = [ 'BUREAU_BAL_'+('_' ).join(column ).upper() for column in bureau_bal_agg.columns.ravel() ]
bureau_bal_agg = bureau_bal_agg.reset_index()
print('bureau_bal_agg shape:', bureau_bal_agg.shape)
 return bureau_bal_agg<merge>
model = XGBRegressor(n_estimators=500, learning_rate=0.05, n_jobs=4)
 model.fit(X_train, y_train,
early_stopping_rounds=5,
eval_set=[(X_test, y_test)],
verbose=False )
def get_bureau_agg(bureau, bureau_bal):
bureau = get_bureau_processed(bureau)
 bureau_day_amt_agg = get_bureau_day_amt_agg(bureau)
 bureau_active_agg = get_bureau_active_agg(bureau)
 bureau_bal_agg = get_bureau_bal_agg(bureau, bureau_bal)
 bureau_agg = bureau_day_amt_agg.merge(bureau_active_agg, on='SK_ID_CURR', how='left')
 bureau_agg = bureau_agg.merge(bureau_bal_agg, on='SK_ID_CURR', how='left')
 print('bureau_agg shape:', bureau_agg.shape)
 return bureau_agg<merge>
y_pred = model.predict(X_test)
 score = mean_squared_error(y_test, y_pred, squared=False)
 print(score )
<categorify><EOS> 
submission['target'] = model.predict(test)
 submission.to_csv('xgb_regressor.csv' )