Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<feature_engineering>
py.init_notebook_mode(connected=True)
 plt.style.use('seaborn')
 sns.set(font_scale=1)
 pd.set_option('display.max_columns', 500 )
train['headShotKillRatio']=train['headshotKills']/train['kills']
test['headShotKillRatio']=test['headshotKills']/test['kills']<feature_engineering>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 print("Train: ",train.shape)
 print("Test: ", test.shape )
train['headshotKillRate'] = train['headshotKills'] / train['kills']
test['headshotKillRate'] = test['headshotKills'] / test['kills']<feature_engineering>
target = train.pop('target')
 train_ids = train.pop('ID_code')
 test_ids = test.pop('ID_code')
 len_train = len(train )
train['killPlaceOverMaxPlace'] = train['killPlace'] / train['maxPlace']
test['killPlaceOverMaxPlace'] = test['killPlace'] / test['maxPlace']<feature_engineering>
y = target.values
X = train
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=1)
 print(X_train.shape, y_train.shape)
 print(X_val.shape, y_val.shape )
train['kills/distance']=train['kills']/train['totalDistance']
test['kills/distance']=test['kills']/test['totalDistance']<feature_engineering>
rfc_model = RandomForestClassifier(random_state=0 ).fit(X_train, y_train)
 perm = PermutationImportance(rfc_model, random_state=1 ).fit(X_val, y_val)
 eli5.show_weights(perm, feature_names = X_val.columns.tolist() )
train['kills/walkDistance']=train['kills']/train['walkDistance']
test['kills/walkDistance']=test['kills']/test['walkDistance']<feature_engineering>
del data, fig
gc.collect()
train['avgKills'] = train['squadKills']/train['teamPlayers']
test['avgKills'] = test['squadKills']/test['teamPlayers']<feature_engineering>
merged = pd.concat([train, test])
 original_features = merged.columns
merged.shape
train['damageRatio'] = train['damageDealt']/train['enemyDamage']
test['damageRatio'] = test['damageDealt']/test['enemyDamage']<feature_engineering>
%%time
idx = features = merged.columns.values[0:200]
for df in [merged]:
df['sum'] = df[idx].sum(axis=1)
 df['min'] = df[idx].min(axis=1)
 df['max'] = df[idx].max(axis=1)
 df['mean'] = df[idx].mean(axis=1)
 df['std'] = df[idx].std(axis=1)
 df['skew'] = df[idx].skew(axis=1)
 df['kurt'] = df[idx].kurtosis(axis=1)
 df['med'] = df[idx].median(axis=1 )
train['distTravelledPerGame'] = train['totalDistance']/train['matchDuration']
test['distTravelledPerGame'] = test['totalDistance']/test['matchDuration']<feature_engineering>
new_features = set(merged.columns)- set(original_features )
train['killPlacePerc'] = train['killPlace']/train['gamePlayers']
test['killPlacePerc'] = test['killPlace']/test['gamePlayers']<feature_engineering>
train = merged.iloc[:len_train]
X = train
train.head()
train["playerSkill"] = train["headshotKills"]+ train["roadKills"]+train["assists"]-(5*train['teamKills'])
 test["playerSkill"] = test["headshotKills"]+ test["roadKills"]+test["assists"]-(5*test['teamKills'] )<feature_engineering>
del merged
gc.collect()
train['gamePlacePerc'] = train['killPlace']/train['maxPlace']
test['gamePlacePerc'] = test['killPlace']/test['maxPlace']<prepare_x_and_y>
params_tuned = {
'bagging_freq': 5,
'bagging_fraction': 0.4,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.05,
'learning_rate': 0.01,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1
}
train.fillna(0,inplace=True)
 train.replace(np.inf, 0, inplace=True)
 test.fillna(0,inplace=True)
 test.replace(np.inf, 0, inplace=True )<count_values>
%%time
X_test = test.values.astype(float)
 y_pred_lgb = np.zeros(len(X_test))
features = [c for c in X.columns]
oof = np.zeros(len(train))
fold_n = 5
cv = fold_n
folds = StratifiedKFold(n_splits=fold_n, random_state=10, shuffle=True)
 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[train_index], y[valid_index]
train_data = lgb.Dataset(X_train, label=y_train)
 valid_data = lgb.Dataset(X_valid, label=y_valid)
 lgb_model = lgb.train(params_tuned,train_data,num_boost_round=100000,
valid_sets = [train_data, valid_data],verbose_eval=1000,early_stopping_rounds = 3000)
 oof[valid_index] = lgb_model.predict(X.iloc[valid_index][features], num_iteration=lgb_model.best_iteration)
 feature_importance_df = pd.DataFrame()
fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = features
fold_importance_df["importance"] = lgb_model.feature_importance()
fold_importance_df["fold"] = fold_n + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 y_pred_lgb += lgb_model.predict(X_test, num_iteration=lgb_model.best_iteration)/cv
print("CV score: {:<8.5f}".format(roc_auc_score(y, oof)) )
<drop_column><EOS> 
submission_lgb = pd.DataFrame({
"ID_code": test_ids,
"target": y_pred_lgb
})
 submission_lgb.to_csv('submission_lgb.csv', index=False )