Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<feature_engineering>
sns.set_style('whitegrid')
X_test.loc[X_test['maxPlace'] == 0, 'winPlacePerc'] = 0
X_test.loc[X_test['maxPlace'] == 1, 'winPlacePerc'] = 1
X_test.loc[(X_test['maxPlace'] > 1)&(X_test['numGroups'] == 1), 'winPlacePerc'] = 0
X_test['winPlacePerc'].describe()<save_to_csv>
train_df = pd.read_csv('.. /input/train.csv')
 print('Rows: ',train_df.shape[0],'Columns: ',train_df.shape[1])
 train_df.info()
test = pd.read_csv('.. /input/test_V2.csv')
 submission = pd.merge(test, X_test[['matchId','groupId','winPlacePerc']])
 submission = submission[['Id','winPlacePerc']]
submission.to_csv("submission.csv", index=False )<feature_engineering>
train_df['target'].value_counts()
train=pd.read_csv('.. /input/train_V2.csv',sep=',',header=0)
 train=train.dropna()
train['num']=train.groupby('matchId')['Id'].transform('count')
 train['max']=train.groupby('matchId')['kills'].transform('max')
 train=train[train['num']>train['max']]
train=train.drop(['num','max'],axis=1 )<merge>
test_df = pd.read_csv('.. /input/test.csv' )
group_rank = train[['matchId','groupId','winPlacePerc','maxPlace','numGroups']].drop_duplicates()
group_rank['rank'] = group_rank.groupby('matchId')['winPlacePerc'].rank(method='min',ascending=False)
 group_rank['gap'] = 1 /(group_rank['numGroups'] - 1)
 group_rank.loc[group_rank['maxPlace']==1,'re_rank'] = 0
group_rank.loc[group_rank['maxPlace']!=1,'re_rank'
] = group_rank['gap'] *(group_rank['numGroups'] - group_rank['rank'])
 train = pd.merge(train, group_rank[['matchId','groupId','re_rank']], how='left', on=['matchId','groupId'] )<feature_engineering>
X_test = test_df.drop('ID_code',axis=1 )
train.loc[train.matchType.str.contains('fpp'),'fpp'] = 1
train.loc[~train.matchType.str.contains('fpp'),'fpp'] = 0
train.loc[train.matchType.str.contains('normal'),'normal'] = 1
train.loc[~train.matchType.str.contains('normal'),'normal'] = 0
train.loc[train.matchType.str.contains('squad'),'squad'] = 1
train.loc[~train.matchType.str.contains('squad'),'squad'] = 0
train.loc[train.matchType.str.contains('duo'),'duo'] = 1
train.loc[~train.matchType.str.contains('duo'),'duo'] = 0
train.loc[train.matchType.str.contains('solo'),'solo'] = 1
train.loc[~train.matchType.str.contains('solo'),'solo'] = 0<categorify>
X = train_df.drop(['ID_code','target'],axis=1)
 y = train_df['target']
train['maxGrp_killPoints'] = train.groupby('groupId')['killPoints'].transform('max')
 train['Perc_kills'] = train['kills']/train.groupby('matchId')['kills'].transform('sum')
 train['maxPerc_kills'] = train.groupby('matchId')['Perc_kills'].transform('max')
 train['Rank_kills'] = train.groupby('matchId')['kills'].rank(method='min',ascending=False,pct=False)
 train['maxRank_kills'] = train.groupby('matchId')['Rank_kills'].transform('max')
 train['max_kills'] = train.groupby('matchId')['kills'].transform('max')
 train['kill_Death'] = train['max_kills']/train.groupby('matchId')['Id'].transform('count')
 train['RankPerc_kills'] = train.groupby('matchId')['kills'].rank(method='max',ascending=True,pct=True)
 train['tkills']=train.groupby('groupId')['kills'].transform('sum' )<categorify>
n_fold = 5
folds = StratifiedKFold(n_splits=n_fold, shuffle=True, random_state=42 )
train['Perc_damageDt'] = train['damageDealt']/train.groupby('matchId')['damageDealt'].transform('sum')
 train['maxPerc_damageDt'] = train.groupby('matchId')['Perc_damageDt'].transform('max')
 train['Rank_damageDt'] = train.groupby('matchId')['damageDealt'].rank(method='min',ascending=False,pct=False)
 train['maxRank_damageDt'] = train.groupby('matchId')['Rank_damageDt'].transform('max')
 train['max_damageDt'] = train.groupby('matchId')['damageDealt'].transform('max' )<categorify>
params = {'num_leaves': 9,
'min_data_in_leaf': 42,
'objective': 'binary',
'max_depth': 16,
'learning_rate': 0.0123,
'boosting': 'gbdt',
'bagging_freq': 5,
'bagging_fraction': 0.8,
'feature_fraction': 0.8201,
'bagging_seed': 11,
'reg_alpha': 1.728910519108444,
'reg_lambda': 4.9847051755586085,
'random_state': 42,
'metric': 'auc',
'verbosity': -1,
'subsample': 0.81,
'min_gain_to_split': 0.01077313523861969,
'min_child_weight': 19.428902804238373,
'num_threads': 4}
train['walkDistance']=np.log(train['walkDistance']+1)
 train['Perc_wkdis'] = train['walkDistance']/train.groupby('matchId')['walkDistance'].transform('sum')
 train['maxPerc_wkdis'] = train.groupby('matchId')['Perc_wkdis'].transform('max')
 train['Rank_wkdis'] = train.groupby('matchId')['walkDistance'].rank(method='min',ascending=False,pct=False)
 train['maxRank_wkdis'] = train.groupby('matchId')['Rank_wkdis'].transform('max')
 train['max_wkdis'] = train.groupby('matchId')['walkDistance'].transform('max')
 train['maxwkdis']=train.groupby('groupId')['walkDistance'].transform('max')
<categorify>
prediction = np.zeros(len(X_test))
for fold_n,(train_index, valid_index)in enumerate(folds.split(X,y)) :
print('Fold', fold_n, 'started at', time.ctime())
 X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
train_data = lgb.Dataset(X_train, label=y_train)
 valid_data = lgb.Dataset(X_valid, label=y_valid)
 model = lgb.train(params,train_data,num_boost_round=20000,
valid_sets = [train_data, valid_data],verbose_eval=300,early_stopping_rounds = 200)
 prediction += model.predict(X_test, num_iteration=model.best_iteration)/5
train['Perc_ridedis'] = train['rideDistance']/train.groupby('matchId')['rideDistance'].transform('sum')
 train['maxPerc_ridedis'] = train.groupby('matchId')['Perc_ridedis'].transform('max')
 train['Rank_ridedis'] = train.groupby('matchId')['rideDistance'].rank(method='min',ascending=False,pct=False)
 train['maxRank_ridedis'] = train.groupby('matchId')['Rank_ridedis'].transform('max')
 train['max_ridedis'] = train.groupby('matchId')['rideDistance'].transform('max')
 train['maxGrp_ridedis']=train.groupby('groupId')['rideDistance'].transform('max' )<categorify>
train_pool = Pool(X,y)
 m = CatBoostClassifier(iterations=300,eval_metric="AUC", boosting_type = 'Ordered')
 m.fit(X,y,silent=True)
 y_pred1 = m.predict(X_test)
 m.best_score_
train['Perc_boo'] = train['boosts']/train.groupby('matchId')['boosts'].transform('sum')
 train['max_boo'] = train.groupby('matchId')['boosts'].transform('max')
 train['Perc_heals'] = train['heals']/train.groupby('matchId')['heals'].transform('sum')
 train['max_heals'] = train.groupby('matchId')['heals'].transform('max')
 train['assReviv'] = train['assists']+train['revives']
train = train.drop(columns=['assists','revives'],axis=1)
 train['Perc_assReviv'] = train['assReviv']/train.groupby('matchId')['assReviv'].transform('sum')
 train['max_assReviv'] = train.groupby('matchId')['assReviv'].transform('max' )<categorify>
sub = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub["target"] = prediction
sub.to_csv("submission.csv", index=False )
train['teamSize'] = train.groupby('groupId')['Id'].transform('count')
 train['numEnemies'] = train.groupby('matchId')['Id'].transform('count')-train['teamSize']
train['pace'] = train.groupby('matchId')['kills'].transform('sum')/train['matchDuration']<drop_column>
sub1 = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub1["target"] = y_pred1
sub1.to_csv("submission1.csv", index=False )
train = train.drop('matchType',axis = 1)
 train = train.drop(columns=['vehicleDestroys','roadKills'],axis=1 )<prepare_x_and_y>
sub2 = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub2["target"] =(prediction + y_pred1)/2
sub2.to_csv("submission2.csv", index=False )