Kaggle competition: Home Credit Default Risk
<SOS> metric: AUC Kaggle data source: home-credit-default-risk<save_to_csv>
import numpy as np
import pandas as pd
import joblib
import gc
submission['target'] = test_df['target']
submission.to_csv("sample_submission.csv", index=False)
 submission.head()<install_modules>
test = pd.read_csv('.. /input/home-credit-default-risk/application_test.csv')
 test.set_index(['SK_ID_CURR'], inplace=True)
 test.shape
!pip install --user catboost 
<load_from_csv>
preprocessor = joblib.load('.. /input/wk6-default/wk6default_preprocessor.joblib')
 LGBM_model = joblib.load('.. /input/wk6-default/wk6_LGBM_default_model.joblib' )
train = pd.read_csv('train.csv')
 test = pd.read_csv('test.csv' )<count_missing_values>
bureau_bal = pd.read_csv('.. /input/home-credit-default-risk/bureau_balance.csv')
 bureau = pd.read_csv('.. /input/home-credit-default-risk/bureau.csv')
 bb = pd.merge(bureau, bureau_bal, on = 'SK_ID_BUREAU', how = 'left')
 bb['REMAIN_CRED'] = bb['AMT_CREDIT_SUM'] - bb['AMT_CREDIT_SUM_DEBT'] - bb['AMT_CREDIT_SUM_LIMIT']
bb['AC_RATIO'] = bb['AMT_ANNUITY'] / bb['AMT_CREDIT_SUM']
bb.columns = ['BU_'+column if column !=('SK_ID_CURR')
 else column for column in bb.columns]
bur_cat = pd.get_dummies(bb.select_dtypes('object'))
bur_cat['SK_ID_CURR'] = bb['SK_ID_CURR']
bur_cat = bur_cat.groupby(by = ['SK_ID_CURR'] ).agg(['mean'])
 bur_num = bb.groupby(by = ['SK_ID_CURR'] ).agg(['max', 'mean', 'sum'] ).astype('float32')
 bureau_rev = bur_cat.merge(bur_num, on = ['SK_ID_CURR'], how = 'left')
 test = test.merge(bureau_rev, on = ['SK_ID_CURR'], how = 'left')
 del bur_cat
del bur_num
del bureau
del bureau_bal
gc.collect()
print(train.isnull().sum())
 test.isnull().sum()<prepare_x_and_y>
cc_bal = pd.read_csv('.. /input/home-credit-default-risk/credit_card_balance.csv')
 cc_bal['DRAW_RATIO'] = cc_bal['AMT_DRAWINGS_CURRENT'] / cc_bal['CNT_DRAWINGS_CURRENT']
cc_bal['RECEIVE_RATIO'] = cc_bal['AMT_RECIVABLE'] / cc_bal['AMT_RECEIVABLE_PRINCIPAL']
cc_bal['RECEIVE_PER'] = cc_bal['AMT_RECIVABLE'] / cc_bal['AMT_TOTAL_RECEIVABLE']
cc_bal.columns = ['CC_'+ column if column !='SK_ID_CURR'
else column for column in cc_bal.columns]
cc_cat = pd.get_dummies(cc_bal.select_dtypes('object'))
cc_cat['SK_ID_CURR'] = cc_bal['SK_ID_CURR']
cc_cat = cc_cat.groupby(by = ['SK_ID_CURR'] ).mean()
cc_num = cc_bal.groupby(by = ['SK_ID_CURR'] ).agg(['max', 'mean', 'sum'] ).astype('float32')
 test = test.merge(cc_cat, on = ['SK_ID_CURR'], how = 'left')
 test = test.merge(cc_num, on = ['SK_ID_CURR'], how = 'left')
 del cc_bal
del cc_cat
del cc_num
gc.collect()
df = train.drop(columns=['ACTION'])
 train_x = train.drop(columns=['ACTION'])
 train_y = train['ACTION']
test_x = test.drop(columns=['id'])
<split>
install = pd.read_csv('.. /input/home-credit-default-risk/installments_payments.csv')
 install['PAY_PERCENT'] = install['AMT_INSTALMENT'] / install['AMT_PAYMENT']
install['PAY_DIFF'] = install['AMT_INSTALMENT'] - install['AMT_PAYMENT']
install['DPD'] = install['DAYS_ENTRY_PAYMENT'] - install['DAYS_INSTALMENT']
install['DPD'] = install['DPD'].apply(lambda x: x if x>0 else 0)
 install['DBD'] = install['DAYS_INSTALMENT'] - install['DAYS_ENTRY_PAYMENT']
install['DBD'] = install['DBD'].apply(lambda x: x if x>0 else 0)
 install.columns = ['IP_'+ column if column !='SK_ID_CURR'
else column for column in install.columns]
inst_num = install.groupby(by = ['SK_ID_CURR'] ).agg(['max', 'mean'] ).astype('float32')
 test = test.merge(inst_num, on = 'SK_ID_CURR', how='left')
 del install
del inst_num
gc.collect()
X_train, X_test, y_train, y_test = train_test_split(train_x, train_y )<choose_model_class>
pos = pd.read_csv('.. /input/home-credit-default-risk/POS_CASH_balance.csv')
 pos.columns = ['PC_'+ column if column !='SK_ID_CURR'
else column for column in pos.columns]
pos_num = pos.groupby(by = ['SK_ID_CURR'] ).agg(['max', 'mean', 'sum'] ).astype('float32')
 test = test.merge(pos_num, on = ['SK_ID_CURR'], how = 'left')
 del pos
del pos_num
gc.collect()
model = lm.LogisticRegression()
model.fit(X_train, y_train)
 predictions = model.predict_proba(test_x)
<prepare_x_and_y>
prev = pd.read_csv('.. /input/home-credit-default-risk/previous_application.csv')
 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)
 prev['AppCred_RATIO'] = prev['AMT_APPLICATION'] /(prev['AMT_CREDIT'] + 1)
 prev['AppGoods_RATIO'] = prev['AMT_APPLICATION'] /(prev['AMT_GOODS_PRICE'] + 1)
 prev['AnnCred_RATIO'] = prev['AMT_ANNUITY'] /(prev['AMT_CREDIT'] + 1)
 prev['CredGoods_RATIO'] = prev['AMT_CREDIT'] /(prev['AMT_GOODS_PRICE'] + 1)
 def calc_rate(row):
return np.rate(row['CNT_PAYMENT'], -row['AMT_ANNUITY'], row['AMT_CREDIT'], 0, guess = 0.05, maxiter = 10)
 prev['CALC_RATE'] = prev.apply(calc_rate, axis=1)
 p_dels = ['RATE_INTEREST_PRIMARY','RATE_INTEREST_PRIVILEGED']
prev = prev.drop(prev[p_dels], axis = 1)
 prev.columns = ['PR_'+ column if column != 'SK_ID_CURR'
else column for column in prev.columns]
prev_cat = pd.get_dummies(prev.select_dtypes('object'))
prev_cat['SK_ID_CURR'] = prev['SK_ID_CURR']
prev_cat = prev_cat.groupby(by = ['SK_ID_CURR'] ).agg(['mean'])
 prev_num = prev.groupby(by = ['SK_ID_CURR'] ).agg(['max', 'mean', 'sum'] ).astype('float32')
 prev_rev = prev_num.merge(prev_cat, on = ['SK_ID_CURR'], how = 'left')
 test = test.merge(prev_rev, on = ['SK_ID_CURR'], how = 'left')
 del prev_rev
del prev_cat
del prev_num
gc.collect()
print(f"{X_train.shape}, {X_test.shape}, {test.drop(columns=['id'] ).shape}")
 test_x = test.drop(columns=['id'])
 X_train.reset_index(drop=True, inplace=True)
 y_train.reset_index(drop=True, inplace=True )<predict_on_test>
test['DAYS_EMPLOYED'].replace(365243, np.nan, inplace = True)
 test['AGE'] = test['DAYS_BIRTH'] / - 365
test['AVG_EXT'] = test.iloc[:, 41:44].sum(axis=1)/(3- test.iloc[:,41:44].isnull().sum(axis=1))
test.EXT_SOURCE_1.fillna(test.AVG_EXT, inplace=True)
 test.EXT_SOURCE_2.fillna(test.AVG_EXT, inplace=True)
 test.EXT_SOURCE_3.fillna(test.AVG_EXT, inplace=True )
model = naive_bayes.CategoricalNB() 
<train_model>
test['EmpAge_RATIO'] = test['DAYS_EMPLOYED'] / test['AGE']
test['CredInc_RATIO'] = test['AMT_CREDIT'] / test['AMT_INCOME_TOTAL']
test['AnnInc_RATIO'] = test['AMT_ANNUITY'] / test['AMT_INCOME_TOTAL']
test['AnnCred_RATIO'] = test['AMT_ANNUITY'] /(test['AMT_CREDIT'] + 1)
 test['CredGoods_RATIO'] = test['AMT_CREDIT'] /(test['AMT_GOODS_PRICE'] + 1)
 test['AVG_EXT_INCOME'] = test['AMT_INCOME_TOTAL'] * test['AVG_EXT']
test['AVG_EXT_GOODS'] = test['AMT_GOODS_PRICE'] * test['AVG_EXT']
model = DecisionTreeClassifier(max_depth=30)
 clf = model.fit(X_train, y_train)
 print(f'{clf.score(X_test,y_test)}')
 predictions = clf.predict(test_x)
<train_model>
dels = ['APARTMENTS_MODE', 'BASEMENTAREA_MODE', 'YEARS_BEGINEXPLUATATION_MODE',
'YEARS_BUILD_MODE', 'COMMONAREA_MODE', 'ELEVATORS_MODE', 'ENTRANCES_MODE',
'FLOORSMAX_MODE', 'FLOORSMIN_MODE', 'LANDAREA_MODE', 'LIVINGAPARTMENTS_MODE',
'LIVINGAREA_MODE', 'NONLIVINGAPARTMENTS_MODE', 'NONLIVINGAREA_MODE',
'APARTMENTS_MEDI', 'BASEMENTAREA_MEDI', 'YEARS_BEGINEXPLUATATION_MEDI',
'YEARS_BUILD_MEDI', 'COMMONAREA_MEDI', 'ELEVATORS_MEDI', 'ENTRANCES_MEDI',
'FLOORSMAX_MEDI', 'FLOORSMIN_MEDI', 'LANDAREA_MEDI', 'LIVINGAPARTMENTS_MEDI',
'LIVINGAREA_MEDI', 'NONLIVINGAPARTMENTS_MEDI', 'NONLIVINGAREA_MEDI',
'FONDKAPREMONT_MODE', 'HOUSETYPE_MODE', 'TOTALAREA_MODE',
'WALLSMATERIAL_MODE', 'EMERGENCYSTATE_MODE', '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', 'DAYS_BIRTH', 'LIVINGAPARTMENTS_AVG',
'LIVINGAREA_AVG', 'CNT_FAM_MEMBERS', 'OBS_30_CNT_SOCIAL_CIRCLE',
'OBS_60_CNT_SOCIAL_CIRCLE', 'ELEVATORS_AVG', 'AVG_EXT']
test = test.drop(test[dels], axis =1)
 gc.collect()
model = RandomForestClassifier(n_estimators = 300)
 clf = model.fit(X_train, y_train)
 print(f'{clf.score(X_test, y_test)}')
 predictions = clf.predict_proba(test_x)
<define_variables>
test = test.replace([np.inf, -np.inf], np.nan )
feature_names = dict()
for column, name in enumerate(train):
if column == 0:
continue
feature_names[column - 1] = name
dataset_dir = './amazon'
create_cd(
 label=0,
cat_features=list(range(1, train.columns.shape[0])) ,
feature_names=feature_names,
output_path=os.path.join(dataset_dir, 'train.cd')
 )<prepare_x_and_y>
test_pred = LGBM_model.predict_proba(X_test)
 print(test_pred.shape)
 print(test_pred[:5] )
X = train.drop(columns=['ACTION'])
 y = train.ACTION
cat_features = list(range(0, X.shape[1]))
print(cat_features )<define_variables>
submission = pd.read_csv('.. /input/home-credit-default-risk/sample_submission.csv')
 submission.head(10 )
pool1 = Pool(data=X, label=y, cat_features=cat_features)
 pool2 = Pool(
 data=os.path.join('/kaggle/input/amazon-employee-access-challenge/', 'train.csv'),
delimiter=',',
column_description=os.path.join(dataset_dir, 'train.cd'),
has_header=True 
)
 pool3 = Pool(data=X, cat_features=cat_features)
 X_prepared = X.values.astype(str ).astype(object)
 pool4 = Pool(
 data=FeaturesData(
 cat_feature_data=X_prepared,
cat_feature_names=list(X)
),
label=y.values 
)
 print('Dataset shape')
 print('dataset 1:' + str(pool1.shape)+
'
dataset 2:' + str(pool2.shape)+
'
dataset 3:' + str(pool3.shape)+
'
dataset 4: ' + str(pool4.shape))
print('
')
 print('Column names')
 print('dataset 1:')
 print(pool1.get_feature_names())
 print('
dataset 2:')
 print(pool2.get_feature_names())
 print('
dataset 3:')
 print(pool3.get_feature_names())
 print('
dataset 4:')
 print(pool4.get_feature_names() )<split>
submission.TARGET = test_pred[:,1]
submission.head(10 )
<train_model><EOS> 
submission.to_csv('default_submission_wk06.csv', index=False, header = True )