Kaggle competition: Santander Customer Transaction Prediction
df_oof = pd.DataFrame()
df_oof['pytorch_oof'] = oof
df_oof.to_csv('pytorch_oof.csv', index_label='id' )<save_to_csv>
train=pd.read_csv('.. /input/train.csv')
 test=pd.read_csv('.. /input/test.csv' )
df_submission = pd.DataFrame(index=df_test_id.index)
 df_submission['winPlacePerc'] = y
df_submission.to_csv('pytorch_raw.csv', index_label='Id' )<load_from_csv>
train_x=train.drop(['ID_code','target'],axis=1)
 corr_matrix = train_x.corr().abs()
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1 ).astype(np.bool))
to_drop = [column for column in upper.columns if any(upper[column] > 0.995)]
df_test = pd.read_csv('.. /input/test_V2.csv')
 df_test.shape<merge>
folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=2019)
 oof = np.zeros(len(train))
predictions = np.zeros(len(test))
feature_importance_df = pd.DataFrame()
target=train.target
train_id=train.ID_code
test_id=test.ID_code
test_df=test.drop('ID_code',axis=1)
 test_df=test_df.drop(to_drop,axis=1 )
df_submission = df_submission.merge(df_test[['Id', 'matchId', 'groupId', 'maxPlace', 'numGroups']], on='Id', how='left')
 df_submission.head()<merge>
param = {'num_leaves': 21,
'min_data_in_leaf': 35,
'objective':'binary',
'max_depth': -1,
'learning_rate': 0.01,
"boosting": "gbdt",
"feature_fraction": 0.6,
"bagging_fraction": 0.8,
"bagging_seed": 42,
"metric": 'auc',
"verbosity": -1,
"nthread": 4,
"random_state": 2019}
df_submission_group = df_submission.groupby(['matchId', 'groupId'] ).first().reset_index()
df_submission_group['rank'] = df_submission_group.groupby(['matchId'])['winPlacePerc'].rank()
df_submission_group = df_submission_group.merge(df_submission_group.groupby('matchId')['rank'].max().to_frame('max_rank' ).reset_index() , on='matchId', how='left')
 df_submission_group['adjusted_perc'] =(df_submission_group['rank'] - 1)/(df_submission_group['numGroups'] - 1)
 df_submission = df_submission.merge(df_submission_group[['adjusted_perc', 'matchId', 'groupId']], on=['matchId', 'groupId'], how='left')
 df_submission['winPlacePerc'] = df_submission['adjusted_perc']
df_submission.head()<feature_engineering>
train_columns=train_x.columns
evescore=pd.DataFrame({'training':[0],'valid_1':[0]},index=['rmse'])
 for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_x,target)) :
trn_data = lgb.Dataset(train_x.iloc[trn_idx][train_columns], label=target.iloc[trn_idx])
 val_data = lgb.Dataset(train_x.iloc[val_idx][train_columns], label=target.iloc[val_idx])
 num_round = 100000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000,early_stopping_rounds = 300)
 oof[val_idx] = clf.predict(train_x.iloc[val_idx][train_columns], num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = train_columns
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 evescore=evescore+pd.DataFrame(clf.best_score)
 predictions += clf.predict(test_df[train_columns], num_iteration=clf.best_iteration )
df_submission.loc[df_submission.maxPlace == 0, 'winPlacePerc'] = 0
df_submission.loc[df_submission.maxPlace == 1, 'winPlacePerc'] = 1<feature_engineering>
predictions/5
lvl_one_train=pd.DataFrame(train_id)
 lvl_one_train['target']=oof
lvl_one_test=pd.DataFrame(test_id)
 lvl_one_test['target']=predictions
lvl_one_train.to_csv('lgb_train1.csv',index=False)
 lvl_one_test.to_csv('lgb_test1.csv',index=False )