Kaggle competition: Home Credit Default Risk
test_df = pd.read_csv(f".. /input/sample_submission.csv" )<count_values>
app_train = pd.read_csv(path + "application_train.csv")
 app_train.head()
df["label"].value_counts()<set_options>
bureau = pd.read_csv(path + "bureau.csv")
 bureau['YEAR']=(( bureau['DAYS_CREDIT'] /365)).abs().pow(0.5 ).round(0)
 bureau.head()
init_notebook_mode(connected=True)
<install_modules>
bureau_balance = pd.read_csv(path + "bureau_balance.csv")
 bureau_balance['YEAR']=(( bureau_balance['MONTHS_BALANCE'])).abs().pow(0.2 ).round(0)
 bureau_balance.head(20)
!pip install imagesize<feature_engineering>
credit_card_balance = pd.read_csv(path + "credit_card_balance.csv")
 credit_card_balance.head()
df["width"] = 0
df["height"] = 0
df["aspect_ratio"] = 0.0
for idx, row in df.iterrows() :
width, height = imagesize.get(row["name"])
 df.at[idx, "width"] = width
df.at[idx, "height"] = height
df.at[idx, "aspect_ratio"] = float(height)/ float(width )<load_pretrained>
pcb = pd.read_csv(path + "POS_CASH_balance.csv")
 pcb.head()
path = Path(".. /input")
 SEED = 24
tfms = get_transforms(do_flip=True, max_rotate=10, max_zoom=1.3, max_lighting=0.4, max_warp=0.25, xtra_tfms=[rgb_randomize(channel=0, thresh=0.9, p=0.1),rgb_randomize(channel=2, thresh=0.9, p=0.1),rgb_randomize(channel=2, thresh=0.9, p=0.1)])
 data = ImageDataBunch.from_folder(path/"train",valid_pct=0.2, ds_tfms=tfms, size=128, bs=64, seed=SEED ).normalize(imagenet_stats )<choose_model_class>
previous_application = pd.read_csv(path + "previous_application.csv")
 previous_application.head()
learn = cnn_learner(data, models.resnet34, metrics=[accuracy],model_dir="/tmp/model/" )<train_model>
installments_payments = pd.read_csv(path + "installments_payments.csv")
 installments_payments.head()
lr=5e-2
learn.fit_one_cycle(15,slice(lr))<save_model>
app_test = pd.read_csv('.. /input/application_test.csv')
 app_test['is_test'] = 1
app_test['is_train'] = 0
app_train['is_test'] = 0
app_train['is_train'] = 1
Y = app_train['TARGET']
train_X = app_train.drop(['TARGET'], axis = 1)
 test_id = app_test['SK_ID_CURR']
test_X = app_test
data = pd.concat([train_X, test_X], axis=0 ).set_index('SK_ID_CURR' )
learn.save('stage1' )<load_pretrained>
def _get_categorical_features(df):
feats = [col for col in list(df.columns)if df[col].dtype == 'object']
return feats
def _factorize_categoricals(df, cats):
for col in cats:
df[col], _ = pd.factorize(df[col])
 return df
def _get_dummies(df, cats):
for col in cats:
df = pd.concat([df, pd.get_dummies(df[col], prefix=col)], axis=1)
 return df
data_cats = _get_categorical_features(data)
 prev_app_cats = _get_categorical_features(previous_application)
 bureau_cats = _get_categorical_features(bureau)
 pcb_cats = _get_categorical_features(pcb)
 ccbal_cats = _get_categorical_features(credit_card_balance)
 previous_application = _get_dummies(previous_application, prev_app_cats)
 bureau = _get_dummies(bureau, bureau_cats)
 pcb = _get_dummies(pcb, pcb_cats)
 credit_card_balance = _get_dummies(credit_card_balance, ccbal_cats)
 data = _factorize_categoricals(data, data_cats )
learn.load('stage1' )<train_model>
prev_apps_count = previous_application[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
previous_application['SK_ID_PREV'] = previous_application['SK_ID_CURR'].map(prev_apps_count['SK_ID_PREV'])
 prev_apps_avg = previous_application.groupby('SK_ID_CURR' ).mean()

 data = data.merge(right=prev_apps_avg.reset_index() , how='left', on='SK_ID_CURR')

data.head()
learn.fit_one_cycle(10, slice(7e-6,(7e-6)/10))<save_model>
bjoined = bureau_balance.merge(right=bureau[['AMT_CREDIT_SUM','SK_ID_BUREAU','SK_ID_CURR']].reset_index() , how='inner', on='SK_ID_BUREAU')
 bjoined = bjoined.merge(right=app_train[['AMT_CREDIT','SK_ID_CURR']].reset_index() , how='inner', on='SK_ID_CURR')
 bjoined['AMT_WEIGHT'] =(bjoined['AMT_CREDIT_SUM'] / bjoined['AMT_CREDIT'] ).pow (.04 ).round(1)
 bpv = pd.pivot_table(bjoined[['SK_ID_CURR','AMT_WEIGHT','YEAR']][(bjoined.AMT_WEIGHT < 1.2)&(bjoined.AMT_WEIGHT >.8)],index=['SK_ID_CURR'], columns=['AMT_WEIGHT'], aggfunc=len, fill_value=0)
 bjoined.head()
learn.save('stage-2' )<load_pretrained>
bflattened = pd.DataFrame(bpv.to_records() ).set_index(['SK_ID_CURR'])
 bflattened.columns = ['year_weight_table_' + col for col in bflattened.columns ]
bflattened.head()
path = Path(".. /input")
 SEED = 24
tfms = get_transforms(do_flip=True, max_rotate=10, max_zoom=1.3, max_lighting=0.4, max_warp=0.25, xtra_tfms=[rgb_randomize(channel=0, thresh=0.9, p=0.1),rgb_randomize(channel=2, thresh=0.9, p=0.1),rgb_randomize(channel=2, thresh=0.9, p=0.1)])
 data = ImageDataBunch.from_folder(path/"train",valid_pct=0.2, ds_tfms=tfms, size=256, bs=64, seed=SEED ).normalize(imagenet_stats )<prepare_output>
data = data.merge(right=bflattened.reset_index() , how='left', on='SK_ID_CURR')
 gc.collect()
data.head()
learn.data = data<define_search_space>
lr=7e-3<train_model>
bjoined = None
gc.collect()
bbgrouped = bflattened.groupby('SK_ID_CURR' ).sum(min_count=1)
 bbgrouped.columns = ['bbpv_sum_' + hdr.replace("('", "_" ).replace("',", "_" ).replace(")", "")\
for hdr in bbgrouped.columns]
bbgrouped.head()
data = data.merge(right=bbgrouped.reset_index() , how='left', on='SK_ID_CURR')
 data.head()

 data.head(2)
learn.fit_one_cycle(15, slice(lr))<save_model>
bflattened = None
bbgrouped = None
gc.collect()
bjoined = bureau.merge(right=app_train[['AMT_CREDIT','SK_ID_CURR']].reset_index() , how='inner', on='SK_ID_CURR')
 bjoined['AMT_WEIGHT'] = bjoined['AMT_CREDIT_SUM'] / bjoined['AMT_CREDIT']
bjoined.head()
learn.save('stage-3' )<load_pretrained>
pv = pd.pivot_table(bjoined, index='SK_ID_CURR', columns=['CREDIT_ACTIVE','YEAR'], \
values=['AMT_WEIGHT', \
'AMT_CREDIT_MAX_OVERDUE', \
'DAYS_CREDIT_ENDDATE'],aggfunc='sum')

flattened = pd.DataFrame(pv.to_records() ).set_index('SK_ID_CURR')
 flattened.columns = ['bpv_' + hdr.replace("('", "_" ).replace("',", "_" ).replace(")", "")\
for hdr in flattened.columns]
pv = None
flattened.head(10)
learn.load('stage-3' )<train_model>
bjoined = None
gc.collect()
data.columns
data.set_index('SK_ID_CURR')
 data = data.merge(right=flattened.reset_index() , how='left', on='SK_ID_CURR')
 data.head()
learn.fit_one_cycle(10, slice(1e-5, 1e-4))<load_from_csv>
bureau_avg = bureau.groupby('SK_ID_CURR' ).mean()
bureau_avg['buro_count'] = bureau[['SK_ID_BUREAU','SK_ID_CURR']].groupby('SK_ID_CURR' ).count() ['SK_ID_BUREAU']
fields = ['AMT_CREDIT_SUM','AMT_CREDIT_SUM_DEBT','CREDIT_DAY_OVERDUE','DAYS_CREDIT_ENDDATE','DAYS_CREDIT']
dates = [300,2000]
for f in fields:
lasti = 0
for i in dates:
bureau_avg['buro_wind_'+ f + str(i)] =(bureau[(bureau.DAYS_CREDIT_ENDDATE < i)&(bureau.DAYS_CREDIT_ENDDATE > lasti)])[[f,'SK_ID_CURR']].groupby('SK_ID_CURR' ).sum()
bureau_avg['buro_'+ f + str(i)] =(bureau[bureau.DAYS_CREDIT_ENDDATE < i])[[f,'SK_ID_CURR']].groupby('SK_ID_CURR' ).sum()
if lasti > 0:
bureau_avg['buro_derivitive_' + f + str(i)] = bureau_avg['buro_' + f + str(lasti)] /(bureau_avg['buro_' + f + str(i)]+ 0.0001)
 lasti = i
bureau_avg.columns = ['b_' + f_ for f_ in bureau_avg.columns]
bureau_avg.head()
data = data.merge(right=bureau_avg.reset_index() , how='left', on='SK_ID_CURR')
 for f in fields:
for i in dates:
data['b_buro_weighted_' + f + str(i)] =(data['b_buro_' + f + str(i)] / data['AMT_CREDIT_x'])
 data.head(10)
path = ".. /input"
test_df = pd.read_csv(f"{path}/sample_submission.csv")
 sub_df = pd.read_csv(f"{path}/sample_submission.csv")
 data.add_test(ImageList.from_df(test_df, path, folder="test/test"))<predict_on_test>
flattened = None
bureau_avg = None
gc.collect()
cnt_inst = installments_payments[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
installments_payments['SK_ID_PREV'] = installments_payments['SK_ID_CURR'].map(cnt_inst['SK_ID_PREV'])
 avg_inst = installments_payments.groupby('SK_ID_CURR' ).mean()

 avg_inst.columns = ['i_' + f_ for f_ in avg_inst.columns]
data = data.merge(right=avg_inst.reset_index() , how='left', on='SK_ID_CURR')
test_probs, _ = learn.get_preds(ds_type=DatasetType.Test)
 test_preds = [data.classes[pred] for pred in np.argmax(test_probs.numpy() , axis=-1)]<save_to_csv>
cnt_inst = None
avg_inst = None
gc.collect()
pcb_count = pcb[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
pcb['SK_ID_PREV'] = pcb['SK_ID_CURR'].map(pcb_count['SK_ID_PREV'])
 pcb_avg = pcb.groupby('SK_ID_CURR' ).mean()
fields = ['CNT_INSTALMENT_FUTURE']
dates = [-12,-36]
for f in fields:
lasti = 0
for i in dates:
pcb_avg['s_'+ f + str(i)] =(pcb[(pcb.MONTHS_BALANCE < i)])[[f,'SK_ID_CURR']].groupby('SK_ID_CURR' ).sum()
data = data.merge(right=pcb_avg.reset_index() , how='left', on='SK_ID_CURR')
 for f in fields:
for i in dates:
data['pcb' + '_wg_' + f + str(i)] =(data['s_' + f + str(i)] / data['AMT_CREDIT_x'])
 data.head()
sub_df = pd.read_csv(f"{path}/sample_submission.csv")
 sub_df.predicted_class = test_preds
sub_df.to_csv("submission.csv", index=False )<set_options>
pcb_avg = None
gc.collect()
nb_prevs = credit_card_balance[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
credit_card_balance['SK_ID_PREV'] = credit_card_balance['SK_ID_CURR'].map(nb_prevs['SK_ID_PREV'])
 avg_cc_bal = credit_card_balance.groupby('SK_ID_CURR' ).mean()
fields = ['AMT_BALANCE','AMT_DRAWINGS_ATM_CURRENT']
dates = [-12,-36]
for f in fields:
lasti = 0
for i in dates:
avg_cc_bal['s_'+ f + str(i)] =(credit_card_balance[(credit_card_balance.MONTHS_BALANCE < i)])[[f,'SK_ID_CURR']].groupby('SK_ID_CURR' ).sum()
avg_cc_bal.columns = ['cc_bal_' + f_ for f_ in avg_cc_bal.columns]
data = data.merge(right=avg_cc_bal.reset_index() , how='left', on='SK_ID_CURR')
 for f in ['AMT_BALANCE']:
for p in ['cc_bal_s_']:
for i in dates:
data[p + '_wg_' + f + str(i)] =(data[p + f + str(i)] / data['AMT_CREDIT_x'])
 data.head(10)
 avg_cc_bal.head()
pd.set_option("display.max_columns", 500)
 pd.set_option("display.max_rows", 200)
 plt.rcParams['figure.figsize'] = [15, 6]
sns.set_style("darkgrid" )<install_modules>
nb_prevs = None
avg_cc_bal = None
gc.collect()
!pip install pandas-profiling<load_from_csv>
ignore_features = ['SK_ID_CURR', 'is_train', 'is_test']
relevant_features = [col for col in data.columns if col not in ignore_features]
trainX = data[data['is_train'] == 1][relevant_features]
testX = data[data['is_test'] == 1][relevant_features]
x_train, x_val, y_train, y_val = train_test_split(trainX, Y, test_size=0.2, random_state=18)
 lgb_train = lgb.Dataset(data=x_train, label=y_train)
 lgb_eval = lgb.Dataset(data=x_val, label=y_val )
online_sales = pd.read_csv('/kaggle/input/uisummerschool/Online_sales.csv', sep=',')
 online_sales.head()<define_variables>
params = {'task': 'train', 'boosting_type': 'gbdt', 'objective': 'binary', 'metric': 'auc',
'learning_rate': 0.01, 'num_leaves': 48, 'num_iteration': 5000, 'verbose': 0 ,
'colsample_bytree':.8, 'subsample':.9, 'max_depth':7, 'reg_alpha':.1, 'reg_lambda':.1,
'min_split_gain':.01, 'min_child_weight':1}
model = lgb.train(params, lgb_train, valid_sets=lgb_eval, early_stopping_rounds=150, verbose_eval=200 )
<define_variables><EOS> 
preds = model.predict(testX)
 sub_lgb = pd.DataFrame()
sub_lgb['SK_ID_CURR'] = test_id
sub_lgb['TARGET'] = preds
sub_lgb.to_csv("lgb_baseline.csv", index=False)
 sub_lgb.head()