Kaggle competition: Santander Customer Transaction Prediction
constant_column = [col for col in all_data.columns if all_data[col].nunique() == 1]
print('drop columns:', constant_column)
 all_data.drop(constant_column, axis=1, inplace=True )<categorify>
train = pd.read_csv('.. /input/train.csv')
 train_label = train['target']
train_id = train['ID_code']
del train['target'], train['ID_code']
test = pd.read_csv('.. /input/test.csv')
 test_id = test['ID_code']
del test['ID_code']
mapper = lambda x: 'solo' if('solo' in x)else 'duo' if('duo' in x)or('crash' in x)else 'squad'
all_data['matchType'] = all_data['matchType'].apply(mapper)
 all_data = pd.concat([all_data, pd.get_dummies(all_data['matchType'], prefix='matchType')], axis=1 )<categorify>
sc = StandardScaler()
X_train_std = sc.fit_transform(train)
 X_test_std = sc.fit_transform(test )
cols = [col for col in all_data.columns if col not in ['Id','matchId','groupId']]
for i, t in all_data.loc[:, cols].dtypes.iteritems() :
if t == object:
all_data[i] = pd.factorize(all_data[i])[0]
all_data = reduce_mem_usage(all_data)
 all_data.head()<prepare_x_and_y>
from sklearn.model_selection import StratifiedKFold, train_test_split, cross_validate, cross_val_predict, cross_val_score
import xgboost as xgb
import lightgbm as lgbm
from sklearn.metrics import roc_auc_score
X_train = all_data[all_data['winPlacePerc'].notnull() ].reset_index(drop=True)
 X_test = all_data[all_data['winPlacePerc'].isnull() ].drop(['winPlacePerc'], axis=1 ).reset_index(drop=True)
 del all_data
gc.collect()
Y_train = X_train.pop('winPlacePerc')
 X_test_grp = X_test[['matchId','groupId']].copy()
X_train.drop(['matchId','groupId'], axis=1, inplace=True)
 X_test.drop(['matchId','groupId'], axis=1, inplace=True)
 X_train_cols = X_train.columns
print(X_train.shape, X_test.shape )<choose_model_class>
num_boost_round = 10000
params = {"objective": "binary",
'metric' : 'binary_logloss',
"boosting_type": "gbdt",
"learning_rate": 0.1,
"num_leaves": 45,
"max_bin": 256,
"feature_fraction": 0.6,
"verbosity": 0,
"drop_rate": 0.1,
"is_unbalance": False,
"max_drop": 50,
"min_child_samples": 10,
"min_child_weight": 150,
"min_split_gain": 0,
"subsample": 0.9
}
def createModel() :
model = Sequential()
model.add(Dense(512, kernel_initializer='he_normal', input_dim=X_train.shape[1], activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(256, kernel_initializer='he_normal'))
model.add(PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=None))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(128, kernel_initializer='he_normal'))
model.add(PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=None))
model.add(BatchNormalization())
 model.add(Dropout(0.1))
model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))
optimizer = optimizers.Adam(lr=0.005)
 model.compile(optimizer=optimizer, loss='mse', metrics=['mae'])
 return model<train_model>
NFOLDS = 5
kfold = StratifiedKFold(n_splits=NFOLDS, shuffle=True, random_state=1112)
 x_score = []
final_cv_train = np.zeros(len(train_label))
final_cv_pred = np.zeros(len(test_id))
np.random.seed(42)
 set_random_seed(1234)
 scaler = preprocessing.StandardScaler().fit(X_train.astype(float))
X_train = scaler.transform(X_train.astype(float))
X_test = scaler.transform(X_test.astype(float))
model = createModel()
history = model.fit(
 X_train, Y_train,
epochs=200,
batch_size=2**15,
validation_split=0.2,
callbacks=[lr_sched, early_stopping],
verbose=2)
 pred = model.predict(X_test ).ravel()<load_from_csv>
for s in range(16):
cv_train = np.zeros(len(train_label))
cv_pred = np.zeros(len(test_id))
params['seed'] = s
kf = kfold.split(X_train_std, train_label)
 best_trees = []
fold_scores = []
for i,(train_fold, validate)in enumerate(kf):
X_train, X_validate, label_train, label_validate = X_train_std[train_fold, :], X_train_std[validate, :], train_label[train_fold], train_label[validate]
dtrain = lgbm.Dataset(X_train, label_train)
 dvalid = lgbm.Dataset(X_validate, label_validate, reference=dtrain)
 bst = lgbm.train(params, dtrain, num_boost_round, valid_sets=dvalid, verbose_eval=100, early_stopping_rounds=100)
 best_trees.append(bst.best_iteration)
 cv_pred += bst.predict(X_test_std, num_iteration=bst.best_iteration)
 cv_train[validate] += bst.predict(X_validate)
 score = roc_auc_score(label_validate, cv_train[validate])
 print(score)
 fold_scores.append(score)
 cv_pred /= NFOLDS
final_cv_train += cv_train
final_cv_pred += cv_pred
print("cv score:")
 print(roc_auc_score(train_label, cv_train))
print("current score:", roc_auc_score(train_label, final_cv_train /(s + 1.)), s+1)
 print(fold_scores)
 print(best_trees, np.mean(best_trees))
x_score.append(roc_auc_score(train_label, cv_train))
print(x_score )
X_test = pd.read_csv('.. /input/test_V2.csv')
 X_test = X_test.groupby(['matchId','groupId','matchType'] ).first().reset_index()
X_test = X_test[['matchId','groupId','matchType','numGroups','maxPlace','kills','killPlace']]
group = X_test_grp.groupby(['matchId'])
 X_test_grp['winPlacePerc'] = pred
X_test_grp['_rank.winPlacePerc'] = group['winPlacePerc'].rank(method='min')
 X_test = pd.merge(X_test, X_test_grp )<feature_engineering>
final_cv_pred_16 = final_cv_pred / 16.0
pd.DataFrame({'ID_code': test_id, 'target': final_cv_pred_16} ).to_csv('lgbm_pred_avg.csv', index=False )
<feature_engineering><EOS> 
for i in range(0,200000):
if final_cv_pred_16[i]>=.5:
final_cv_pred_16[i]=1
else:
final_cv_pred_16[i]=0
pd.DataFrame({'ID_code':test_id, 'target': final_cv_pred_16} ).to_csv('lgbm_pred_bin.csv', index=False )