Kaggle competition: Santander Customer Transaction Prediction
match_ids = train['matchId'].drop_duplicates()
msk=np.random.rand(len(match_ids)) <0.8
valid = train[train['matchId'].isin(match_ids[~msk])]
train = train[train['matchId'].isin(match_ids[msk])]
train = train.sort_values(by=['matchId','re_rank'])
 y_train = train['re_rank'].values
group_train = train.groupby('matchId',as_index=False)['Id'].agg('count')['Id'].tolist()
x_train = scipy.sparse.csr_matrix(train.drop(columns=['matchId','groupId','Id','re_rank','winPlacePerc'],axis=1))<prepare_x_and_y>
train_df=pd.read_csv(".. /input/train.csv" )
valid = valid.sort_values(by=['matchId','re_rank'])
 y_valid = valid['re_rank'].values
group_valid = valid.groupby('matchId',as_index=False)['Id'].agg('count')['Id'].tolist()
x_valid = scipy.sparse.csr_matrix(valid.drop(columns=['matchId','groupId','Id','re_rank','winPlacePerc'],axis=1))<feature_engineering>
test_df=pd.read_csv(".. /input/test.csv" )
test=pd.read_csv('.. /input/test_V2.csv',sep=',',header=0)
 test.loc[test.matchType.str.contains('fpp'),'fpp'] = 1
test.loc[~test.matchType.str.contains('fpp'),'fpp'] = 0
test.loc[test.matchType.str.contains('normal'),'normal'] = 1
test.loc[~test.matchType.str.contains('normal'),'normal'] = 0
test.loc[test.matchType.str.contains('squad'),'squad'] = 1
test.loc[~test.matchType.str.contains('squad'),'squad'] = 0
test.loc[test.matchType.str.contains('duo'),'duo'] = 1
test.loc[~test.matchType.str.contains('duo'),'duo'] = 0
test.loc[test.matchType.str.contains('solo'),'solo'] = 1
test.loc[~test.matchType.str.contains('solo'),'solo'] = 0<categorify>
train_df.target.value_counts() /train_df.shape[0]
test['maxGrp_killPoints'] = test.groupby('groupId')['killPoints'].transform('max')
 test['Perc_kills'] = test['kills']/test.groupby('matchId')['kills'].transform('sum')
 test['maxPerc_kills'] = test.groupby('matchId')['Perc_kills'].transform('max')
 test['Rank_kills'] = test.groupby('matchId')['kills'].rank(method='min',ascending=False,pct=False)
 test['maxRank_kills'] = test.groupby('matchId')['Rank_kills'].transform('max')
 test['max_kills'] = test.groupby('matchId')['kills'].transform('max')
 test['kill_Death'] = test['max_kills']/test.groupby('matchId')['Id'].transform('count')
 test['RankPerc_kills'] = test.groupby('matchId')['kills'].rank(method='max',ascending=True,pct=True)
 test['tkills']=test.groupby('groupId')['kills'].transform('sum')
 test['Perc_damageDt'] = test['damageDealt']/test.groupby('matchId')['damageDealt'].transform('sum')
 test['maxPerc_damageDt'] = test.groupby('matchId')['Perc_damageDt'].transform('max')
 test['Rank_damageDt'] = test.groupby('matchId')['damageDealt'].rank(method='min',ascending=False,pct=False)
 test['maxRank_damageDt'] = test.groupby('matchId')['Rank_damageDt'].transform('max')
 test['max_damageDt'] = test.groupby('matchId')['damageDealt'].transform('max')
 test['walkDistance']=np.log(test['walkDistance']+1)
 test['Perc_wkdis'] = test['walkDistance']/test.groupby('matchId')['walkDistance'].transform('sum')
 test['maxPerc_wkdis'] = test.groupby('matchId')['Perc_wkdis'].transform('max')
 test['Rank_wkdis'] = test.groupby('matchId')['walkDistance'].rank(method='min',ascending=False,pct=False)
 test['maxRank_wkdis'] = test.groupby('matchId')['Rank_wkdis'].transform('max')
 test['max_wkdis'] = test.groupby('matchId')['walkDistance'].transform('max')
 test['maxwkdis']=test.groupby('groupId')['walkDistance'].transform('max')
 test['Perc_ridedis'] = test['rideDistance']/test.groupby('matchId')['rideDistance'].transform('sum')
 test['maxPerc_ridedis'] = test.groupby('matchId')['Perc_ridedis'].transform('max')
 test['Rank_ridedis'] = test.groupby('matchId')['rideDistance'].rank(method='min',ascending=False,pct=False)
 test['maxRank_ridedis'] = test.groupby('matchId')['Rank_ridedis'].transform('max')
 test['max_ridedis'] = test.groupby('matchId')['rideDistance'].transform('max')
 test['maxGrp_ridedis']=test.groupby('groupId')['rideDistance'].transform('max')
 test['Perc_boo'] = test['boosts']/test.groupby('matchId')['boosts'].transform('sum')
 test['max_boo'] = test.groupby('matchId')['boosts'].transform('max')
 test['Perc_heals'] = test['heals']/test.groupby('matchId')['heals'].transform('sum')
 test['max_heals'] = test.groupby('matchId')['heals'].transform('max')
 test['assReviv'] = test['assists']+test['revives']
test = test.drop(columns=['assists','revives'],axis=1)
 test['Perc_assReviv'] = test['assReviv']/test.groupby('matchId')['assReviv'].transform('sum')
 test['max_assReviv'] = test.groupby('matchId')['assReviv'].transform('max')
 test['teamSize'] = test.groupby('groupId')['Id'].transform('count')
 test['numEnemies'] = test.groupby('matchId')['Id'].transform('count')-test['teamSize']
test['pace'] = test.groupby('matchId')['kills'].transform('sum')/test['matchDuration']
test = test.drop('matchType',axis = 1)
 test = test.drop(columns=['vehicleDestroys','roadKills'],axis=1)
 test = test.sort_values(by=['matchId'])
 group_test = test.groupby('matchId',as_index=False)['Id'].agg('count')['Id'].tolist()
x_test = scipy.sparse.csr_matrix(test.drop(axis=1,columns=['Id','groupId','matchId']))<train_model>
train_df.isnull().any().sum()
params = {'objective': 'rank:pairwise', 'learning_rate': 0.1,
'gamma': 0, 'min_child_weight': 60,
'max_depth': 14, 'n_estimators': 6,
'colsample_bytree': 1,'subsample': 1,
'eval_metric': 'mae'}
model = xgb.sklearn.XGBRanker(**params)
 model.fit(x_train, y_train, group_train,
eval_set=[(x_valid, y_valid)], eval_group=[group_valid])
 pred = model.predict(x_test )<feature_engineering>
target=train_df.target
test['pred'] = pred
test['mean_pred'] = test.groupby('groupId')['pred'].transform('mean')
 group_rank = test[['matchId','groupId','mean_pred','maxPlace','numGroups']].drop_duplicates()
group_rank['prank'] = group_rank.groupby('matchId')['mean_pred'].rank(method='min',ascending=False )<feature_engineering>
target = train_df['target']
train=train_df.drop(["ID_code","target"],axis=1)
 test = test_df.drop('ID_code',axis=1 )
group_rank['emp_ranks'] = group_rank['maxPlace'] - group_rank['numGroups']
group_rank['gap'] = 1 /(group_rank['maxPlace'] - 1)
 group_rank.loc[group_rank['maxPlace']==1,'winPlacePerc'] = 0
group_rank.loc[(group_rank['maxPlace']!=1)&(group_rank['prank']<(group_rank['numGroups']/2 ).astype(int)) ,'winPlacePerc'
] = group_rank['gap'] *(group_rank['maxPlace'] - group_rank['prank'])
 group_rank.loc[(group_rank['maxPlace']!=1)&(group_rank['prank']>(group_rank['numGroups']/2 ).astype(int)) ,'winPlacePerc'
] = group_rank['gap'] *(group_rank['maxPlace'] - group_rank['prank'] - group_rank['emp_ranks'])
 group_rank.loc[(group_rank['maxPlace']!=1)&(group_rank['prank']==(group_rank['numGroups']/2 ).astype(int)) ,'winPlacePerc'
] = group_rank['gap'] *(group_rank['maxPlace'] - group_rank['prank'] -(group_rank['emp_ranks']/2 ).astype(int))<save_to_csv>
scv=StratifiedKFold(n_splits=5,shuffle=True,random_state=42)
 oof=np.zeros(train_df.shape[0])
 predictions=np.zeros(test_df.shape[0])
 feature_importance_df=pd.DataFrame()
param = {
'num_leaves': 10,
'max_bin': 119,
'min_data_in_leaf': 11,
'learning_rate': 0.02,
'min_sum_hessian_in_leaf': 0.00245,
'bagging_fraction': 1.0,
'bagging_freq': 5,
'feature_fraction': 0.05,
'lambda_l1': 4.972,
'lambda_l2': 2.276,
'min_gain_to_split': 0.65,
'max_depth': 14,
'save_binary': True,
'seed': 1337,
'feature_fraction_seed': 1337,
'bagging_seed': 1337,
'drop_seed': 1337,
'data_random_seed': 1337,
'objective': 'binary',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': 'auc',
'is_unbalance': True,
'boost_from_average': False,
}
for fold_,(trn_idx, val_idx)in enumerate(scv.split(train.values, target.values)) :
print("fold n°{}".format(fold_))
trn_data = lgb.Dataset(train.iloc[trn_idx], label=target.iloc[trn_idx])
 val_data = lgb.Dataset(train.iloc[val_idx], label=target.iloc[val_idx])
 num_round = 10000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 100)
 oof[val_idx] = clf.predict(train.iloc[val_idx], 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)
 predictions += clf.predict(test, num_iteration=clf.best_iteration)/ 5
print("CV score: {:<8.5f}".format(roc_auc_score(target, oof)) )
<set_options><EOS> 
sub_lgb = pd.DataFrame({"ID_code":test_df["ID_code"].values})
 sub_lgb["target"] = predictions
sub_lgb.to_csv("submission_lgb.csv", index=False )