Kaggle competition: MLH - Pokemon Challenge
def create_date_feat(data, cf, ft):
for d in data['date'].drop_duplicates() :
for i in data['cp_le'].drop_duplicates() :
org_mask =(data['date']==d)&(data['cp_le']==i)
 for lag in range(1,15):
mask_loc =(data['date']==(d-pd.Timedelta(days=lag)))&(data['cp_le']==i)
 try:
data.loc[org_mask, 'cf_' + str(lag)]=data.loc[mask_loc, cf].values
data.loc[org_mask, 'ft_' + str(lag)]=data.loc[mask_loc, ft].values
except:
data.loc[org_mask, 'cf_' + str(lag)]=0.0
data.loc[org_mask, 'ft_' + str(lag)]=0.0
create_date_feat(train_df,'confirmedcases','fatalities' )<drop_column>
battles = pd.read_csv(".. /input/battles.csv")
 pokemon = pd.read_csv(".. /input/pokemon.csv")
 pokemon.describe()
cf_feat = ['cp_le', 'weekofyear','quarter','month','dayofyear','cf_1', 'cf_2', 'cf_3',
'cf_4', 'cf_5', 'cf_6', 'cf_7', 'cf_8', 'cf_9','cf_10', 'cf_11', 'cf_12',
'cf_13', 'cf_14']
ft_feat = ['cp_le', 'weekofyear','quarter','month','dayofyear','ft_1', 'ft_2', 'ft_3',
'ft_4', 'ft_5', 'ft_6', 'ft_7', 'ft_8', 'ft_9','ft_10', 'ft_11', 'ft_12',
'ft_13', 'ft_14']
train_x_cf = train_df[cf_feat]
print(train_x_cf.shape)
 train_x_ft = train_df[ft_feat]
print(train_x_ft.shape)
 train_x_cf_reshape = train_x_cf.values.reshape(train_x_cf.shape[0],1,train_x_cf.shape[1])
 train_x_ft_reshape = train_x_ft.values.reshape(train_x_ft.shape[0],1,train_x_ft.shape[1])
 train_y_cf = train_df['confirmedcases']
train_y_ft = train_df['fatalities']
train_y_cf_reshape = train_y_cf.values.reshape(-1,1)
 train_y_ft_reshape = train_y_ft.values.reshape(-1,1)
 tr_x_cf, val_x_cf, tr_y_cf, val_y_cf = train_test_split(train_x_cf_reshape, train_y_cf_reshape, test_size=0.2, random_state=0)
 tr_x_ft, val_x_ft, tr_y_ft, val_y_ft = train_test_split(train_x_ft_reshape, train_y_ft_reshape, test_size=0.2, random_state=0)
<choose_model_class>
def normalizeColumns(columns, dataset):

 for column in columns:
dataset[column] =(dataset[column] - dataset[column].mean())/ dataset[column].std()
def rmsle(pred,true):
assert pred.shape[0]==true.shape[0]
return K.sqrt(K.mean(K.square(K.log(pred+1)- K.log(true+1))))
es = EarlyStopping(monitor='val_loss', min_delta = 0, verbose=0, patience=10, mode='auto')
 mc_cf = ModelCheckpoint('model_cf.h5', monitor='val_loss', verbose=0, save_best_only=True)
 mc_ft = ModelCheckpoint('model_ft.h5', monitor='val_loss', verbose=0, save_best_only=True)
 def lstm_model(hidden_nodes, second_dim, third_dim):
model = Sequential([LSTM(hidden_nodes, input_shape=(second_dim, third_dim), activation='relu'),
Dense(64, activation='relu'),
Dense(32, activation='relu'),
Dense(1, activation='relu')])
 model.compile(loss=rmsle, optimizer = 'adam')
 return model
model_cf = lstm_model(10, tr_x_cf.shape[1], tr_x_cf.shape[2])
 model_ft = lstm_model(10, tr_x_ft.shape[1], tr_x_ft.shape[2])
 history_cf = model_cf.fit(tr_x_cf, tr_y_cf, epochs=200, batch_size=512, validation_data=(val_x_cf,val_y_cf), callbacks=[es,mc_cf])
 history_ft = model_ft.fit(tr_x_ft, tr_y_ft, epochs=200, batch_size=512, validation_data=(val_x_ft,val_y_ft), callbacks=[es,mc_ft] )<define_variables>
pokemon.fillna("noType", inplace=True)
 pokemon = pd.concat([
pokemon,
pd.get_dummies(pokemon["Type 1"], prefix="t1"),
pd.get_dummies(pokemon["Type 2"], prefix="t2"),
], axis=1)
 pokemon.drop(["Type 1", "Type 2", "Name", "Generation", "Legendary"], axis=1, inplace=True)
 normalizeColumns(["HP", "Sp.Atk", "Sp.Def", "Attack", "Defense", "Speed"], pokemon )
feat = ['confirmedcases','fatalities','cf_1', 'ft_1', 'cf_2', 'ft_2', 'cf_3', 'ft_3',
'cf_4', 'ft_4', 'cf_5', 'ft_5', 'cf_6', 'ft_6', 'cf_7', 'ft_7', 'cf_8', 'ft_8',
'cf_9', 'ft_9', 'cf_10', 'ft_10', 'cf_11', 'ft_11', 'cf_12', 'ft_12', 'cf_13', 'ft_13',
'cf_14', 'ft_14']
c_feat = ['cp_le', 'weekofyear','quarter','month','dayofyear','cf_1', 'cf_2', 'cf_3',
'cf_4', 'cf_5', 'cf_6', 'cf_7', 'cf_8', 'cf_9','cf_10', 'cf_11', 'cf_12',
'cf_13', 'cf_14']
f_feat = ['cp_le', 'weekofyear','quarter','month','dayofyear','ft_1', 'ft_2', 'ft_3',
'ft_4', 'ft_5', 'ft_6', 'ft_7', 'ft_8', 'ft_9','ft_10', 'ft_11', 'ft_12',
'ft_13', 'ft_14']
tot_feat = ['cp_le', 'weekofyear','quarter','month','dayofyear','cf_1', 'ft_1', 'cf_2', 'ft_2', 'cf_3', 'ft_3',
'cf_4', 'ft_4', 'cf_5', 'ft_5', 'cf_6', 'ft_6', 'cf_7', 'ft_7', 'cf_8', 'ft_8',
'cf_9', 'ft_9', 'cf_10', 'ft_10', 'cf_11', 'ft_11', 'cf_12', 'ft_12', 'cf_13', 'ft_13',
'cf_14', 'ft_14']
test_new = test_df.copy().join(pd.DataFrame(columns=feat))
test_mask =(test_df['date'] <= train_df['date'].max())
 train_mask =(train_df['date'] >= test_df['date'].min())
 test_new.loc[test_mask,feat] = train_df.loc[train_mask, feat].values
future_df = pd.date_range(start = train_df['date'].max() +pd.Timedelta(days=1),end=test_df['date'].max() , freq='1D')
 def create_add_trend_pred(data, cf, ft):
for d in future_df:
for i in data['cp_le'].drop_duplicates() :
org_mask =(data['date']==d)&(data['cp_le']==i)
 for lag in range(1,15):
mask_loc =(data['date']==(d-pd.Timedelta(days=lag)))&(data['cp_le']==i)
 try:
data.loc[org_mask, 'cf_' + str(lag)]=data.loc[mask_loc,cf].values
data.loc[org_mask, 'ft_' + str(lag)]=data.loc[mask_loc,ft].values
except:
data.loc[org_mask, 'cf_' + str(lag)]=0.0
data.loc[org_mask, 'ft_' + str(lag)]=0.0
test_x = data.loc[org_mask,tot_feat]
test_x_cf = test_x[c_feat]
test_x_cf = test_x_cf.to_numpy().reshape(1,-1)
 test_x_cf_reshape = test_x_cf.reshape(test_x_cf.shape[0],1,test_x_cf.shape[1])
 test_x_ft = test_x[f_feat]
test_x_ft = test_x_ft.to_numpy().reshape(1,-1)
 test_x_ft_reshape = test_x_ft.reshape(test_x_ft.shape[0],1,test_x_ft.shape[1])
 data.loc[org_mask, cf] = model_cf.predict(test_x_cf_reshape)
 data.loc[org_mask, ft] = model_ft.predict(test_x_ft_reshape)
 create_add_trend_pred(test_new, 'confirmedcases', 'fatalities' )<save_to_csv>
def mergePokemonStats(battles, pokemon):
data = battles \ 
.merge(pokemon, left_on="First_pokemon", right_on=" 
.merge(pokemon, left_on="Second_pokemon", right_on=" 
.sort_values(['battle_number'])\ 
.drop(["
data = data.reindex(sorted(data.columns), axis=1)
 return data
sub_pred = pd.DataFrame({'ForecastId': test_id, 'ConfirmedCases':test_new['confirmedcases'],'Fatalities':test_new['fatalities']})
 sub_pred.to_csv('submission.csv', index=False )<import_modules>
data = mergePokemonStats(battles, pokemon)
 data.head()
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
y = data["Winner"].values
x = data.drop(["Winner"],axis=1 ).values
train_df = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_df.info() 
<load_from_csv>
import tensorflow as tf
import sklearn.model_selection
import sklearn.ensemble
import sklearn.metrics
test_df = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_df.info()<count_missing_values>
mlp = tf.keras.Sequential([
tf.keras.layers.Dense(310, activation=tf.nn.relu),
tf.keras.layers.GaussianNoise(0.17),
tf.keras.layers.Dropout(0.50),
tf.keras.layers.Dense(310, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.50),
tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)
 ] )
train_df["Age"].isnull().sum()<count_missing_values>
mlp.compile(optimizer="adam",
loss="binary_crossentropy",
metrics=["accuracy"] )
test_df["Age"].isnull().sum()<data_type_conversions>
history = mlp.fit(
 x,
y,
epochs=350,
workers=4,
batch_size=512,
shuffle=True,
validation_split=0.1
)
data = [train_df, test_df]
for dataset in data:
mean = train_df["Age"].mean()
dataset['Age'] = dataset['Age'].fillna(mean)
 dataset["Age"] = train_df["Age"].astype(float)
 for dataset in data:
dataset.loc[ dataset['Age'] <= 15, 'Age'] = 0
dataset.loc[(dataset['Age'] > 15)&(dataset['Age'] <= 25), 'Age'] = 1
dataset.loc[(dataset['Age'] > 25)&(dataset['Age'] <= 35), 'Age'] = 2
dataset.loc[(dataset['Age'] > 35)&(dataset['Age'] <= 40), 'Age'] = 3
dataset.loc[(dataset['Age'] > 40)&(dataset['Age'] <= 45), 'Age'] = 4
dataset.loc[(dataset['Age'] > 45)&(dataset['Age'] <= 50), 'Age'] = 5
dataset.loc[ dataset['Age'] > 50, 'Age'] = 6
train_df['Age'].value_counts()<count_values>
x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x, y, test_size=0.1)
 gradientBoosting = sklearn.ensemble.GradientBoostingClassifier(
 n_estimators=1000,
max_depth=10,
verbose = 1 
)
 gradientBoosting.fit(x_train, y_train )
train_df.Cabin.value_counts() 
<categorify>
sklearn.metrics.accuracy_score(y_test, gradientBoosting.predict(x_test), normalize=True, sample_weight=None )
deck = {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5, "F": 6, "G": 7, "O": 8}
data = [train_df, test_df]
for dataset in data:
dataset['CabinDeck'] = dataset['Cabin'].fillna("O")
 dataset['CabinDeck'] = dataset['Cabin'].astype(str ).str[0]
dataset['CabinDeck'] = dataset['CabinDeck'].map(deck)
 dataset['CabinDeck'] = dataset['CabinDeck'].fillna(0)
 dataset['CabinDeck'] = dataset['CabinDeck'].astype(int)
 train_df.head(10 )<drop_column>
submission = pd.read_csv(".. /input/test.csv")
 test = mergePokemonStats(submission, pokemon )
train_df = train_df.drop(['Cabin'], axis=1)
 test_df.drop(['Cabin'], axis=1, inplace = True )<count_values>
predictionMLP = np.transpose(mlp.predict(test.values))
predictionGB = gradientBoosting.predict_proba(test.values)
 predictionGB = np.array([predict[1] for predict in predictionGB])
 prediction =(( predictionMLP)+(0.10*predictionGB)) /1.10
prediction[prediction <= 0.5] = 0
prediction[prediction > 0.5] = 1
test_df['CabinDeck'].value_counts()<count_values>
submission["Winner"] = prediction[0].astype("int")
 submission.drop(["First_pokemon", "Second_pokemon"], axis=1, inplace=True)
 submission.head()
train_df.Embarked.value_counts() 
<feature_engineering>
submission.to_csv("submission.csv", index=False )
for dataset in data:
dataset['Fare'] = dataset['Fare'].fillna(0)
 for dataset in data:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[(dataset['Fare'] > 31)&(dataset['Fare'] <= 99), 'Fare'] = 3
dataset.loc[(dataset['Fare'] > 99)&(dataset['Fare'] <= 250), 'Fare'] = 4
dataset.loc[ dataset['Fare'] > 250, 'Fare'] = 5
dataset['Fare'] = dataset['Fare'].astype(int)
 train_df['Fare'].value_counts()<count_values>
submission.to_csv("submission.csv", index=False )