Kaggle competition: MLH - Pokemon Challenge
X_train_date = X_train[date_col].copy()
X_test_date = X_test[date_col].copy()
X_train_lang = X_train[lang_col].copy()
X_test_lang = X_test[lang_col].copy()
X_train_cats = X_train[cats_col].copy()
X_test_cats = X_test[cats_col].copy()
X_train_num = X_train[num_col].copy()
X_test_num = X_test[num_col].copy()<prepare_x_and_y>
pokemon = pd.read_csv(".. /input/pokemon.csv")
 battles = pd.read_csv(".. /input/battles.csv")
 test = pd.read_csv(".. /input/test.csv" )
X_train_num.fillna(X_train_num.median() ,axis=0, inplace=True)
 X_test_num.fillna(X_train_num.median() ,axis=0, inplace=True )<split>
pokemon = pokemon.drop(columns = ['Name','Generation','Legendary'],errors = 'ignore')
 pokemon['Type 2'] = pokemon['Type 2'].fillna('None')
 pokemon.loc[:,'Type 1'] = pokemon.loc[:,'Type 1'].astype('category')
 pokemon.loc[:,'Type 2'] = pokemon.loc[:,'Type 2'].astype('category')
 pokemon.dtypes
target = 'loan_condition'
X_temp = pd.concat([X_train_cats, y_train], axis=1)
 for col in cats_col:
print(col)
 summary = X_temp.groupby([col])[target].mean()
X_test_cats[col] = X_test_cats[col].map(summary)
 skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 enc_train = pd.Series(np.zeros(len(X_train)) , index=X_train.index)
 for i,(train_ix, val_ix)in enumerate(( skf.split(X_train, y_train))):
X_train_, _ = X_temp.iloc[train_ix], y_train.iloc[train_ix]
X_val, _ = X_temp.iloc[val_ix], y_train.iloc[val_ix]
summary = X_train_.groupby([col])[target].mean()
enc_train.iloc[val_ix] = X_val[col].map(summary)
 X_train_cats[col] = enc_train<data_type_conversions>
data = pd.merge(battles,pokemon,left_on = 'First_pokemon', right_on = '
data = pd.merge(data,pokemon,left_on = 'Second_pokemon', right_on = '
data = data.drop(columns = ['
data = data.sort_values(by = ['battle_number'])
 data.head()
X_train_cats.fillna(X_train_cats.median() ,axis=0, inplace=True)
 X_test_cats.fillna(X_train_cats.median() ,axis=0, inplace=True )<feature_engineering>
data_test = pd.merge(test,pokemon,left_on = 'First_pokemon', right_on = '
data_test = pd.merge(data_test,pokemon,left_on = 'Second_pokemon', right_on = '
data_test = data_test.drop(columns = ['
data_test = data_test.sort_values(by = ['battle_number'])
 data_test.head()
tfidf = TfidfVectorizer(max_features=1000, analyzer='word', ngram_range=(1, 2))
train = []
test = []
for col in lang_col:
train.append(tfidf.fit_transform(X_train_lang[col].fillna('
test.append(tfidf.transform(X_test_lang[col].fillna('<concatenate>
lr = LinearRegression(normalize=True)
 Y_train = data.loc[:,'Winner']
X_train = data.drop(columns = ['Winner'])
 X_train = pd.get_dummies(X_train)
 X_test = pd.get_dummies(data_test)
X_train = pd.concat([X_train_date,X_train_cats, X_train_num], axis=1)
 X_test = pd.concat([X_test_cats , X_test_num], axis=1)
 df_concat = pd.concat([X_train,y_train],axis=1 )<define_variables>
input_dim = X_train.shape[1]
print(np.shape(Y_train))
model = keras.models.Sequential([
keras.layers.Dense(310, activation=tf.nn.relu, input_shape=(X_train.shape[1],)) ,
keras.layers.GaussianNoise(0.16),
keras.layers.Dropout(0.5),
keras.layers.Dense(310, activation=tf.nn.relu),
keras.layers.GaussianNoise(0.16),
keras.layers.Dropout(0.5),
keras.layers.Dense(310, activation=tf.nn.relu),
keras.layers.Dropout(0.5),
keras.layers.Dense(1, activation=tf.nn.sigmoid)
 ])
 model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
 model.summary
train_index_date = df_concat.set_index("issue_d")[:"2015-06-01"].reset_index().index
val_index_date = df_concat.set_index("issue_d")["2015-07-01":].reset_index().index<prepare_x_and_y>
validation_split = 0.2
history = model.fit(X_train, Y_train, workers=4, epochs=400, batch_size = 512, shuffle = True, validation_split=validation_split )
X_inner_train = df_concat.drop(["issue_d","loan_condition"],axis=1)
 y_inner_train = df_concat["loan_condition"]<concatenate>
prediction = model.predict_classes(X_test)
 sampleSubmission = pd.read_csv('.. /input/sampleSubmission.csv')
 sampleSubmission.loc[:,'Winner'] = prediction
sampleSubmission.to_csv('resultados.csv', index=False)
 print(sampleSubmission )