Kaggle competition: MLH - Pokemon Challenge
y_pred = np.argmax(y_hat,axis=1 )<save_to_csv>
sns.set_context("paper")
 print(tf.__version__ )
solution = pd.DataFrame({'ImageId': sample_submission['ImageId'], 'Label': y_pred})
 solution[["ImageId","Label"]].to_csv("CNNPrediction.csv", index=False)
 solution.head()<load_from_csv>
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-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)
 print(multiplier("Water-vs-Fire-None"))
print(multiplier("Water-vs-Fire-Grass"))
print(multiplier("Fire-vs-Water-Fire"))
print(multiplier("Fire-vs-Grass-Bug"))
print(multiplier("None-vs-Grass-Bug"))
train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/train.csv', parse_dates=['Date'])
 test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/test.csv',parse_dates=['Date'])
 submission = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/submission.csv' )<count_duplicates>
class ModelCallback(keras.callbacks.Callback):
def set_params(self, params):
self.epochs = params["epochs"]
def on_epoch_end(self, epoch, epoch_logs):
print("\r", "%5s/%-5s " %(epoch + 1, self.epochs), end="")
 for k, v in epoch_logs.items() :
print("%s: %04.4f "%(k, v), end="" )
train.shape 
<data_type_conversions>
to_underscore = lambda x: re.sub("[^0-9a-zA-Z
load_csv = lambda file: pd.read_csv(".. /input/pokemon-challenge-mlh/%s.csv" % file ).rename(to_underscore, axis='columns')
 to_underscore("First Colum.Value" )
train.columns = train.columns.str.lower()
test.columns = test.columns.str.lower()<drop_column>
def df_pokemon() :
pokemon = load_csv("pokemon" ).fillna("None")
 pokemon = pokemon.drop(["name", "generation", "legendary"], axis = 1)
 pokemon["speed2"] = pokemon["speed"] ** 2
pokemon["speed2"] = pokemon["speed2"] / pokemon["speed2"].max()
pokemon["speed"] = pokemon["speed"] / pokemon["speed"].max()
pokemon["hp"] = pokemon["hp"] / pokemon["hp"].max()
mx = max([pokemon["defense"].max() , pokemon["sp_def"].max() ])
 pokemon["defense"] = pokemon["defense"] / mx
pokemon["sp_def"] = pokemon["sp_def"] / mx
mx = max([pokemon["attack"].max() , pokemon["sp_atk"].max() ])
 pokemon["attack"] = pokemon["attack"] / mx
pokemon["sp_atk"] = pokemon["sp_atk"] / mx
t1 = pd.get_dummies(pokemon["type_1"], prefix='t1_')
 t2 = pd.get_dummies(pokemon["type_2"], prefix='t2_')
 ds = [pokemon, t1, t2]
pokemon = pd\ 
.concat(ds,axis=1)\ 
.rename(to_underscore, axis='columns')
 return pokemon
train.fillna(' ',inplace=True)
 test.fillna(' ', inplace=True)
 train_id = train.pop('id')
 test_id = test.pop('forecastid')
 train['cp'] = train['country_region'] + train['province_state']
test['cp'] = test['country_region'] + test['province_state']
train.drop(['province_state','country_region'], axis=1, inplace=True)
 test.drop(['province_state','country_region'], axis =1, inplace=True )<count_unique_values>
def merge_data(battles, pokemon):
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.get("s_type_2", "None")).map(multiplier)
 battles["s_t1"] =(battles.s_type_1 + "-vs-" + battles.f_type_1 + "-" + battles.get("f_type_2", "None")).map(multiplier)
 battles["f_t2"] =(battles.f_type_2 + "-vs-" + battles.s_type_1 + "-" + battles.get("s_type_2", "None")).map(multiplier)
 battles["s_t2"] =(battles.s_type_2 + "-vs-" + battles.f_type_1 + "-" + battles.get("f_type_2", "None")).map(multiplier)
 battles["f_t"] = battles[["f_t1", "f_t2"]].max(axis=1)
 battles["s_t"] = battles[["s_t1", "s_t2"]].max(axis=1)
 battles["f_t_min"] = battles[["f_t1", "f_t2"]].min(axis=1)
 battles["s_t_min"] = battles[["s_t1", "s_t2"]].min(axis=1)
 battles = battles.drop(["f_type_1", "s_type_1", "f_type_2", "s_type_2", "f_t1", "f_t2", "s_t1", "s_t2"], axis=1)
 battles = battles\ 
.sort_values(['battle_number'])\ 
.reset_index(drop=True)\ 
.drop(["battle_number", "first_pokemon", "second_pokemon", "f_
return battles
train.cp.nunique() , test.cp.nunique()<feature_engineering>
def params(train, units=[]):
X, Xs = to_model(train)
 Y = train["winner"].values
y = keras.utils.to_categorical(Y)
 assert X.shape[0] == y.shape[0]
input_dim = X.shape[1]
outuput_dim = 1 if len(y.shape)== 1 else y.shape[1]
samples = train.shape[0]
print("𝑁𝑖: %s, 𝑁𝑜: %s, 𝑁𝑠: %s" %(input_dim, outuput_dim, samples))
print("units: sum(%s)= %s" %(units, sum(units)))
 return(input_dim, outuput_dim, units, X, Xs, Y, y )
df = pd.DataFrame()
def create_time_feat(data):
df['date']= data['date']
df['hour']=df['date'].dt.hour
df['weekofyear']=df['date'].dt.weekofyear
df['quarter'] =df['date'].dt.quarter
df['month'] = df['date'].dt.month
df['dayofyear']=df['date'].dt.dayofyear
x=df[['hour','weekofyear','quarter','month','dayofyear']]
return x
cr_tr = create_time_feat(train)
 cr_te = create_time_feat(test )<concatenate>
def to_model(X):
if "winner" in X:
X = X.drop(["winner"], axis=1)
 X = X.values
return X, X
train_df = pd.concat([train,cr_tr], axis=1)
 test_df = pd.concat([test, cr_te], axis =1)
 train_df.shape, test_df.shape, train_df.cp.nunique() , test_df.cp.nunique() , test.shape<correct_missing_values>
def remove_duplicate_battles(battles):
return battles\ 
.groupby(["first_pokemon", "second_pokemon", "winner"])\ 
.count() \ 
.reset_index() \ 
.drop(["battle_number"],axis=1)\ 
.sample(frac=1)\ 
.reset_index(drop=True)\ 
.reset_index() \ 
.rename(columns={"index": "battle_number"} )
test_df.dropna(inplace=True )<categorify>
battles_values = load_csv("battles" ).values
acc100 = dict(zip(map(tuple, battles_values[:, 1:-1]), map(tuple, battles_values[:, -1:])) )
le=LabelEncoder()
train_df['cp_le']=le.fit_transform(train_df['cp'])
 test_df['cp_le']=le.transform(test_df['cp'])
 train_df.drop(['cp'], axis=1, inplace=True)
 test_df.drop(['cp'], axis=1, inplace=True )<data_type_conversions>
epochs, validation_split =(600, 0.0)
 history = model.fit(Xs, y, workers=4, epochs=epochs, verbose=0, validation_split=validation_split,
batch_size=512, shuffle=True, callbacks=[ModelCallback() ], initial_epoch=0)
 plot_history(( history), title="", since=0 )
<save_to_csv>
test = load_csv("test")
 X, Xs = to_model(merge_data(super_battle(load_csv("test")) , pokemon))
prediction = model.predict_classes(Xs)
 df_submission = pd.DataFrame({"Winner": prediction}, index = test.index.rename("battle_number"))
for i, p1, p2 in load_csv("test" ).values:
if(p1, p2)in acc100: df_submission.iloc[i] = acc100[(p1, p2)][0]
df_submission.to_csv("solution.csv" )
<count_duplicates>
model.save_weights("cp-{epoch:04d}.ckpt".format(epoch=epochs))
! mkdir "saved_models"
saved_model_path = tf.contrib.saved_model.save_keras_model(model, "./saved_models" )