Kaggle competition: Jigsaw Multilingual Toxic Comment Classification
<SOS> metric: AUC Kaggle data source: jigsaw-multilingual-toxic-comment-classification<categorify>
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import os
POS_CASH_balance = replace_XNA_XAP(POS_CASH_balance)
 pos_bin =[]
POS_CASH_balance,pos_bin = binary_encoding(POS_CASH_balance,columns=['NAME_CONTRACT_STATUS'])
 POS_CASH_balance,pos_cat = one_hot_encoding(POS_CASH_balance,columns=['NAME_CONTRACT_STATUS'], nan_as_category=True)
 pos_aggregate ={
'MONTHS_BALANCE':['mean','min','max'],
'CNT_INSTALMENT':['mean','min','max'],
'CNT_INSTALMENT_FUTURE':['mean','min','max'],
'SK_DPD':['min','max','mean'],
'SK_DPD_DEF':['min','max','mean']
}
for col in pos_cat+pos_bin:
pos_aggregate[col] =['sum','mean']
pos_agg = POS_CASH_balance.groupby('SK_ID_CURR' ).agg(pos_aggregate)
 pos_agg.columns = pd.Index(['POS_'+ e[0]+ '_'+ e[1].upper() for e in pos_agg.columns.tolist() ])
 pos_agg['POS_COUNT'] = POS_CASH_balance.groupby('SK_ID_CURR' ).size()
del POS_CASH_balance
pos_agg.head(2 )<set_options>
sub_path = ".. /input/jigsaw-multilingual-submission-files"
all_files = os.listdir(sub_path)
 all_files
gc.collect()
reduce_memory_usage(pos_agg )<feature_engineering>
all_files = [f for f in all_files if '0.9361' not in f and '0.9383' not in f and '0.9422' not in f]
all_files
credit_card_balance['AMT_CREDIT_LIMIT_ACTUAL'] = np.sqrt(credit_card_balance['AMT_CREDIT_LIMIT_ACTUAL'] )<feature_engineering>
outs = [pd.read_csv(os.path.join(sub_path, f), index_col=0)for f in all_files]
concat_sub = pd.concat(outs, axis=1)
 cols = list(map(lambda x: "jigsaw" + str(x), range(len(concat_sub.columns))))
concat_sub.columns = cols
concat_sub.reset_index(inplace=True)
 concat_sub.head()
ncol = concat_sub.shape[1]
credit_card_balance['CNT_DRAWINGS_CURRENT'] = np.sqrt(credit_card_balance['CNT_DRAWINGS_CURRENT'] )<data_type_conversions>
concat_sub['jigsaw_max'] = concat_sub.iloc[:, 1:ncol].max(axis=1)
 concat_sub['jigsaw_min'] = concat_sub.iloc[:, 1:ncol].min(axis=1)
 concat_sub['jigsaw_mean'] = concat_sub.iloc[:, 1:ncol].mean(axis=1)
 concat_sub['jigsaw_median'] = concat_sub.iloc[:, 1:ncol].median(axis=1 )
credit_card_balance['CNT_DRAWINGS_OTHER_CURRENT'] = credit_card_balance['CNT_DRAWINGS_OTHER_CURRENT'].astype('object' )<feature_engineering>
cutoff_lo = 0.3
cutoff_hi = 0.7
credit_card_balance['CNT_DRAWINGS_POS_CURRENT'] = np.sqrt(credit_card_balance['CNT_DRAWINGS_POS_CURRENT'] )<feature_engineering>
concat_sub['toxic'] = concat_sub['jigsaw_mean']
concat_sub[['id', 'toxic']].to_csv('submission1.csv',
index=False, float_format='%.6f' )
credit_card_balance['CNT_INS'] = np.sqrt(credit_card_balance['CNT_INSTALMENT_MATURE_CUM'] )<data_type_conversions>
concat_sub['toxic'] = concat_sub['jigsaw_median']
concat_sub[['id', 'toxic']].to_csv('submission2.csv',
index=False, float_format='%.6f' )
credit_card_balance['NAME_CONTRACT_STATUS'] = credit_card_balance['NAME_CONTRACT_STATUS'].astype('object' )<categorify>
concat_sub['toxic'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1), 1,
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
0, concat_sub['jigsaw_median']))
concat_sub[['id', 'toxic']].to_csv('submission3.csv',
index=False, float_format='%.6f' )
credit_card_balance = replace_XNA_XAP(credit_card_balance)
 credit_object_col = credit_card_balance.select_dtypes('object' ).columns
credit_bin =[]
credit_card_balance,credit_bin = binary_encoding(credit_card_balance,credit_object_col)
 credit_card_balance,credit_cat = one_hot_encoding(credit_card_balance,credit_object_col, nan_as_category=False)
 credit_aggregation = {
'MONTHS_BALANCE':['mean','min','max'],
'AMT_BALANCE':['mean','min'],
'AMT_CREDIT_LIMIT_ACTUAL':['mean','min'],
'AMT_DRAWINGS_ATM_CURRENT':['mean','min','max'],
'AMT_DRAWINGS_CURRENT':['mean','min','max'],
'AMT_DRAWINGS_OTHER_CURRENT':['mean','min','max'],
'AMT_DRAWINGS_POS_CURRENT':['mean','min','max'],
'AMT_INST_MIN_REGULARITY':['mean','min','max'],
'AMT_PAYMENT_CURRENT':['mean','min','max'],
'AMT_PAYMENT_TOTAL_CURRENT':['mean','min','max'],
'AMT_RECEIVABLE_PRINCIPAL':['mean','min','max'],
'AMT_RECIVABLE':['mean','min','max'],
'AMT_TOTAL_RECEIVABLE':['mean','min','max'],
'CNT_DRAWINGS_ATM_CURRENT':['mean','min','max'],
'CNT_DRAWINGS_CURRENT':['mean','min','max'],
'CNT_DRAWINGS_POS_CURRENT':['mean','min','max'],
'CNT_INSTALMENT_MATURE_CUM':['mean','min','max'],
'SK_DPD':['mean','min','max'],
'SK_DPD_DEF':['mean','min','max'],
}
for col in credit_cat+credit_bin:
credit_aggregation[col] = ['mean','sum']
credit_agg = credit_card_balance.groupby('SK_ID_CURR' ).agg(credit_aggregation)
 credit_agg.columns = pd.Index(['CREDIT_'+e[0]+'_'+ e[1].upper() for e in credit_agg.columns.tolist() ])
 credit_agg['CREDIT_COUNT'] = credit_card_balance.groupby('SK_ID_CURR' ).size()
del credit_card_balance
credit_agg.head(2 )<define_variables>
concat_sub['toxic'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1),
concat_sub['jigsaw_max'],
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
concat_sub['jigsaw_min'],
concat_sub['jigsaw_mean']))
concat_sub[['id', 'toxic']].to_csv('submission.csv',
index=False, float_format='%.6f' )
prev_cat_col = previous_application.select_dtypes(include='object' ).columns
int_col = [i for i in previous_application.columns.values if i not in prev_cat_col]<feature_engineering>
concat_sub['toxic'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1),
concat_sub['jigsaw_max'],
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
concat_sub['jigsaw_min'],
concat_sub['jigsaw_median']))
concat_sub[['id', 'toxic']].to_csv('submission5.csv',
index=False, float_format='%.6f' )
previous_application['AMT_ANNUITY'] = np.log1p(previous_application['AMT_ANNUITY'] )<feature_engineering>
df1 = [f for f in all_files if '0.9462' in f][0]
df2 = [f for f in all_files if '0.9459b' in f][0]
df3 = [f for f in all_files if '0.9459' in f and '0.9459b' not in f][0]
df4 = [f for f in all_files if '0.9450' in f][0]
df5 = [f for f in all_files if '0.9432' in f][0]
df6 = [f for f in all_files if '0.9428' in f][0]
df7 = [f for f in all_files if '0.9428b' in f][0]
df8 = [f for f in all_files if '0.9416' in f][0]
df9 = [f for f in all_files if '0.9414' in f and '0.9414b' not in f][0]
df10 = [f for f in all_files if '0.9414b' in f][0]
df11 = [f for f in all_files if '0.9409' in f][0]
df12 = [f for f in all_files if '0.9322' in f][0]
previous_application['AMT_APPLICATION'] = np.sqrt(previous_application['AMT_APPLICATION'] )<feature_engineering>
df1 = pd.read_csv(os.path.join(sub_path, df1))
df2 = pd.read_csv(os.path.join(sub_path, df2))
df3 = pd.read_csv(os.path.join(sub_path, df3))
df4 = pd.read_csv(os.path.join(sub_path, df4))
df5 = pd.read_csv(os.path.join(sub_path, df5))
df6 = pd.read_csv(os.path.join(sub_path, df6))
df7 = pd.read_csv(os.path.join(sub_path, df7))
df8 = pd.read_csv(os.path.join(sub_path, df8))
df9 = pd.read_csv(os.path.join(sub_path, df9))
df10 = pd.read_csv(os.path.join(sub_path, df10))
df11 = pd.read_csv(os.path.join(sub_path, df11))
df12 = pd.read_csv(os.path.join(sub_path, df12))
df1.head()
previous_application['AMT_CREDIT'] = np.sqrt(previous_application['AMT_CREDIT'] )<feature_engineering>
sub = df1.copy() [['id']]
previous_application['AMT_DOWN_PAYMENT'] = np.log1p(previous_application['AMT_DOWN_PAYMENT'] )<feature_engineering>
W = 0.9462 + 2*0.9459 + 0.9450 + 2*0.9428 + 0.9422 + 0.9416 + 2*0.9414 + 0.9409 + 0.9322
w1 = 0.9462/W
w2 = 0.9459/W
w3 = 0.9459/W
w4 = 0.9450/W
w5 = 0.9432/W
w6 = 0.9428/W
w7 = 0.9428/W
w8 = 0.9416/W
w9 = 0.9414/W
w10 = 0.9414/W
w11 = 0.9409/W
w12 = 0.9322/W
previous_application['AMT_GOODS_PRICE'] = np.log1p(previous_application['AMT_GOODS_PRICE'] )<data_type_conversions>
sub['toxic'] =(w1*df1['toxic'] +
w2*df2['toxic'] +
w3*df3['toxic'] +
w4*df4['toxic'] +
w5*df5['toxic'] +
w6*df6['toxic'] +
w7*df7['toxic'] +
w8*df8['toxic'] +
w9*df9['toxic'] +
w10*df10['toxic'] +
w11*df11['toxic'] +
w12*df12['toxic'])
 sub.head()
previous_application[['HOUR_APPR_PROCESS_START','NFLAG_INSURED_ON_APPROVAL']] = previous_application[['HOUR_APPR_PROCESS_START','NFLAG_INSURED_ON_APPROVAL']].astype('object' )<filter>
sub.to_csv('submission_blend.csv', index=False)
previous_application[previous_application['SELLERPLACE_AREA']>500000]<categorify>
def get_dist_preds(predictions, metric):
new_preds = []
for j, pred in enumerate(predictions):
distances = []
remaining_preds = predictions[:j] + predictions[j+1:]
for pred_ in remaining_preds:
if metric == 'euclid':
distances += [euclidean(pred_, pred)]
elif metric == 'cosine':
distances += [cosine(pred_, pred)]
elif metric == 'jaccard':
distances += [jaccard(pred_, pred)]
elif metric == 'chebyshev':
distances += [chebyshev(pred_, pred)]
elif metric == 'correlation':
distances += [correlation(pred_, pred)]
elif metric == 'cityblock':
distances += [cityblock(pred_, pred)]
elif metric == 'canberra':
distances += [canberra(pred_, pred)]
elif metric == 'braycurtis':
distances += [braycurtis(pred_, pred)]
elif metric == 'hamming':
distances += [hamming(pred_, pred)]
elif metric == 'battacharyya':
distances += [DistanceMetrics.battacharyya(pred_, pred, method='continuous')]
new_preds += [(pred, sum(distances)) ]
weights = [tup[1] for tup in new_preds]
W = sum(weights)
 preds_ascending_dist = sorted(new_preds, key=lambda x: x[1])
 weights_descending = sorted(weights, reverse=True)
 weighted_pred = sum([pred_tup[0]*(weights_descending[k]/W)for k, pred_tup in enumerate(preds_ascending_dist)])
 return weighted_pred
previous_application['DAYS_FIRST_DRAWING'].replace({365243:np.nan},inplace=True)
 previous_application['DAYS_FIRST_DUE'].replace({365243:np.nan},inplace=True)
 previous_application['DAYS_LAST_DUE_1ST_VERSION'].replace({365243:np.nan},inplace=True)
 previous_application['DAYS_LAST_DUE'].replace({365243:np.nan},inplace=True)
 previous_application['DAYS_TERMINATION'].replace({365243:np.nan},inplace=True)
<categorify>
dfs = [df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12]
preds = [np.array(df['toxic'])for df in dfs]
previous_application = replace_XNA_XAP(previous_application)
 prev_bin =[]
previous_application,prev_bin = binary_encoding(previous_application,prev_cat_col)
 previous_application,prev_cat = one_hot_encoding(previous_application,columns= prev_cat_col,nan_as_category=True)
 prev_aggregate = {
'AMT_ANNUITY':['mean','sum','min'],
'AMT_APPLICATION':['mean'],
'AMT_CREDIT':['mean','min','max'],
'AMT_DOWN_PAYMENT':['mean','min'],
'AMT_GOODS_PRICE':['mean','sum','max'],
'HOUR_APPR_PROCESS_START':['mean','min'],
'NFLAG_LAST_APPL_IN_DAY': ['mean'],
'RATE_DOWN_PAYMENT': ['mean'],
'RATE_INTEREST_PRIMARY':['mean','min','max'],
'RATE_INTEREST_PRIVILEGED':['mean','min'],
'DAYS_DECISION':['mean'],
'SELLERPLACE_AREA':['mean'],
'CNT_PAYMENT':['mean','sum'],
'DAYS_FIRST_DRAWING':['mean','min'],
'DAYS_FIRST_DUE':['mean','min'],
'DAYS_LAST_DUE_1ST_VERSION':['mean','min'],
'DAYS_LAST_DUE':['mean','max'],
'DAYS_TERMINATION':['mean','max'],
'NFLAG_INSURED_ON_APPROVAL' : ['mean'],
}
cat_prev_aggregate = {}
for col in prev_cat+prev_bin:
cat_prev_aggregate[col] =['mean','sum']
prev_agg = previous_application.groupby('SK_ID_CURR' ).agg({**prev_aggregate,**cat_prev_aggregate})
 prev_agg.columns = pd.Index(['PREV_'+e[0]+ '_'+ e[1].upper() for e in prev_agg.columns.tolist() ])
 prev_agg = prev_agg.reset_index()
refused = previous_application[previous_application['NAME_CONTRACT_STATUS_Refused'] ==1]
refused_agg = refused.groupby('SK_ID_CURR' ).agg(prev_aggregate)
 refused_agg.columns = pd.Index(['REFUSE_'+e[0]+'_'+ e[1].upper() for e in refused_agg.columns.tolist() ])
 prev_agg = prev_agg.join(refused_agg, on='SK_ID_CURR', how='left')
 del refused, refused_agg
canceled = previous_application[previous_application['NAME_CONTRACT_STATUS_Canceled']==1]
canceled_agg = canceled.groupby('SK_ID_CURR' ).agg(prev_aggregate)
 canceled_agg.columns = pd.Index(['CANC_'+ e[0]+ '_'+ e[1].upper() for e in canceled_agg.columns.tolist() ])
 prev_agg = prev_agg.join(canceled_agg, on='SK_ID_CURR', how='left')
 del canceled, canceled_agg
prev_agg['PREV_COUNT'] = previous_application.groupby('SK_ID_CURR' ).size()
prev_agg = prev_agg.set_index('SK_ID_CURR')
 del previous_application
prev_agg.head()<groupby>
df_deboost = get_dist_preds(preds, 'correlation' )
inst_aggregator = {
'NUM_INSTALMENT_VERSION':['mean'],
'NUM_INSTALMENT_NUMBER':['mean','max'],
'DAYS_INSTALMENT':['min','mean'],
'AMT_INSTALMENT':['mean'],
'AMT_PAYMENT':['mean']
}
inst_agg = installments_payments.groupby('SK_ID_CURR' ).agg(inst_aggregator)
 inst_agg.columns = pd.Index(['INST_'+e[0]+ '_'+ e[1].upper() for e in inst_agg.columns.tolist() ])
 inst_agg['INST_COUNT'] = installments_payments.groupby('SK_ID_CURR' ).size()
del installments_payments
inst_agg.head()<merge>
_sub = df1.copy() [['id']]
_sub['toxic'] = df_deboost
_sub.head()
train_test = train_test.join(bureau_agg,how='left',on='SK_ID_CURR')
 del bureau_agg
train_test = train_test.join(pos_agg, how='left',on='SK_ID_CURR')
 del pos_agg
train_test = train_test.join(inst_agg,how='left',on='SK_ID_CURR')
 del inst_agg
train_test = train_test.join(credit_agg, how='left',on='SK_ID_CURR')
 del credit_agg
train_test = train_test.join(prev_agg, how='left',on='SK_ID_CURR')
 del prev_agg
reduce_memory_usage(train_test )<prepare_x_and_y>
_sub.to_csv('submission_deboost.csv', index=False)
col_drop = ['TARGET','SK_ID_CURR']
X = train_test[train_test['TARGET'].notnull() ].drop(col_drop, axis=1)
 y = train_test[train_test['TARGET'].notnull() ]['TARGET']
test_new = train_test[train_test['TARGET'].isnull() ].drop(col_drop, axis=1)
<train_model>
n_folds = len(dfs)
 n_folds
def model(X_train, X_valid, y_train, y_valid,test_new,random_seed):
lg_param = {}
lg_param['learning_rate'] = 0.02
lg_param['n_estimators'] = 10000
lg_param['max_depth'] = 8
lg_param['num_leaves'] = 34
lg_param['boosting_type'] = 'gbdt'
lg_param['feature_fraction'] = 0.9
lg_param['bagging_fraction'] = 0.9
lg_param['min_child_samples'] = 30
lg_param['lambda_l1'] = 0.04
lg_param['lambda_l2'] = 0.08
lg_param['silent'] = -1
lg_param['verbose'] = -1
lg_param['nthread'] = 4
lg_param['seed'] = random_seed
lgb_model = lgb.LGBMClassifier(**lg_param)
 print('-'*10,'*'*20,'-'*10)
 lgb_model.fit(X_train,y_train,eval_set=[(X_train,y_train),(X_valid,y_valid)],
eval_metric ='auc', verbose =100, early_stopping_rounds=200)
 y_pred = lgb_model.predict_proba(X_valid)[:,1]
print('roc_auc_score',roc_auc_score(y_valid,y_pred),'-'*30,i+1)
 y_pred_new = lgb_model.predict_proba(test_new)[:,1]
return y_pred,y_pred_new,lgb_model<split>
nrow = len(dfs[0])
 nrow
kf = KFold(n_splits=3, shuffle=True, random_state=seed)
 y_pred_new = 0
for i,(train_index, valid_index)in enumerate(kf.split(X,y)) :
X_train, X_valid = X.loc[train_index], X.loc[valid_index]
y_train, y_valid = y[train_index], y[valid_index]
print('
{} fold of {} KFold'.format(i+1,kf.n_splits))
y_pred,y_pred2,lgb_model = model(X_train, X_valid, y_train, y_valid,test_new,random_seed = i)
 y_pred_new += y_pred2 
<save_to_csv>
num_row_per_df = nrow//n_folds
num_row_per_df
feat_impo = pd.DataFrame({'Columns':X.columns,'Importance':lgb_model.feature_importances_})
 feat_impo.sort_values('Importance',ascending=False ).head()
feat_impo.to_csv('feat_impo.csv',index=False )<save_to_csv>
remainder_rows = nrow - n_folds * num_row_per_df
remainder_rows
submit = pd.DataFrame({'SK_ID_CURR':test_index,'TARGET':y_pred_new/kf.n_splits})
 submit.to_csv('home_credit.csv',index=False)
 submit.head()<load_from_csv>
end_indices = [num_row_per_df*i for i in range(1,n_folds)] + [nrow]
end_indices
M1 = pd.read_csv('.. /input/home123/11111.csv')
 M2 = pd.read_csv('.. /input/home123/22222.csv')
 M3 = pd.read_csv('.. /input/home123/33333.csv')
 M4 = pd.read_csv('.. /input/home123/44444.csv')
 M5 = pd.read_csv('.. /input/home123/55555.csv')
 M6 = pd.read_csv('.. /input/home123/Think1.csv')
 M7 = pd.read_csv('.. /input/home123/new1submit8aug.csv')
 M8 = pd.read_csv('.. /input/home123/newthinking.csv')
 M9 = pd.read_csv('.. /input/home123/Think123.csv' )<merge>
partitions = [[[]]*n_folds]*n_folds
partitions
def merge_dataframes(dfs, merge_keys):
dfs_merged = reduce(lambda left,right: pd.merge(left, right, on=merge_keys), dfs)
 return dfs_merged<merge>
data = {}
for i, df in enumerate(dfs):
data[str(i)] = df
ranks = pd.DataFrame(columns=data.keys())
 for key in data.keys() :
ranks[key] = data[key].toxic.rank(method='min')
 ranks['Average'] = ranks.mean(axis=1)
 ranks['Scaled Rank'] =(ranks['Average'] - ranks['Average'].min())/(ranks['Average'].max() - ranks['Average'].min())
 ranks.corr() [:1]
dfs = [M1,M2,M3,M4,M5,M6,M7,M8,M9]
merge_keys=['SK_ID_CURR']
df = merge_dataframes(dfs, merge_keys=merge_keys )<rename_columns>
weights = [0.15, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.05, 0.05, 0.05, 0.05, 0.05]
ranks['Score'] = ranks[[str(i)for i in range(n_folds)]].mul(weights ).sum(1)/ ranks.shape[0]
submission_lb = pd.read_csv(".. /input/jigsaw-multilingual-toxic-comment-classification/sample_submission.csv")
 submission_lb['toxic'] = ranks['Score']
submission_lb.to_csv("WEIGHT_AVERAGE_RANK.csv", index=False)
 submission_lb.head()
df.columns = ['SK_ID_CURR','T1','T2','T3','T4','T5','T6','T7','T8','T9']
df.head()<define_variables>
labels = ["toxic"]
for label in labels:
print(label)
 print(np.round(np.corrcoef([df[label].rank(pct=True)for df in dfs]), 4))
pred_prob = 0.6 * df['T9'] + 0.4 * df['T6']
pred_prob.head()<prepare_output>
submission = dfs[0][['id']]
for label in labels:
preds = []
submission[label] =(
 0.15*(0.9*dfs[0][label] + 0.1*dfs[5][label])+
0.15*(0.9*dfs[1][label] + 0.1*dfs[5][label])+
0.15*(0.9*dfs[2][label] + 0.1*dfs[7][label])+
0.15*(0.9*dfs[3][label] + 0.1*dfs[7][label])+
0.1*(0.5*dfs[4][label] + 0.5*dfs[5][label])+
0.1*(0.5*dfs[5][label] + 0.5*dfs[4][label])+
0.1*(0.1*dfs[6][label] + 0.9*dfs[4][label])+
0.1*(0.5*dfs[7][label] + 0.5*dfs[5][label])
 )
sub = pd.DataFrame()
sub['SK_ID_CURR'] = df['SK_ID_CURR']
sub['target']= pred_prob<save_to_csv>
submission.to_csv('submission_correlations.csv', index=False)
sub.to_csv('ldit.csv', index=False )<define_variables>
df1 = [f for f in all_files if '0.9462' in f][0]
df2 = [f for f in all_files if '0.9459b' in f][0]
df3 = [f for f in all_files if '0.9459' in f and '0.9459b' not in f][0]
df4 = [f for f in all_files if '0.9450' in f][0]
df5 = [f for f in all_files if '0.9432' in f][0]
df6 = [f for f in all_files if '0.9428b' in f][0]
df7 = [f for f in all_files if '0.9428' in f][0]
df8 = [f for f in all_files if '0.9416' in f][0]
df9 = [f for f in all_files if '0.9414' in f][0]
df10 = [f for f in all_files if '0.9322' in f][0]
df1 = pd.read_csv(os.path.join(sub_path, df1))
df2 = pd.read_csv(os.path.join(sub_path, df2))
df3 = pd.read_csv(os.path.join(sub_path, df3))
df4 = pd.read_csv(os.path.join(sub_path, df4))
df5 = pd.read_csv(os.path.join(sub_path, df5))
df6 = pd.read_csv(os.path.join(sub_path, df6))
df7 = pd.read_csv(os.path.join(sub_path, df7))
df8 = pd.read_csv(os.path.join(sub_path, df8))
df9 = pd.read_csv(os.path.join(sub_path, df9))
df10 = pd.read_csv(os.path.join(sub_path, df10))
B_prob = 0.4 * df['T9'] + 0.15 * df['T4'] + 0.15 * df['T6'] + 0.15 *df['T1'] + 0.15*df['T5']<save_to_csv>
dfs = [df1, df2, df3, df4, df5, df6, df7, df8, df9, df10]
SUB = pd.DataFrame()
SUB['SK_ID_CURR'] = df['SK_ID_CURR']
SUB['TARGET'] = B_prob
SUB.to_csv('Blendss.csv', index=False )<save_to_csv>
p = 1.5
SuB = pd.DataFrame()
SuB['SK_ID_CURR'] = df['SK_ID_CURR']
SuB['TARGET'] = corr_pred
SuB.to_csv('corr_blend.csv', index=False )<define_variables>
labels = ["toxic"]
for label in labels:
print(label)
 print(np.round(np.corrcoef([df[label].rank(pct=True)for df in dfs]), 4))
N_FOLDS = 5
MAX_EVALS = 5<load_from_csv>
sub_power = submission[['id']].copy()
sub_power['toxic'] =(df1['toxic']**p * 0.2 +
df2['toxic']**p * 0.1 +
df3['toxic']**p * 0.1 +
df4['toxic']**p * 0.15 +
df5['toxic']**p * 0.25 +
df6['toxic']**p * 0.025 +
df7['toxic']**p * 0.025 +
df8['toxic']**p * 0.05 +
df9['toxic']**p * 0.05 +
df10['toxic']**p * 0.05 )
features = pd.read_csv('.. /input/home-credit-default-risk/application_train.csv')
 features = features.sample(n = 17000, random_state = 42)
 features = features.select_dtypes('number')
 labels = np.array(features['TARGET'].astype(np.int32)).reshape(( -1,))
features = features.drop(columns = ['TARGET', 'SK_ID_CURR'])
 train_features, test_features, train_labels, test_labels = train_test_split(features, labels, test_size = 7000, random_state = 37 )<create_dataframe>
sub_power.to_csv('submission_power_ensemble.csv', index=False)
train_set = lgb.Dataset(data = train_features, label = train_labels)
 test_set = lgb.Dataset(data = test_features, label = test_labels )<choose_model_class>
sub = submission[['id']].copy()
model = lgb.LGBMClassifier()
default_params = model.get_params()
del default_params['n_estimators']
cv_results = lgb.cv(default_params, train_set, num_boost_round = 10000, early_stopping_rounds = 100,
metrics = 'auc', nfold = N_FOLDS, seed = 42 )<train_model>
sub['toxic'] = 0.1*df10['toxic'] + 0.9*df9['toxic']
sub['toxic'] = 0.51*df9['toxic'] + 0.49*sub['toxic']
sub['toxic_temp'] = 0.1*df8['toxic'] + 0.9*df7['toxic']
sub['toxic_temp'] = 0.51*df7['toxic'] + 0.49*sub['toxic_temp']
sub['toxic'] = 0.5*sub['toxic_temp'] + 0.5*sub['toxic']
sub['toxic_temp'] = 0.1*df6['toxic'] + 0.9*df5['toxic']
sub['toxic_temp'] = 0.51*df5['toxic'] + 0.49*sub['toxic_temp']
sub['toxic'] = 0.5*sub['toxic_temp'] + 0.5*sub['toxic']
sub['toxic_temp'] = 0.1*df6['toxic'] + 0.9*df5['toxic']
sub['toxic_temp'] = 0.51*df5['toxic'] + 0.49*sub['toxic_temp']
sub['toxic'] = 0.5*sub['toxic_temp'] + 0.5*sub['toxic']
sub['toxic_temp'] = 0.1*df4['toxic'] + 0.9*df3['toxic']
sub['toxic_temp'] = 0.51*df3['toxic'] + 0.49*sub['toxic_temp']
sub['toxic'] = 0.5*sub['toxic_temp'] + 0.5*sub['toxic']
sub['toxic_temp'] = 0.1*df2['toxic'] + 0.9*df1['toxic']
sub['toxic_temp'] = 0.51*df1['toxic'] + 0.49*sub['toxic_temp']
sub['toxic'] = 0.5*sub['toxic_temp'] + 0.5*sub['toxic']
print('The maximum validation ROC AUC was: {:.5f} with a standard deviation of {:.5f}.'.format(cv_results['auc-mean'][-1], cv_results['auc-stdv'][-1]))
print('The optimal number of boosting rounds(estimators)was {}.'.format(len(cv_results['auc-mean'])) )<import_modules>
sub[['id', 'toxic']].to_csv('submission_pairwise_blend_ensemble.csv', index=False)
 sub[['id', 'toxic']].head()
from sklearn.metrics import roc_auc_score<compute_train_metric>
sub = sub[['id']].copy()
sub_final = sub[['id']].copy()
sub['toxic'] = concat_sub['jigsaw_mean']
sub.head()
model.n_estimators = len(cv_results['auc-mean'])
 model.fit(train_features, train_labels)
 preds = model.predict_proba(test_features)[:, 1]
baseline_auc = roc_auc_score(test_labels, preds)
 print('The baseline model scores {:.5f} ROC AUC on the test set.'.format(baseline_auc))<compute_train_metric>
upper = 0.95
lower = 0.05
lp = 0.8
up = 1.5
def objective(hyperparameters, iteration):

 if 'n_estimators' in hyperparameters.keys() :
del hyperparameters['n_estimators']
cv_results = lgb.cv(hyperparameters, train_set, num_boost_round = 10000, nfold = N_FOLDS,
early_stopping_rounds = 100, metrics = 'auc', seed = 42)
 score = cv_results['auc-mean'][-1]
estimators = len(cv_results['auc-mean'])
 hyperparameters['n_estimators'] = estimators
return [score, hyperparameters, iteration]<compute_test_metric>
sub[(sub['toxic'] <= lower)] = sub[(sub['toxic'] <= lower)]**lp
score, params, iteration = objective(default_params, 1)
 print('The cross-validation ROC AUC was {:.5f}.'.format(score))<choose_model_class>
sub_final['toxic'] = sub['toxic']
sub_final.head()
model = lgb.LGBMModel()
model.get_params()<define_search_space>
sub_final.to_csv('submission_minmax_power_scaling_ensemble.csv', index=False)
 sub_final.head()