Kaggle competition: Santander Customer Transaction Prediction
test['NumberOfPlayers'] = test.groupby('matchId')['matchId'].transform('count')
 test['NumberOfPlayersInTeam'] = test.groupby(['matchId','groupId'])['groupId'].transform('count' )<concatenate>
color = sns.color_palette()
%matplotlib inline
py.init_notebook_mode(connected=True)
 pd.options.mode.chained_assignment = None
pd.options.display.max_columns = 999
train = pd.concat([train,train_matchtype],axis=1 )<concatenate>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv" )
test = pd.concat([test,test_matchtype],axis=1 )<remove_duplicates>
from sklearn.model_selection import train_test_split
from sklearn.metrics import(roc_curve, auc, accuracy_score)
 from sklearn.model_selection import GridSearchCV
traing= train.drop_duplicates(subset=['groupId'], keep='first')
 del train 
<remove_duplicates>
test_ID = test_df['ID_code'].values
train_df = train_df.drop("ID_code",axis=1)
 test_df = test_df.drop("ID_code",axis=1 )
testg= test.drop_duplicates(subset=['groupId'], keep='first')
<groupby>
target_col = "target"
traing['NumberOfGroups'] = traing.groupby('matchId')['groupId'].transform('count')
<groupby>
params = {
"objective" : "binary",
"metric" : "auc",
"max_depth" : -1,
"num_leaves" : 8,
"min_data_in_leaf" : 25,
"learning_rate" : 0.006,
"bagging_fraction" : 0.2,
"feature_fraction" : 0.4,
"bagging_freq" : 1,
"lambda_l1" : 5,
"lambda_l2" : 5,
"verbosity" : 1,
"max_bin": 512,
"num_threads" : 6
}
testg['NumberOfGroups'] = testg.groupby('matchId')['groupId'].transform('count')
<prepare_x_and_y>
gridParams = {
'learning_rate': [0.005],
'n_estimators': [2000],
'num_leaves': [4,2],
'colsample_bytree' : [0.7,0.07],
'reg_alpha' : [1,1.2,7],
'reg_lambda' : [5,5.5,0.6],
'subsample' : [0.7,0.07],
'objective' : ['binary'],
'boosting_type' : ['gbdt'],
}
Y=traing["winPlacePerc"]
traing=traing.drop(columns="winPlacePerc")
 train_objs_num = len(traing)
<define_variables>
train_y = train_df[target_col].values
features = [c for c in train_df.columns if c not in ['target']]
train_df = train_df.drop("target",axis=1)
 train_X = train_df
test_X = test_df
for i in ['stdassists', 'stdboosts', 'stddamageDealt',
'stdDBNOs', 'stdheadshotKills', 'stdheals', 'stdkillPlace',
'stdkillPoints', 'stdkills', 'stdkillStreaks', 'stdlongestKill',
'stdmatchDuration', 'stdmaxPlace', 'stdnumGroups', 'stdrankPoints',
'stdrevives', 'stdrideDistance', 'stdroadKills', 'stdswimDistance',
'stdteamKills', 'stdvehicleDestroys', 'stdwalkDistance',
'stdweaponsAcquired', 'stdwinPoints', 'Meanassists', 'Meanboosts',
'MeandamageDealt', 'MeanDBNOs', 'MeanheadshotKills', 'Meanheals',
'MeankillPlace', 'MeankillPoints', 'Meankills', 'MeankillStreaks',
'MeanlongestKill', 'MeanmatchDuration', 'MeanmaxPlace', 'MeannumGroups',
'MeanrankPoints', 'Meanrevives', 'MeanrideDistance', 'MeanroadKills',
'MeanswimDistance', 'MeanteamKills', 'MeanvehicleDestroys',
'MeanwalkDistance', 'MeanweaponsAcquired', 'MeanwinPoints',
]:
a='std2'+i
print(a)
 testg[a] = testg.groupby(['matchId'])[i].transform('std')
 traing[a] = traing.groupby(['matchId'])[i].transform('std')
 testg[a] = testg[a].fillna(0)
 traing[a] = traing[a].fillna(0)
 for i in ['stdassists', 'stdboosts', 'stddamageDealt',
'stdDBNOs', 'stdheadshotKills', 'stdheals', 'stdkillPlace',
'stdkillPoints', 'stdkills', 'stdkillStreaks', 'stdlongestKill',
'stdmatchDuration', 'stdmaxPlace', 'stdnumGroups', 'stdrankPoints',
'stdrevives', 'stdrideDistance', 'stdroadKills', 'stdswimDistance',
'stdteamKills', 'stdvehicleDestroys', 'stdwalkDistance',
'stdweaponsAcquired', 'stdwinPoints', 'Meanassists', 'Meanboosts',
'MeandamageDealt', 'MeanDBNOs', 'MeanheadshotKills', 'Meanheals',
'MeankillPlace', 'MeankillPoints', 'Meankills', 'MeankillStreaks',
'MeanlongestKill', 'MeanmatchDuration', 'MeanmaxPlace', 'MeannumGroups',
'MeanrankPoints', 'Meanrevives', 'MeanrideDistance', 'MeanroadKills',
'MeanswimDistance', 'MeanteamKills', 'MeanvehicleDestroys',
'MeanwalkDistance', 'MeanweaponsAcquired', 'MeanwinPoints',
]:
a='Mean2'+i
print(a)
 testg[a] = testg.groupby(['matchId'])[i].transform('mean')
 traing[a] = traing.groupby(['matchId'])[i].transform('mean')
 testg[a] = testg[a].fillna(0)
 traing[a] = traing[a].fillna(0 )<groupby>
lgtrain = lgb.Dataset(train_X, label=train_y)
 evals_result = {}
temp1=testg['groupId']<create_dataframe>
mdl = lgb.LGBMClassifier(boosting_type= 'gbdt',
objective = 'binary',
n_jobs = 6,
silent = True,
max_depth = params['max_depth'],
max_bin = params['max_bin'] )
temp2=pd.DataFrame()
temp2['groupId']=testg['groupId']
temp2['NumberOfGroups']=testg['NumberOfGroups']
temp2['maxPlace']=testg['MeanmaxPlace']<drop_column>
grid = GridSearchCV(mdl, gridParams,scoring='roc_auc',
verbose=0,
cv=4,
n_jobs=8 )
testg=testg.drop(columns=["Id"])
 testg=testg.drop(columns=["groupId"])
 testg=testg.drop(columns=["matchId"])
<drop_column>
grid.fit(train_X, train_y )
traing=traing.drop(columns=["Id"])
 traing=traing.drop(columns=["groupId"])
 traing=traing.drop(columns=["matchId"] )<count_missing_values>
print(grid.best_params_)
 print(grid.best_score_ )
traing.isna().sum().sum()<normalization>
params['feature_fraction'] = grid.best_params_['colsample_bytree']
params['learning_rate'] = grid.best_params_['learning_rate']
params['num_leaves'] = grid.best_params_['num_leaves']
params['lambda_l1'] = grid.best_params_['reg_alpha']
params['lambda_l2'] = grid.best_params_['reg_lambda']
params['bagging_fraction'] = grid.best_params_['subsample']
numberOfEstimators = grid.best_params_['n_estimators']
sc = StandardScaler()
X_train = sc.fit_transform(traing)
<normalization>
def run_lgb(train_X, train_y, val_X, val_y, test_X,params):
lgtrain = lgb.Dataset(train_X, label=train_y)
 lgval = lgb.Dataset(val_X, label=val_y)
 evals_result = {}
model = lgb.train(params, lgtrain, 300000, valid_sets=[lgtrain,lgval], early_stopping_rounds=500, verbose_eval=100, evals_result=evals_result)
 pred_test_y = model.predict(test_X, num_iteration=model.best_iteration)
 return pred_test_y, model, evals_result
X_test = sc.transform(testg)
<drop_column>
pred_test = 0
kf = model_selection.KFold(n_splits=6, random_state=2018, shuffle=True)
 feature_importance_df = pd.DataFrame()
for dev_index, val_index in kf.split(train_df):
dev_X, val_X = train_X.loc[dev_index,:], train_X.loc[val_index,:]
dev_y, val_y = train_y[dev_index], train_y[val_index]
pred_test_tmp, model, evals_result = run_lgb(dev_X, dev_y, val_X, val_y, test_X,params)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = model.feature_importance()
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 pred_test += pred_test_tmp / 6
<import_modules><EOS> 
print('save result.')
 pd.DataFrame({'ID_code':test_ID,'target':pred_test} ).to_csv('submission.csv',index=False)
 print('done.' )