Kaggle competition: Home Credit Default Risk
pd.options.display.max_columns = 1000
pd.options.display.max_rows = 1000<load_from_csv>
PATH = ".. /input"
list_of_files = os.listdir(PATH)
 application_train = pd.read_csv(PATH+"/application_train.csv")
 application_test = pd.read_csv(PATH+"/application_test.csv")
 bureau = pd.read_csv(PATH+"/bureau.csv")
 bureau_balance = pd.read_csv(PATH+"/bureau_balance.csv")
 credit_card_balance = pd.read_csv(PATH+"/credit_card_balance.csv")
 installments_payments = pd.read_csv(PATH+"/installments_payments.csv")
 previous_application = pd.read_csv(PATH+"/previous_application.csv")
 POS_CASH_balance = pd.read_csv(PATH+"/POS_CASH_balance.csv" )
train = pd.read_csv('.. /input/train.csv', encoding='utf-8')
 test = pd.read_csv('.. /input/test.csv', encoding='utf-8', index_col='id')
 example = pd.read_csv('.. /input/random_example.csv', encoding='utf-8', index_col='id' )<train_model>
total_IDS = np.concatenate(( application_test["SK_ID_CURR"].values, application_train["SK_ID_CURR"].values))
print(len(np.unique(np.array(total_IDS)))== len(total_IDS))
print('Training data size: ', train.shape[0])
 print('Test data size: ', test.shape[0] )<feature_engineering>
POS_CASH_balance_IDS = POS_CASH_balance["SK_ID_CURR"].values
bureau_IDS = bureau["SK_ID_CURR"].values
credit_card_balance_IDS = credit_card_balance["SK_ID_CURR"].values
installments_payments_IDS = installments_payments["SK_ID_CURR"].values
previous_application_IDS = previous_application["SK_ID_CURR"].values
tot = len(total_IDS)
 print(tot)
 print(len(np.intersect1d(POS_CASH_balance_IDS, total_IDS)) /tot*100,
len(np.intersect1d(bureau_IDS, total_IDS)) /tot*100,
len(np.intersect1d(credit_card_balance_IDS, total_IDS)) /tot*100,
len(np.intersect1d(installments_payments_IDS, total_IDS)) /tot*100,
len(np.intersect1d(previous_application_IDS, total_IDS)) /tot*100 )
train = train.reset_index()
train = train.rename(columns={'index': 'id'})
 train['id'] = train['id']+test.shape[0]+1
test['type'] = '????'<concatenate>
prev = previous_application["SK_ID_PREV"].values
POS_CASH_balance_IDS_prev = POS_CASH_balance["SK_ID_PREV"].values
credit_card_balance_IDS_prev = credit_card_balance["SK_ID_PREV"].values
installments_payments_IDS_prev = installments_payments["SK_ID_PREV"].values
prev_num = len(prev)
 print(prev_num)
 print(len(np.intersect1d(POS_CASH_balance_IDS_prev, prev)) /prev_num*100,
len(np.intersect1d(credit_card_balance_IDS_prev, prev)) /prev_num*100,
len(np.intersect1d(installments_payments_IDS_prev, prev)) /prev_num*100)
combined = pd.concat([test.reset_index() , train], sort=False)
 combined.set_index('id', inplace=True )<count_missing_values>
bureau_br = np.unique(bureau["SK_ID_BUREAU"].values)
 print(len(np.intersect1d(np.unique(bureau_balance["SK_ID_BUREAU"].values), bureau_br)) /len(bureau_br)*100 )
combined.isna().sum()<define_variables>
breau_total = np.unique(np.intersect1d(bureau_IDS, total_IDS))
bureau_filtered = bureau.loc[bureau["SK_ID_CURR"].isin(breau_total)]
b = np.intersect1d(np.unique(bureau_filtered["SK_ID_BUREAU"].values), np.unique(bureau_balance["SK_ID_BUREAU"].values))
bureau_filtered = bureau_filtered.loc[bureau_filtered["SK_ID_BUREAU"].isin(b)]
len(bureau_filtered["SK_ID_CURR"].values)
 bureau_filtered
labels = ['mind','energy','nature','tactics']
label_letters = ['E','N','T','J']
alt_label_letters = ['I', 'S', 'F', 'P']<categorify>
print(len(np.unique(bureau_filtered["SK_ID_CURR"].values)) /tot*100 )
def convert_type_to_int(df):

 for i in range(len(labels)) :
df[labels[i]] = df['type'].apply\ 
(lambda x: x[i] == label_letters[i] ).astype('int')
 return df<create_dataframe>
train = application_train.drop(["TARGET"], axis = 1)
 train_target = application_train["TARGET"]
test= application_test.copy()
tr = len(application_train)
 print(all(i ==True for i in train.columns==test.columns))
data = combined<data_type_conversions>
df = pd.concat([train, test])
 del train, test, application_train, application_test
gc.collect()
def categorical_features(data):
features = [i for i in list(data.columns)if data[i].dtype == 'object']
return features
categorical = categorical_features(df)
 numerical = [i for i in df.columns if i not in categorical]
numerical.remove("SK_ID_CURR")
 IDs = df["SK_ID_CURR"]
data = convert_type_to_int(data )<string_transform>
for feature in categorical:
df[feature].fillna("unidentified")
 print(f'Transforming {feature}...')
 encoder = LabelEncoder()
encoder.fit(df[feature].astype(str))
df[feature] = encoder.transform(df[feature].astype(str))
df.head()
def separate_posts(post):
return ' '.join(post.split('|||'))<categorify>
for feats in df.columns:
df[feats] = df[feats].fillna(-1)
 df.head()
def remove_urls(post):
pattern_url = r'http[s]?://(?:[A-Za-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9A-Fa-f][0-9A-Fa-f])) +'
subs_url = r' '
return re.sub(pattern_url, subs_url, post )<define_variables>
POS_CASH_balance_G1 = POS_CASH_balance.loc[POS_CASH_balance["SK_ID_CURR"].isin(total_IDS)]
print(len(np.unique(POS_CASH_balance_G1["SK_ID_CURR"].values)))
 POS_CASH_balance_G1.head()
def remove_numbers(post):
p_numbers = '0123456789'
return ''.join([l for l in post if l not in p_numbers] )<string_transform>
np.unique(POS_CASH_balance_G1["NAME_CONTRACT_STATUS"].values)
 POS_CASH_balance_G1_num =(POS_CASH_balance_G1.groupby("SK_ID_CURR", as_index=False ).mean())
 nb = POS_CASH_balance_G1[["SK_ID_CURR", "NAME_CONTRACT_STATUS"]].groupby("SK_ID_CURR", as_index = False ).count()
nb["num_in_POS_CASH"] = nb["NAME_CONTRACT_STATUS"]
df = df.merge(POS_CASH_balance_G1_num.drop("SK_ID_PREV", axis = 1), on='SK_ID_CURR', how='left' ).fillna(-1)
 df = df.merge(nb.drop("NAME_CONTRACT_STATUS", axis = 1), on='SK_ID_CURR', how='left' ).fillna(-1)
 del nb, POS_CASH_balance_G1_num, POS_CASH_balance_G1
gc.collect()
df.head()
def remove_punctuation(post):
return ''.join([l for l in post if l not in string.punctuation] )<define_variables>
bureau_G1 = bureau.drop(["SK_ID_BUREAU"], axis = 1 ).loc[bureau["SK_ID_CURR"].isin(total_IDS)]
print(len(np.unique(bureau_G1["SK_ID_CURR"].values)))
 bureau_G1.head()
def remove_strange(post):
return post.replace("‘", '' ).replace("’", '' ).replace("'", '' )<string_transform>
bureau_G1_num =(bureau_G1.groupby("SK_ID_CURR", as_index=False ).mean())
 nb = bureau_G1[["SK_ID_CURR", "CREDIT_ACTIVE"]].groupby("SK_ID_CURR", as_index = False ).count()
nb["num_in_bureau"] = nb["CREDIT_ACTIVE"]
df = df.merge(bureau_G1_num, on='SK_ID_CURR', how='left' ).fillna(-1)
 df = df.merge(nb.drop("CREDIT_ACTIVE", axis=1), on='SK_ID_CURR', how='left' ).fillna(-1)
 del nb, bureau_G1_num, bureau_G1
gc.collect()
df.head()
def lower_case(post):
return post.lower()<drop_column>
credit_card_balance_G1 = credit_card_balance.drop(["SK_ID_PREV"], axis = 1 ).loc[credit_card_balance["SK_ID_CURR"].isin(total_IDS)]
print(len(np.unique(credit_card_balance_G1["SK_ID_CURR"].values)))
 credit_card_balance_G1.head()
def remove_extra_spaces(post):
return re.sub('\s+', ' ', post )<string_transform>
credit_card_balance_G1_num =(credit_card_balance_G1.groupby("SK_ID_CURR", as_index=False ).mean())
 nb = credit_card_balance_G1[["SK_ID_CURR", "NAME_CONTRACT_STATUS"]].groupby("SK_ID_CURR", as_index = False ).count()
nb["num_in_credit_card"] = nb["NAME_CONTRACT_STATUS"]
df = df.merge(credit_card_balance_G1_num, on='SK_ID_CURR', how='left' ).fillna(-1)
 df = df.merge(nb.drop("NAME_CONTRACT_STATUS", axis=1), on='SK_ID_CURR', how='left' ).fillna(-1)
 del nb, credit_card_balance_G1_num, credit_card_balance_G1
gc.collect()
df.head()
def tokenizer_func(post):
return TreebankWordTokenizer().tokenize(post )<define_variables>
installments_payments_G1 = installments_payments.drop(["SK_ID_PREV"], axis = 1 ).loc[installments_payments["SK_ID_CURR"].isin(total_IDS)]
print(len(np.unique(installments_payments_G1["SK_ID_CURR"].values)))
 installments_payments_G1.head()
custom_stop_words = [remove_punctuation(word)for word in stopwords.words('english')[39:]]<drop_column>
installments_payments_G1_num =(installments_payments_G1.groupby("SK_ID_CURR", as_index=False ).mean())
 nb = installments_payments_G1[["SK_ID_CURR", "NUM_INSTALMENT_VERSION"]].groupby("SK_ID_CURR", as_index = False ).count()
nb["num_in_install_pay"] = nb["NUM_INSTALMENT_VERSION"]
df = df.merge(installments_payments_G1_num, on='SK_ID_CURR', how='left' ).fillna(-1)
 df = df.merge(nb.drop("NUM_INSTALMENT_VERSION", axis=1), on='SK_ID_CURR', how='left' ).fillna(-1)
 del nb, installments_payments_G1_num, installments_payments_G1
gc.collect()
df.head()
def remove_stop_words(tokens, stop_words=custom_stop_words):
return [token for token in tokens if(token not in stop_words)and(len(token)<15)]<compute_test_metric>
previous_application_G1 = previous_application.drop(["SK_ID_PREV"], axis = 1 ).loc[previous_application["SK_ID_CURR"].isin(total_IDS)]
print(len(np.unique(previous_application_G1["SK_ID_CURR"].values)))
 previous_application_G1.head()
def lem_func(words, lemma = WordNetLemmatizer()):
return [lemma.lemmatize(word)for word in words if word not in custom_stop_words]<string_transform>
previous_application_G1_num =(previous_application_G1.groupby("SK_ID_CURR", as_index=False ).mean())
 nb = previous_application_G1[["SK_ID_CURR", "NAME_CONTRACT_TYPE"]].groupby("SK_ID_CURR", as_index = False ).count()
nb["num_in_previous_app"] = nb["NAME_CONTRACT_TYPE"]
df = df.merge(previous_application_G1_num, on='SK_ID_CURR', how='left' ).fillna(-1)
 df = df.merge(nb.drop("NAME_CONTRACT_TYPE", axis=1), on='SK_ID_CURR', how='left' ).fillna(-1)
 del nb, previous_application_G1_num, previous_application_G1
gc.collect()
df.head()
def join_tokens(post):
return ' '.join(post )<drop_column>
train_X = df[:tr].drop("SK_ID_CURR", axis = 1)
 test_X = df[tr:].drop("SK_ID_CURR", axis = 1)
 train_X["TARGET"] = train_target
y = train_target
folds = KFold(n_splits=5, shuffle=True, random_state=42)
 oof_preds = np.zeros(train_X.shape[0])
 sub_preds = np.zeros(test_X.shape[0])
 feats = [f for f in train_X.columns if f not in ['SK_ID_CURR','TARGET']]
def clean_data(df, col, cleaning_funcs):

 df['posts_processed'] = df[col].apply(separate_posts)
 for func in cleaning_funcs:
df['posts_processed'] = df['posts_processed'].apply(func)
 return df<count_values>
for n_fold,(trn_idx, val_idx)in enumerate(folds.split(train_X)) :
trn_x, trn_y = train_X[feats].iloc[trn_idx], train_X.iloc[trn_idx]['TARGET']
val_x, val_y = train_X[feats].iloc[val_idx], train_X.iloc[val_idx]['TARGET']
clf = LGBMClassifier(
 n_estimators=10000,
learning_rate=0.01,
num_leaves=30,
colsample_bytree=.8,
subsample=.9,
max_depth=7,
reg_alpha=.1,
reg_lambda=.1,
min_split_gain=.01,
min_child_weight=100,
silent=-1,
verbose=-1, 
)
 clf.fit(trn_x, trn_y,
eval_set= [(trn_x, trn_y),(val_x, val_y)],
eval_metric='auc', verbose=100, early_stopping_rounds=100 
)
 oof_preds[val_idx] = clf.predict_proba(val_x, num_iteration=clf.best_iteration_)[:, 1]
sub_preds += clf.predict_proba(test_X[feats], num_iteration=clf.best_iteration_)[:, 1] / folds.n_splits
print('Fold %2d AUC : %.6f' %(n_fold + 1, roc_auc_score(val_y, oof_preds[val_idx])))
 del trn_x, trn_y, val_x, val_y
gc.collect()
for label in labels:
train['naive_'+label] = train[label].value_counts().index[0]<compute_test_metric>
submission = pd.read_csv(".. /input/sample_submission.csv")
 submission['TARGET'] = sub_preds
submission.to_csv("baseline2.csv", index=False)
 submission.head()