Kaggle competition: MLH - Pokemon Challenge
for i,col in enumerate(lang_col):
X_inner_train = sp.sparse.hstack([X_inner_train, train[i]])
 X_test = sp.sparse.hstack([X_test, test[i]])
 X_inner_train = X_inner_train.tocsr()
X_test = X_test.tocsr()<drop_column>
to_underscore = lambda x: re.sub("[^0-9a-zA-Z
del X_train_date, X_test_date, X_train_lang, X_test_lang, X_train_cats, X_test_cats, X_train_num, X_test_num
gc.collect()<load_pretrained>
pokemon = pd.read_csv('.. /input/pokemon-challenge-mlh/pokemon.csv' ).rename(to_underscore, axis='columns' ).fillna("None")
 battles = pd.read_csv('.. /input/pokemon-challenge-mlh/battles.csv' ).rename(to_underscore, axis='columns' ).fillna("None")
 sampleSubmission = pd.read_csv('.. /input/pokemon-challenge-mlh/sampleSubmission.csv' ).rename(to_underscore, axis='columns' ).fillna("None")
 test = pd.read_csv('.. /input/pokemon-challenge-mlh/test.csv' ).rename(to_underscore, axis='columns' ).fillna("None")
 typetable = pd.read_csv('.. /input/pokemon-type-table/typetable.csv')
 print("pokemon", pokemon.shape)
 print("battles", battles.shape)
 print("sampleSubmision", sampleSubmission.shape)
 print("test", test.shape)
 print("typetable", typetable.shape )
def objectives(trial):
params = {
'random_seed' : 71,
'num_leaves' : trial.suggest_int('num_leaves', 2, 256),
'min_child_samples': int(trial.suggest_loguniform('min_child_samples', 100, 10000)) ,
'min_child_weight' : trial.suggest_loguniform('min_child_weight', 0.1, 2000),
'subsample' : trial.suggest_uniform('subsample', 0.8, 1),
'colsample_bytree' : trial.suggest_uniform('colsample_bytree', 0.5, 1),
'learning_rate' : trial.suggest_loguniform('learning_rate', 0.025, 0.5),
'min_data_in_leaf' : int(trial.suggest_loguniform('min_data_in_leaf', 1, 1000)) ,
}
scores = []
skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(skf.split(X_inner_train, y_inner_train)) :
X_train_, y_train_ = X_inner_train[train_ix,:], y_inner_train.values[train_ix]
X_val, y_val = X_inner_train[test_ix,:], y_inner_train.values[test_ix]
model = lgb.LGBMRegressor(**params)
 model.fit(X_train_, y_train_,
eval_set=(X_val,y_val),
early_stopping_rounds=20,
eval_metric='auc',
verbose=False)
 y_inner_pred = model.predict(X_val)
 score = roc_auc_score(y_val,y_inner_pred)
 scores.append(score)
 return np.mean(scores )<find_best_params>
pokemon = pokemon.drop(labels=["name"], axis=1)
 pokemon = pokemon.fillna({'Type 2': 'None'})
 pokemon.head()
%%time
opt = optuna.create_study(direction='maximize',sampler=optuna.samplers.RandomSampler(seed=71))
opt.optimize(objectives, n_trials=100 )<find_best_params>
columns = ['attack', 'defense', 'hp', 'speed', 'sp_atk', 'sp_def']
pokemon[columns] = MinMaxScaler().fit_transform(pokemon[columns])
 pokemon.head()
trial = opt.best_trial
params_best = dict(trial.params.items())
 params_best['random_seed'] = 71
params_best["num_leaves"] = int(params_best["num_leaves"])
 params_best["min_data_in_leaf"] = int(params_best["min_data_in_leaf"] )<find_best_params>
pokemon["legendary"] = pokemon["legendary"].astype(int)
 pokemon.head()
params_best2 = dict(trial.params.items())
 params_best2['random_seed'] = 123
params_best2["num_leaves"] = int(params_best["num_leaves"])
 params_best2["min_data_in_leaf"] = int(params_best["min_data_in_leaf"] )<define_variables>
vals = []
for c1 in typetable.columns[1:]:
vals.append(pd.DataFrame({
"idx": typetable["atck"].map(lambda x: "%s-vs-%s-None" %(x, c1)) ,
"mul": typetable[c1],
}))
for c2 in typetable.columns[1:]:
vals.append(pd.DataFrame({
"idx": typetable["atck"].map(lambda x: "%s-vs-%s-%s" %(x, c1, c2)) ,
"mul": typetable[c1] * typetable[c2],
}))
mult = pd.concat(vals ).reset_index().drop(["index"], axis=1)
 mult = dict(zip(mult.values[:,0], mult.values[:,1]))
def multiplier(cat):
return mult.get(cat, 0)
 mult
list_params_best_skf = [params_best,params_best2]<load_pretrained>
def merge_data(battles):
battles = battles \ 
.merge(pokemon.rename(lambda x: "f_%s" % x, axis="columns"), left_on="first_pokemon", right_on="f_ 
.merge(pokemon.rename(lambda x: "s_%s" % x, axis="columns"), left_on="second_pokemon", right_on="s_
battles["f_t1"] =(battles["f_type_1"] + "-vs-" + battles["s_type_1"] + "-" + battles["s_type_2"] ).map(multiplier)
 battles["f_t2"] =(battles["f_type_2"] + "-vs-" + battles["s_type_1"] + "-" + battles["s_type_2"] ).map(multiplier)
 battles["s_t1"] =(battles["s_type_1"] + "-vs-" + battles["f_type_1"] + "-" + battles["f_type_2"] ).map(multiplier)
 battles["s_t2"] =(battles["s_type_2"] + "-vs-" + battles["f_type_1"] + "-" + battles["f_type_2"] ).map(multiplier)
 battles["speed"] =(battles["f_speed"] - battles["s_speed"])
 battles = battles\ 
.sort_values(['battle_number'])\ 
.reset_index() \ 
.drop(["index","battle_number", "first_pokemon", "second_pokemon", "f_
return battles
print(battles.head())
 train = merge_data(battles)
 train.head()
def objectives(trial):
params = {
'random_seed' : 71,
'num_leaves' : trial.suggest_int('num_leaves', 2, 256),
'min_child_samples': int(trial.suggest_loguniform('min_child_samples', 100, 10000)) ,
'min_child_weight' : trial.suggest_loguniform('min_child_weight', 0.1, 2000),
'subsample' : trial.suggest_uniform('subsample', 0.8, 1),
'colsample_bytree' : trial.suggest_uniform('colsample_bytree', 0.5, 1),
'learning_rate' : trial.suggest_loguniform('learning_rate', 0.025, 0.5),
'min_data_in_leaf' : int(trial.suggest_loguniform('min_data_in_leaf', 1, 1000)) ,
}
X_train_, y_train_ = X_inner_train[train_index_date,:], y_inner_train.values[train_index_date]
X_val, y_val = X_inner_train[val_index_date,:], y_inner_train.values[val_index_date]
model = lgb.LGBMRegressor(**params)
 model.fit(X_train_, y_train_,
eval_set=(X_val,y_val),
early_stopping_rounds=20,
eval_metric='auc',
verbose=False)
 y_inner_pred = model.predict(X_val)
 score = roc_auc_score(y_val,y_inner_pred)
 return score<find_best_params>
train = pd.get_dummies(train )
%%time
opt = optuna.create_study(direction='maximize',sampler=optuna.samplers.RandomSampler(seed=71))
opt.optimize(objectives, n_trials=100 )<find_best_params>
Y_train = train["winner"]
X_train = train.drop(labels=["winner"],axis=1)
 print(X_train.shape)
 print(Y_train.shape )
seed_i = [71,123,51,17,91]
list_params_best_tss = []
for i in range(5):
trial = opt.best_trial
params_best = dict(trial.params.items())
 params_best['random_seed'] = seed_i[i]
params_best["num_leaves"] = int(params_best["num_leaves"])
 params_best["min_data_in_leaf"] = int(params_best["min_data_in_leaf"])
 list_params_best_tss.append(params_best )<load_from_csv>
X_train, X_val, y_train, y_val = train_test_split(X_train, Y_train, test_size=0.1, random_state = 3 )
submission = pd.read_csv('.. /input/homework-for-students4plus/sample_submission.csv', index_col=0 )<train_model>
model = GradientBoostingClassifier(learning_rate=0.01,
min_samples_split=3,
max_depth=10,
n_estimators=2000,
random_state=2 )
y_pred_test = np.zeros(len(submission))
for i,(train_ix, test_ix)in enumerate(skf.split(X_inner_train, y_inner_train)) :
print(i)
 X_train_, y_train_ = X_inner_train[train_ix,:], y_inner_train.values[train_ix]
X_val, y_val = X_inner_train[test_ix,:], y_inner_train.values[test_ix]
for j in list_params_best_skf:
params_best = j
model = lgb.LGBMRegressor(**params_best)
 model.fit(X_train_, y_train_,
eval_set=(X_val,y_val),
early_stopping_rounds=20,
eval_metric='auc',
verbose=False)
 y_inner_pred = model.predict(X_test)
 y_pred_test += y_inner_pred<train_model>
model.fit(X_train, y_train )
X_train_, y_train_ = X_inner_train[train_index_date,:], y_inner_train.values[train_index_date]
X_val, y_val = X_inner_train[val_index_date,:], y_inner_train.values[val_index_date]
for index,j in enumerate(list_params_best_tss):
print(index)
 params_best = j
model = lgb.LGBMRegressor(**params_best)
 model.fit(X_train_, y_train_,
eval_set=(X_val,y_val),
early_stopping_rounds=20,
eval_metric='auc',
verbose=False)
 y_inner_pred = model.predict(X_test)
 y_pred_test += y_inner_pred<prepare_output>
model.fit(X_train, y_train )
y_pred = y_pred_test / 20
y_pred<save_to_csv>
y_val = np.array(y_val, dtype='int32')
 y_val
submission = pd.read_csv('.. /input/homework-for-students4plus/sample_submission.csv', index_col=0)
 submission.loan_condition = y_pred
submission.to_csv('submission.csv' )<set_options>
predictions = model.predict(X_val)
 res = model.score(X_val, y_val)
 predictions
plt.style.use('ggplot')
 %matplotlib inline 
<load_from_csv>
X_test = merge_data(test)
 X_test = pd.get_dummies(X_test)
 X_test.head()
df_train = pd.read_csv('.. /input/homework-for-students4plus/train.csv', index_col=0)
 df_test = pd.read_csv('.. /input/homework-for-students4plus/test.csv', index_col=0 )<data_type_conversions>
predictions_test = model.predict(X_test)
 predictions_test
result = np.squeeze(np.around(predictions_test ).astype(int))
result
df_train['issue_d_year']= pd.to_datetime(df_train['issue_d'] ).apply(lambda x: int(x.strftime('%Y')))
 df_test['issue_d_year']= pd.to_datetime(df_test['issue_d'] ).apply(lambda x: int(x.strftime('%Y')) )<prepare_x_and_y>
data_to_submit = pd.DataFrame(columns=['Winner'])
 data_to_submit['Winner'] = result
data_to_submit.insert(0, 'battle_number', range(0, len(data_to_submit)))
 data_to_submit
data_to_submit.to_csv('submission.csv', index = False)
 data_to_submit