Kaggle competition: Home Credit Default Risk
<SOS> metric: AUC Kaggle data source: home-credit-default-risk<predict_on_test>
pd.set_option('display.max_columns', None)
 %matplotlib inline
color = sns.color_palette()
warnings.filterwarnings("ignore" )
def print_accuracy(sn):
Y_pred_train = sn.predict(X_scaled_train)
 Y_pred_binarised_train =(Y_pred_train >= 0.5 ).astype("int" ).ravel()
accuracy_train = accuracy_score(Y_pred_binarised_train, Y_train)
 print("Train Accuracy : ", accuracy_train)
 print("-"*50 )<compute_test_metric>
application_train = pd.read_csv('.. /input/application_train.csv')
 application_test= pd.read_csv('.. /input/application_test.csv')
 bureau = pd.read_csv('.. /input/bureau.csv')
 bureau_balance = pd.read_csv('.. /input/bureau_balance.csv')
 POS_CASH_balance = pd.read_csv('.. /input/POS_CASH_balance.csv')
 credit_card_balance = pd.read_csv('.. /input/credit_card_balance.csv')
 previous_application = pd.read_csv('.. /input/previous_application.csv')
 installments_payments = pd.read_csv('.. /input/installments_payments.csv' )
print_accuracy(sn_ce )<save_to_csv>
application_train = pd.read_csv('.. /input/application_train.csv')
 application_test= pd.read_csv('.. /input/application_test.csv' )
Y_pred_test = sn_ce.predict(X_scaled_test)
 Y_pred_binarised_test =(Y_pred_test >= 0.5 ).astype("int" ).ravel()
submission = {}
submission['ImageId'] = ID_test
submission['Class'] = Y_pred_binarised_test
submission = pd.DataFrame(submission)
 submission = submission[['ImageId', 'Class']]
submission = submission.sort_values(['ImageId'])
 submission.to_csv("submisision.csv", index=False )<import_modules>
def feature_type_split(data, special_list=[]):
cat_list = []
dis_num_list = []
num_list = []
for i in data.columns.tolist() :
if data[i].dtype == 'object':
cat_list.append(i)
 elif data[i].nunique() < 25:
dis_num_list.append(i)
 elif i in special_list:
dis_num_list.append(i)
 else:
num_list.append(i)
 return cat_list, dis_num_list, num_list
cat_list, dis_num_list, num_list = feature_type_split(application_train, special_list=['AMT_REQ_CREDIT_BUREAU_YEAR'] )
import pandas as pd
from math import floor<load_from_csv>
application_train['TERM'] = application_train.AMT_CREDIT / application_train.AMT_ANNUITY
application_test['TERM'] = application_test.AMT_CREDIT / application_test.AMT_ANNUITY
association = pd.read_csv('.. /input/market-basket-id-ndsc-2020/association_order.csv')
 rules = pd.read_csv('.. /input/market-basket-id-ndsc-2020/rules.csv' )<groupby>
application_train['OVER_EXPECT_CREDIT'] =(application_train.AMT_CREDIT > application_train.AMT_GOODS_PRICE ).map({False:0, True:1})
 application_test['OVER_EXPECT_CREDIT'] =(application_test.AMT_CREDIT > application_test.AMT_GOODS_PRICE ).map({False:0, True:1} )
group_itemid = association.groupby('itemid')['orderid'].apply(set )<concatenate>
application_train['MEAN_BUILDING_SCORE_AVG'] = application_train.iloc[:, 44:58].mean(skipna=True, axis=1)
 application_train['TOTAL_BUILDING_SCORE_AVG'] = application_train.iloc[:, 44:58].sum(skipna=True, axis=1)
 application_test['MEAN_BUILDING_SCORE_AVG'] = application_test.iloc[:, 44:58].mean(skipna=True, axis=1)
 application_test['TOTAL_BUILDING_SCORE_AVG'] = application_test.iloc[:, 44:58].sum(skipna=True, axis=1 )
def confidence(data):
data = [i.split('&')for i in data.split('>')]
denominator = group_itemid[int(data[0][0])]
if len(data[0])> 1:
denominator = denominator.intersection(group_itemid[int(data[0][1])])
 numerator = set(denominator)
 for i in data[1]:
numerator = numerator.intersection(group_itemid[int(i)])
 return floor(len(numerator)/len(denominator)*1000 )<feature_engineering>
application_train['FLAG_DOCUMENT_TOTAL'] = application_train.iloc[:, 96:116].sum(axis=1)
 application_test['FLAG_DOCUMENT_TOTAL'] = application_test.iloc[:, 96:116].sum(axis=1 )
rules['confidence'] = rules['rule'].apply(confidence )<save_to_csv>
application_train['AMT_REQ_CREDIT_BUREAU_TOTAL'] = application_train.iloc[:, 116:122].sum(axis=1)
 application_test['AMT_REQ_CREDIT_BUREAU_TOTAL'] = application_test.iloc[:, 116:122].sum(axis=1 )
rules.to_csv('submission.csv',index=False )<load_from_csv>
application_train['BIRTH_EMPLOTED_INTERVEL'] = application_train.DAYS_EMPLOYED - application_train.DAYS_BIRTH
application_test['BIRTH_EMPLOTED_INTERVEL'] = application_test.DAYS_EMPLOYED - application_test.DAYS_BIRTH
train = pd.read_csv('.. /input/tweet-sentiment-analysis/train.csv')
 test = pd.read_csv('.. /input/tweet-sentiment-analysis/test.csv' )<define_variables>
application_train['BIRTH_REGISTRATION_INTERVEL'] = application_train.DAYS_REGISTRATION - application_train.DAYS_BIRTH
application_test['BIRTH_REGISTRATION_INTERVEL'] = application_test.DAYS_REGISTRATION - application_test.DAYS_BIRTH
print(f"There are {len(train)} tweets in the training data.")
 positive = sum(train.target)/len(train)
 print(f"{positive:.2f}% of the tweets are positive.")
 avg_len = sum([len(text)for text in train.text])/len(train)
 print(f"The average length of the tweets is {avg_len:.1f} characters." )<feature_engineering>
application_train['MEAN_BUILDING_SCORE_AVG'] = application_train.iloc[:, 44:58].mean(skipna=True, axis=1)
 application_train['TOTAL_BUILDING_SCORE_AVG'] = application_train.iloc[:, 44:58].sum(skipna=True, axis=1)
 application_test['MEAN_BUILDING_SCORE_AVG'] = application_test.iloc[:, 44:58].mean(skipna=True, axis=1)
 application_test['TOTAL_BUILDING_SCORE_AVG'] = application_test.iloc[:, 44:58].sum(skipna=True, axis=1)
 application_train['INCOME_PER_FAMILY_MEMBER'] = application_train.AMT_INCOME_TOTAL / application_train.CNT_FAM_MEMBERS
application_test['INCOME_PER_FAMILY_MEMBER'] = application_test.AMT_INCOME_TOTAL / application_test.CNT_FAM_MEMBERS
application_train['SEASON_REMAINING'] = application_train.AMT_INCOME_TOTAL/4 - application_train.AMT_ANNUITY
application_test['SEASON_REMAINING'] = application_test.AMT_INCOME_TOTAL/4 - application_test.AMT_ANNUITY
application_train['RATIO_INCOME_GOODS'] = application_train.AMT_INCOME_TOTAL - application_train.AMT_GOODS_PRICE
application_test['RATIO_INCOME_GOODS'] = application_test.AMT_INCOME_TOTAL - application_test.AMT_GOODS_PRICE
application_train['CHILDREN_RATIO'] = application_train['CNT_CHILDREN'] / application_train['CNT_FAM_MEMBERS']
application_test['CHILDREN_RATIO'] = application_test['CNT_CHILDREN'] / application_test['CNT_FAM_MEMBERS']
def label_encoder(input_df, encoder_dict=None):

 categorical_feats = input_df.columns[input_df.dtypes == 'object']
for feat in categorical_feats:
encoder = LabelEncoder()
input_df[feat] = encoder.fit_transform(input_df[feat].fillna('NULL'))
return input_df, categorical_feats.tolist() , encoder_dict
application_train, categorical_feats, encoder_dict = label_encoder(application_train)
 application_test, categorical_feats, encoder_dict = label_encoder(application_test )
count_vect = CountVectorizer()
count_vect.fit(train['text'])
 bow_train = count_vect.transform(train['text'])
 bow_test = count_vect.transform(test['text'] )<categorify>
import lightgbm as lgb
from lightgbm import LGBMClassifier
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import StratifiedKFold
count_vect = CountVectorizer(max_features=15000)
 count_vect.fit(train['text'])
 bow_train = count_vect.transform(train['text'])
 bow_test = count_vect.transform(test['text'] )<train_model>
X = application_train.drop(['SK_ID_CURR', 'TARGET'], axis=1)
 y = application_train.TARGET
X_pred = application_test.drop(['SK_ID_CURR'], axis=1 )
model = LogisticRegression(max_iter=1000 ).fit(bow_train, train.target )<predict_on_test>
folds = StratifiedKFold(n_splits=5,random_state=6)
 oof_preds = np.zeros(X.shape[0])
 sub_preds = np.zeros(X_pred.shape[0] )
predictions = model.predict_proba(bow_test)
 predictions = [probs[1] for probs in predictions]<load_from_csv>
start = time.time()
valid_score = 0
for n_fold,(trn_idx, val_idx)in enumerate(folds.split(X, y)) :
trn_x, trn_y = X.iloc[trn_idx], y[trn_idx]
val_x, val_y = X.iloc[val_idx], y[val_idx]
train_data = lgb.Dataset(data=trn_x, label=trn_y,categorical_feature=categorical_feats)
 valid_data = lgb.Dataset(data=val_x, label=val_y)
 param = {'application':'binary','num_iterations':4000, 'learning_rate':0.05, 'num_leaves':24, 'feature_fraction':0.8, 'bagging_fraction':0.9,
'lambda_l1':0.1, 'lambda_l2':0.1, 'min_split_gain':0.01, 'early_stopping_round':100, 'max_depth':7, 'min_child_weight':40, 'metric':'auc'}
lgb_es_model = lgb.train(param, train_data, valid_sets=[train_data, valid_data], verbose_eval=100, categorical_feature=categorical_feats)
 oof_preds[val_idx] = lgb_es_model.predict(val_x, num_iteration=lgb_es_model.best_iteration)
 sub_preds += lgb_es_model.predict(X_pred, num_iteration=lgb_es_model.best_iteration)/ folds.n_splits
print('Fold %2d AUC : %.6f' %(n_fold + 1, roc_auc_score(val_y, oof_preds[val_idx])))
 valid_score += roc_auc_score(val_y, oof_preds[val_idx])
 print('valid score:', str(round(valid_score/folds.n_splits,4)))
 end = time.time()
print('training time:', str(round(( end - start)/60)) , 'mins' )
<save_to_csv><EOS> 
application_test= pd.read_csv('.. /input/application_test.csv')
 output = pd.DataFrame({'SK_ID_CURR':application_test.SK_ID_CURR, 'TARGET': sub_preds})
 output.to_csv('only_application_pred.csv', index=False )