Kaggle competition: MLH - Pokemon Challenge
y_train = df_train.query('issue_d_year > 2014' ).loan_condition
X_train = df_train.query('issue_d_year > 2014' ).drop(['loan_condition'], axis=1)
 X_test = df_test<feature_engineering>
to_underscore = lambda x: re.sub("[^0-9a-zA-Z
X_train['nan_count'] = X_train.isnull().sum(axis=1)
 X_test['nan_count'] = X_test.isnull().sum(axis=1 )<data_type_conversions>
typetable = pd.read_csv(".. /input/pokemon-type-table/typetable.csv")
 vals = []
for c1 in typetable.columns[1:]:
vals.append(pd.DataFrame({
"idx": typetable["atck"].map(lambda x: "%s-%s-" %(x, c1)) ,
"mul": typetable[c1],
}))
for c2 in typetable.columns[1:]:
vals.append(pd.DataFrame({
"idx": typetable["atck"].map(lambda x: "%s-%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)
for col in X_train.columns:
if X_train[col].isnull().any() :
X_train['nanflg_'+col] = X_train[col].isnull().astype(int)
 for col in X_test.columns:
if X_test[col].isnull().any() :
X_test['nanflg_'+col] = X_test[col].isnull().astype(int )<feature_engineering>
print(multiplier("Grass-Rock-"))
print(multiplier("Rock-Grass-"))
print(multiplier("Water-Fire-"))
print(multiplier("Water-Fire-Grass"))
print(multiplier("Fire-Water-Fire"))
print(multiplier("Fire-Grass-Bug"))
print(multiplier("-Grass-Bug"))
X_train['amnt_per_inc'] = X_train['loan_amnt']/X_train['annual_inc']
X_test['amnt_per_inc'] = X_test['loan_amnt']/X_test['annual_inc']<feature_engineering>
pokemon = pd.read_csv(".. /input/pokemon-challenge-mlh/pokemon.csv" ).rename(to_underscore, axis='columns' ).fillna("")
 pokemon["legendary"] = pokemon["legendary"].map(int)
 pokemon.head()
X_train['amnt_per_revol'] = X_train['loan_amnt']/X_train['revol_bal']
X_test['amnt_per_revol'] = X_test['loan_amnt']/X_test['revol_bal']<feature_engineering>
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"] + "-" + battles["s_type_1"] + "-" + battles["s_type_2"] ).map(multiplier)
 battles["f_t2"] =(battles["f_type_2"] + "-" + battles["s_type_1"] + "-" + battles["s_type_2"] ).map(multiplier)
 battles["s_t1"] =(battles["s_type_1"] + "-" + battles["f_type_1"] + "-" + battles["f_type_2"] ).map(multiplier)
 battles["s_t2"] =(battles["s_type_2"] + "-" + battles["f_type_1"] + "-" + battles["f_type_2"] ).map(multiplier)
 battles["f_attack"] = battles["f_attack"] * battles[["f_t1", "f_t2"]].max(axis=1)
 battles["s_attack"] = battles["s_attack"] * battles[["s_t1", "s_t2"]].max(axis=1)
 battles = battles\ 
.drop(["f_type_1", "f_type_2", "s_type_1", "s_type_2", "f_name", "s_name"], axis=1)\ 
.sort_values(['battle_number'])\ 
.reset_index() \ 
.drop(["index","battle_number", "f_
return battles
def feature_scaling(train):
sc = StandardScaler()
x_train = pd.DataFrame(sc.fit_transform(train), columns = train.columns)
 if 'winner' in x_train.columns: x_train["winner"] = train["winner"]
if 'f_t1' in x_train.columns: x_train["f_t1"] = train["f_t1"]
if 'f_t2' in x_train.columns: x_train["f_t2"] = train["f_t2"]
if 's_t1' in x_train.columns: x_train["s_t1"] = train["s_t1"]
if 's_t2' in x_train.columns: x_train["s_t2"] = train["s_t2"]
return x_train
battles = pd.read_csv(".. /input/pokemon-challenge-mlh/battles.csv" ).rename(to_underscore, axis='columns')
 train = merge_data(battles)
 train.head()
X_train['amnt_per_cur'] = X_train['loan_amnt']/X_train['tot_cur_bal']
X_test['amnt_per_cur'] = X_test['loan_amnt']/X_test['tot_cur_bal']<feature_engineering>
train_split, test_split = train_test_split(train, test_size = 0.1, random_state = 0)
 features = ["first_pokemon", "f_hp", "f_attack", "f_sp_atk", "f_sp_def", "f_speed", "second_pokemon", "s_hp", "s_attack", "s_sp_atk", "s_sp_def", "s_speed", "f_t1","f_t2", "s_t1", "s_t2"]
d_train = lgb.Dataset(train_split[features], label=np.ravel(train_split["winner"]))
d_test = lgb.Dataset(test_split[features], label=np.ravel(test_split["winner"]))
params = {
'objective':'binary',
'max_depth': -1,
'learning_rate': 0.05,
'boosting': "gbdt",
'metric': ['acu','binary_logloss', 'binary_error'],
'num_rounds': 90000,
'early_stopping': 2000}
def set_values(data):
for i in range(len(data)) :
data[i] = 1 if data[i] >=.5 else 0
return data
clf = lgb.train(params, d_train, valid_sets=[d_test], verbose_eval=1000)
 print("DONE!" )
X_train['amnt_per_acc'] = X_train['loan_amnt']/X_train['total_acc']
X_test['amnt_per_acc'] = X_test['loan_amnt']/X_test['total_acc']<feature_engineering>
pred_train = clf.predict(train_split[features], num_iteration=clf.best_iteration)
 pred_test = clf.predict(test_split[features], num_iteration=clf.best_iteration)
 metrics = []
for x in np.arange (.3,.7,.01):
t = pred_train.copy()
t[t >= x] = int(1)
 t[t < x] = int(0)
 metrics.append(( x,sk_metric(train_split["winner"], t)))
 threshold = max(metrics, key=lambda item:item[1])[0]
print("Winner threashold: ", threshold)
 print(pred_train)
 pred_test[pred_test >= threshold] = 1
pred_test[pred_test < threshold] = 0
pred_train[pred_train >= threshold] = 1
pred_train[pred_train < threshold] = 0
print(pred_train)
 accuracy_train = accuracy_score(pred_train, np.ravel(train_split["winner"]))
accuracy_test = accuracy_score(pred_test, np.ravel(test_split["winner"]))
print(accuracy_train)
 print(accuracy_test )
X_train['revol_per_acc'] = X_train['revol_bal']/X_train['total_acc']
X_test['revol_per_acc'] = X_test['revol_bal']/X_test['total_acc']<feature_engineering>
battles = pd.read_csv(INPUT_DIR + "test.csv" ).rename(to_underscore, axis='columns')
 test = merge_data(battles)
 col_names = test.columns
test = pd.DataFrame(test, columns = col_names)
 test.head()
<feature_engineering><EOS> 
predictions = clf.predict(test[features])
 predictions[predictions >= threshold] = 1
predictions[predictions < threshold] = 0
predictions = predictions.astype(int)
 sampleSubmission = pd.read_csv(INPUT_DIR + 'sampleSubmission.csv')
 sampleSubmission['Winner'] = predictions
sampleSubmission.to_csv('my_sub.csv', index=False)
 sampleSubmission.head()