Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
learn.load('s1');<find_best_params>
%matplotlib inline
learn.lr_find()<train_model>
pd.set_option('display.float_format', '{:.2f}'.format)
 pd.set_option('display.max_columns', 200)
 pd.set_option('display.max_rows', 300)
learn.fit_one_cycle(10,max_lr=slice(1e-6,1e-5))<choose_model_class>
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )
interp = ClassificationInterpretation.from_learner(learn )<create_dataframe>
def reload() :
gc.collect()
df = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/train_V2.csv')
 invalid_match_ids = df[df['winPlacePerc'].isna() ]['matchId'].values
df = df[-df['matchId'].isin(invalid_match_ids)]
return df
learn1 = learn.load('s1')
 sub_df = pd.DataFrame(columns=['ImageId','Label'] )<normalization>
train = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/train_V2.csv')
 train = reduce_mem_usage(train )
def get_img(data):
t1 = data.reshape(28,28)/255
t1 = np.stack([t1]*3,axis=0)
 img = Image(FloatTensor(t1))
return img<import_modules>
for c in ['Id','groupId','matchId']:
print(f'unique [{c}] count:', train[c].nunique() )
from fastprogress import progress_bar<save_to_csv>
display(list(train.columns[train.dtypes != 'object']))
display(list(train.columns[train.dtypes == 'object']))
sub_df.to_csv('submission.csv',index=False )<import_modules>
train = train.dropna(axis='rows')
 display(train.isna().sum() )
print(os.listdir(".. /input")) 
<load_from_csv>
def delete_cheaters(df):
df.drop(df[df['roadKills'] >= 10].index, inplace=True)
 df.drop(df[df['kills'] >= 50].index, inplace=True)
 df.drop(df[df['longestKill'] >= 1000].index, inplace=True)
 df.drop(df[df['walkDistance'] >= 13000].index, inplace=True)
 df.drop(df[df['rideDistance'] >= 25000].index, inplace=True)
 df.drop(df[df['swimDistance'] >= 1500].index, inplace=True)
 df.drop(df[df['weaponsAcquired'] >= 50].index, inplace=True)
 df.drop(df[df['heals'] >= 40].index, inplace=True)
 df = df.drop(df[df['killStreaks'] >= 10].index)
 df.drop(df[(df['walkDistance']<=10.0)&(df['damageDealt'] >= 1000)].index, inplace=True)
 df.drop(df[(df['walkDistance']<=10.0)&(df['kills'] >= 5)].index, inplace=True)
 df.drop(df[(df['walkDistance']<=10.0)&(df['heals'] >= 5)].index, inplace=True)
 df.drop(df[(df['walkDistance']<=10.0)&(df['headshotKills'] >= 5)].index, inplace=True)
 df.drop(df[(df['walkDistance']<=10.0)&(df['headshotKills'] >= 5)].index, inplace=True)
 return df
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 print("Train shape : ",train_df.shape)
 print("Test shape : ",test_df.shape )<concatenate>
def feature_engineering(df):
df['heals_and_boosts'] = df['heals']+df['boosts']
df['total_distance'] = df['walkDistance']+df['rideDistance']+df['swimDistance']
df['kills_over_walkDistance'] = df['kills'] / df['walkDistance']
df['killPlace_over_maxPlace'] = df['killPlace'] / df['maxPlace']
df['players_joined'] = df.groupby('matchId')['matchId'].transform('count')
 df['players_in_team'] = df.groupby('groupId')['matchId'].transform('count')
 df.drop(['boosts','heals'], axis=1, inplace=True)
 df.drop(['rideDistance','swimDistance','matchDuration'], axis=1, inplace=True)
 df.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True)
 df.drop(['headshotKills','roadKills','vehicleDestroys','teamKills'], axis=1, inplace=True)
 match = df.groupby('matchId')
 df['killsPerc'] = match['kills'].rank(pct=True ).values
df['killPlacePerc'] = match['killPlace'].rank(pct=True ).values
df['walkDistancePerc'] = match['walkDistance'].rank(pct=True ).values
df['walkPerc_killsPerc'] = df['walkDistancePerc'] / df['killsPerc']
check_cols = ['kills_over_walkDistance', 'killPlace_over_maxPlace', 'walkPerc_killsPerc']
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
for c in check_cols: df[c].fillna(0, inplace=True)
 numcols = df.select_dtypes(include='number' ).columns
use_cols = numcols[numcols != 'winPlacePerc']
rank_cols = use_cols.drop(['numGroups', 'maxPlace', 'players_joined', 'players_in_team'])
 stat_cols = use_cols.drop(['numGroups', 'maxPlace', 'players_joined', 'players_in_team',
'assists', 'longestKill', 'weaponsAcquired','heals_and_boosts',
'total_distance', 'kills_over_walkDistance'])
 group = df.groupby(['matchId', 'groupId'])
 df = df.merge(group[stat_cols].median() , suffixes=['', '_team_median'], how='left',
on=['matchId', 'groupId'])
 df = reduce_mem_usage(df)
 df = df.merge(group[stat_cols].max() , suffixes=['', '_team_max'], how='left',
on=['matchId', 'groupId'])
 df = reduce_mem_usage(df)
 df = df.merge(group[stat_cols].min() , suffixes=['', '_team_min'], how='left',
on=['matchId', 'groupId'])
 df = reduce_mem_usage(df)
 df = df.merge(group[stat_cols].mean() , suffixes=['', '_team_mean'], how='left',
on=['matchId', 'groupId'])
 df = reduce_mem_usage(df)
 df = df.merge(group[stat_cols].sum() , suffixes=['', '_team_sum'], how='left',
on=['matchId', 'groupId'])
 df = reduce_mem_usage(df)
 df = df.merge(group[rank_cols].mean().groupby('matchId')[rank_cols].rank(pct=True),
suffixes=['', '_team_mean_rank'], how='left', on=['matchId', 'groupId'])
 df = reduce_mem_usage(df)
 mapper = lambda x: 'solo' if 'solo' in x else 'duo' if('duo' in x)or('crash' in x)else 'squad'
df['matchType'] = df['matchType'].apply(mapper)
 df = pd.get_dummies(df, columns=['matchType'])
 df.drop(['Id', 'matchId','groupId'], axis=1, inplace=True)
 constant_column = [col for col in df.columns if df[col].nunique() == 1]
print('drop constant columns:', constant_column)
 df.drop(constant_column, axis=1, inplace=True)
 assert df.isna().sum().sum() == 0
return df
tabla_completa = train_df.append(test_df, ignore_index=True )<feature_engineering>
def isolation_forest(df, contamination):
cols = list(df.columns[df.dtypes != 'object'])
 train_df = df[cols]
outlier_detect = IsolationForest(n_estimators=500,
contamination=contamination,
max_features=train_df.shape[1])
 outlier_detect.fit(train_df)
 outliers_predicted = outlier_detect.predict(train_df)
 df['outlier'] = outliers_predicted
df = df[df['outlier'] == 1]
return df
tabla_completa["Cabin"] = tabla_completa["Cabin"].fillna("N")
 letras_cabinas = tabla_completa["Cabin"].str[0].unique().tolist()<feature_engineering>
def delete_outlier(y_pred, y_true, remain=0.99):
mse_array = np.square(np.subtract(y_pred, y_true)).mean(axis=1)
 mse_series = pd.Series(mse_array)
 check_value = mse_series.quantile(remain)
 check_outlier = np.where(mse_array <= check_value, 1, -1)
 return check_outlier, mse_series
def sampling(args):

 z_mean, z_log_var = args
batch = tf.keras.backend.shape(z_mean)[0]
dim = tf.keras.backend.int_shape(z_mean)[1]
epsilon = tf.keras.backend.random_normal(shape=(batch, dim))
return z_mean + tf.keras.backend.exp(0.5 * z_log_var)* epsilon
def VAE_model(inputs, origin_dim):
latent_dim = 2
origin_half_dim = origin_dim//2
x = tf.keras.layers.Dense(origin_half_dim, activation='relu' )(inputs)
 z_mean = tf.keras.layers.Dense(latent_dim, name='z_mean' )(x)
 z_log_var = tf.keras.layers.Dense(latent_dim, name='z_log_var' )(x)
 z = tf.keras.layers.Lambda(sampling, output_shape=(latent_dim,), name='z' )([z_mean, z_log_var])
 encoder = tf.keras.Model(inputs, [z_mean, z_log_var, z], name='encoder')
 latent_inputs = tf.keras.Input(shape=(latent_dim,), name='z_sampling')
 x = tf.keras.layers.Dense(origin_half_dim, activation='relu' )(latent_inputs)
 outputs = tf.keras.layers.Dense(origin_dim, activation='sigmoid' )(x)
 decoder = tf.keras.Model(latent_inputs, outputs, name='decoder')
 outputs = decoder(encoder(inputs)[2])
 vae = tf.keras.Model(inputs, outputs, name='vae_mlp')
 reconstruction_loss = tf.keras.losses.MSE(inputs, outputs)
 reconstruction_loss *= origin_dim
kl_loss = 1 + z_log_var - tf.keras.backend.square(z_mean)- tf.keras.backend.exp(z_log_var)
 kl_loss = tf.keras.backend.sum(kl_loss, axis=-1)
 kl_loss *= -0.5
vae_loss = tf.keras.backend.mean(reconstruction_loss + kl_loss)
 vae.add_loss(vae_loss)
 encoder.summary()
decoder.summary()
return vae
tabla_completa["Letra_Cabina"] = tabla_completa["Cabin"].str[0]<feature_engineering>
def VAE(df, remain_ratio):
np.random.seed(0)
 tf.random.set_seed(0)
 lr_sched = step_decay_schedule(initial_lr=0.001, decay_factor=0.97, step_size=1, verbose=0)
 early_stopping = EarlyStopping(monitor='val_loss', patience=10, verbose=1)
 cols = list(df.columns[df.dtypes != 'object'])
 train_df = df[cols]
input_shape =(train_df.shape[1],)
 origin_dim = train_df.shape[1]
inputs = tf.keras.Input(shape = input_shape)
 epochs = 300
batch_size = 20480
validation_split = 0.2
steps = len(train_df)*(1-validation_split)// batch_size
optimizer = tf.keras.optimizers.Adam()
callbacks_list = [lr_sched, early_stopping]
scaler = StandardScaler()
x_train = scaler.fit_transform(train_df.astype(float))
with strategy.scope() :
model = VAE_model(inputs, origin_dim)
 model.compile(optimizer=optimizer)
 model.fit(
 x_train.astype(np.float32),
epochs=epochs,
batch_size=batch_size,
validation_split=validation_split,
steps_per_epoch=steps,
callbacks=callbacks_list,
verbose=0)
 print('
VAE: Predict cheaters')
 y_pred = model.predict(x_train.astype(np.float32), verbose=1, batch_size=batch_size,
workers=strategy.num_replicas_in_sync, use_multiprocessing=True)
 print('VAE: Check cheaters')
 df['outlier'], mse_serise = delete_outlier(y_pred, y_true=x_train, remain=remain_ratio)
 df = df[df['outlier'] == 1]
df.drop(columns='outlier', inplace=True)
 return df
dic_titulos_simples = {
"Mr" : "Mr",
"Mrs" : "Mrs",
"Miss" : "Miss",
"Master" : "Master",
"Don" : "Nobleza",
"Rev" : "Oficial",
"Dr" : "Oficial",
"Mme" : "Mrs",
"Ms" : "Mrs",
"Major" : "Oficial",
"Lady" : "Nobleza",
"Sir" : "Nobleza",
"Mlle" : "Miss",
"Col" : "Oficial",
"Capt" : "Oficial",
"Countess" : "Nobleza",
"Jonkheer" : "Nobleza",
"Dona" : "Nobleza"
}
tabla_completa["Titulo"] = tabla_completa["Name"].str.partition('.')[0].str.rpartition() [2].map(dic_titulos_simples )<feature_engineering>
def std_n_sigma(df, n, filter_):
cols = list(df.columns[df.dtypes != 'object'])
 filter_ = list(df[filter_])
 df = df[cols]
scaler = StandardScaler()
std_array = scaler.fit_transform(df.astype(float))
std_df = pd.DataFrame(std_array, columns=df.columns, index=filter_)
 remove_idx = set([])
 for col in std_df.columns:
remove_idx.update(std_df[(std_df[col] < -n)|(n < std_df[col])].index)
 print('PUBG cheaters: {}%'.format(round(len(remove_idx)/ len(df)* 100,2)))
 return remove_idx
grupo = tabla_completa.groupby(['Sex','Pclass','Titulo'])
 tabla_completa['Age'] = grupo['Age'].apply(lambda x: x.fillna(x.median()))<feature_engineering>
def data_preparation(df, train_flag=True):
origin_size = df.shape[0]
if train_flag:
print("Delete PUBG Cheaters
")
 df = VAE(df, remain_ratio=0.998)
 print('PUBG cheaters: {0}, {1}%
'.format(origin_size -len(df),
round(100 - 100 * len(df)/ origin_size,4)))
 else:
pass
print("Feature engineering")
 df = feature_engineering(df)
 return df
tabla_completa['Fare'] = tabla_completa['Fare'].fillna(tabla_completa['Fare'].median() )<feature_engineering>
train_ = data_preparation(train)
 train_.head()
gc.collect()
tabla_completa['Num_Familiares'] = tabla_completa['SibSp'] + tabla_completa['Parch']<categorify>
train_data = train_.drop(columns = ['winPlacePerc'])
 train_labels = train_['winPlacePerc']
train_x, val_x, train_y, val_y = train_test_split(train_data, train_labels, test_size=0.1, random_state=0)
 print(train_x.shape, train_y.shape, val_y.shape, val_x.shape)
 del train_, train_data, train_labels
gc.collect()
tabla_completa['Sex'] = tabla_completa['Sex'].map({"male": 0, "female":1})
 dummies_pclass = pd.get_dummies(tabla_completa['Pclass'], prefix="Pclass")
 dummies_titulo = pd.get_dummies(tabla_completa['Titulo'], prefix="Titulo")
 dummies_letra_cab = pd.get_dummies(tabla_completa['Letra_Cabina'], prefix="Letra_Cabina")
 dummies_grupo_edad = pd.get_dummies(tabla_completa['Grupo_Edad'], prefix="Grupo_Edad")
 dummies_tabla = pd.concat([tabla_completa, dummies_pclass, dummies_titulo, dummies_letra_cab,dummies_grupo_edad], axis=1)
 dummies_tabla.drop(['Pclass', 'Titulo', 'Cabin', 'Letra_Cabina', 'Grupo_Edad', 'Embarked', 'Name', 'Ticket'], axis=1, inplace=True )<data_type_conversions>
use_features = 80
im_features = feature_importance.sort_values(by='Value', ascending=False)[:use_features].Feature
scaler = StandardScaler()
X_train = scaler.fit_transform(train_x[im_features].astype(np.float32))
Y_train = train_y.values
X_val = scaler.fit_transform(val_x[im_features].astype(np.float32))
Y_val = val_y.values
gc.collect()
train_df = dummies_tabla[ :len(train_df)]
test_df = dummies_tabla[(len(dummies_tabla)- len(test_df)) : ]
train_df['Survived'] = train_df['Survived'].astype(int)
 print("Train:
",train_df)
 print("Test:
",test_df )<prepare_x_and_y>
from keras import optimizers, regularizers
from keras.models import Sequential, Model, load_model
from keras.layers import Dense, Dropout, BatchNormalization
from keras.callbacks import LearningRateScheduler, EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from keras.layers import Input, Dense, Lambda
from keras.losses import mse, binary_crossentropy
from keras.models import Model
from keras import backend as K
from keras.losses import mse, binary_crossentropy
from sklearn import preprocessing
test_df = test_df.reset_index(drop = True)
 train_df = train_df.reset_index(drop = True)
 train_dfX = train_df.drop(['PassengerId','Survived'], axis=1)
 train_dfY = train_df['Survived']
submission = pd.DataFrame(data=test_df['PassengerId'].copy())
 print(submission)
 test_df = test_df.drop(['PassengerId', 'Survived'], axis=1 )<normalization>
def model() :
hidden_layer = tf.keras.layers.Dense(2048, kernel_initializer='he_normal', activation='relu' )(inputs)
 hidden_layer = tf.keras.layers.BatchNormalization()(hidden_layer)
 hidden_layer = tf.keras.layers.Dropout(rate=0.1, seed=1234 )(hidden_layer)
 hidden_layer = tf.keras.layers.Dense(1024, kernel_initializer='he_normal', activation='relu' )(hidden_layer)
 hidden_layer = tf.keras.layers.BatchNormalization()(hidden_layer)
 hidden_layer = tf.keras.layers.Dropout(rate=0.1, seed=1234 )(hidden_layer)
 hidden_layer = tf.keras.layers.Dense(512, kernel_initializer='he_normal', activation='relu' )(hidden_layer)
 hidden_layer = tf.keras.layers.BatchNormalization()(hidden_layer)
 hidden_layer = tf.keras.layers.Dense(256, kernel_initializer='he_normal', activation='relu' )(hidden_layer)
 hidden_layer = tf.keras.layers.BatchNormalization()(hidden_layer)
 hidden_layer = tf.keras.layers.Dense(128, kernel_initializer='he_normal', activation='relu' )(hidden_layer)
 hidden_layer = tf.keras.layers.BatchNormalization()(hidden_layer)
 hidden_layer = tf.keras.layers.Dense(64, kernel_initializer='he_normal', activation='relu' )(hidden_layer)
 hidden_layer = tf.keras.layers.BatchNormalization()(hidden_layer)
 prediction = tf.keras.layers.Dense(1, kernel_initializer='normal', activation='sigmoid' )(hidden_layer)
 return prediction
sc = StandardScaler()
train_dfX = sc.fit_transform(train_dfX)
 test_df = sc.transform(test_df )<split>
np.random.seed(0)
 tf.random.set_seed(0)
 epochs = 500
batch_size = 20480
steps = len(X_train)// batch_size
optimizer = tf.keras.optimizers.Adam()
lr_sched = step_decay_schedule(initial_lr=0.001, decay_factor=0.97, step_size=1, verbose=0)
 early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_mae', mode='min', patience=10, verbose=1)
 callbacks_list = [lr_sched, early_stopping]
with strategy.scope() :
inputs = tf.keras.Input(shape=(X_train.shape[1],))
predictions = model()
model = tf.keras.Model(inputs=inputs, outputs=predictions)
 model.compile(optimizer=optimizer, loss='mae', metrics=['mae'])
 history = model.fit(
 X_train.astype(np.float32), Y_train.astype(np.float32),
shuffle=True,
epochs=epochs,
batch_size=batch_size,
validation_data =(X_val.astype(np.float32), Y_val.astype(np.float32)) ,
steps_per_epoch=steps,
callbacks=callbacks_list,
verbose=0 )
train_dfX,val_dfX,train_dfY, val_dfY = train_test_split(train_dfX,train_dfY , test_size=0.1, stratify=train_dfY)
 print("Entrnamiento: ",train_dfX.shape)
 print("Validacion : ",val_dfX.shape )<choose_model_class>
del X_train, Y_train, X_val, Y_val
gc.collect()
def func_model(arquitectura):
np.random.seed(42)
 random_seed = 42
first =True
inp = Input(shape=(train_dfX.shape[1],))
for capa in arquitectura:
if first:
x=Dense(capa, activation="relu", kernel_initializer=initializers.RandomNormal(seed=random_seed), bias_initializer='zeros' )(inp)
 first = False
else:
x=Dense(capa, activation="relu", kernel_initializer=initializers.RandomNormal(seed=random_seed), bias_initializer='zeros' )(x)
 x=Dense(1, activation="sigmoid", kernel_initializer=initializers.RandomNormal(seed=random_seed), bias_initializer='zeros' )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer=optimizers.Adam(lr=0.0002), metrics=['binary_accuracy'])
 return model<train_model>
%matplotlib inline
arq1 = [1024, 1024, 512]
model1 = None
model1 = func_model(arq1)
 train_history_tam1 = model1.fit(train_dfX, train_dfY, batch_size=32, epochs=epochs, validation_data=(val_dfX, val_dfY))
graf_model(train_history_tam1)
 precision(model1 )<train_model>
pd.set_option('display.float_format', '{:.2f}'.format)
 pd.set_option('display.max_columns', 200)
 pd.set_option('display.max_rows', 300)
arq2 = [1024, 512, 512]
model2 = None
model2 = func_model(arq2)
 train_history_tam2 = model2.fit(train_dfX, train_dfY, batch_size=32, epochs=epochs, validation_data=(val_dfX, val_dfY))
graf_model(train_history_tam2)
 precision(model2 )<train_model>
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )
arqFinal = [1024, 1024, 1024]
modelF = None
modelF = func_model(arqFinal)
 print(modelF.summary())
 train_history_tamF = modelF.fit(train_dfX, train_dfY, batch_size=32, epochs=epochs, validation_data=(val_dfX, val_dfY))
graf_model(train_history_tamF)
 precision(modelF, True )<choose_model_class>
def reload() :
gc.collect()
df = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/train_V2.csv')
 invalid_match_ids = df[df['winPlacePerc'].isna() ]['matchId'].values
df = df[-df['matchId'].isin(invalid_match_ids)]
return df
def func_model_reg() :
np.random.seed(42)
 random_seed = 42
inp = Input(shape=(train_dfX.shape[1],))
x=Dropout(0.1 )(inp)
 x=Dense(1024, activation="relu", kernel_initializer=initializers.RandomNormal(seed=random_seed), bias_initializer='zeros', kernel_regularizer=regularizers.l2(0.01))(x)
 x=Dropout(0.7 )(x)
 x=Dense(1024, activation="relu", kernel_initializer=initializers.RandomNormal(seed=random_seed), bias_initializer='zeros', kernel_regularizer=regularizers.l2(0.01))(x)
 x=Dropout(0.7 )(x)
 x=Dense(512, activation="relu", kernel_initializer=initializers.RandomNormal(seed=random_seed), bias_initializer='zeros', kernel_regularizer=regularizers.l2(0.01))(x)
 x=Dropout(0.9 )(x)
 x=Dense(1, activation="sigmoid", kernel_initializer=initializers.RandomNormal(seed=random_seed), bias_initializer='zeros' )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer=optimizers.Adam(lr=0.0002), metrics=['binary_accuracy'])
 return model<train_model>
train = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/train_V2.csv')
 train = reduce_mem_usage(train )
modelReg = None
modelReg = func_model_reg()
train_history_tamReg = modelReg.fit(train_dfX, train_dfY, batch_size=32, epochs=epochs, validation_data=(val_dfX, val_dfY))
graf_model(train_history_tamReg)
 precision(modelReg )<save_to_csv>
for c in ['Id','groupId','matchId']:
print(f'unique [{c}] count:', train[c].nunique() )
y_test = modelReg.predict(test_df)
 submission['Survived'] = y_test.round().astype(int)
 submission.to_csv('submission.csv', index=False )<import_modules>
display(list(train.columns[train.dtypes != 'object']))
display(list(train.columns[train.dtypes == 'object']))
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from keras.utils import to_categorical
import os
import time
from keras.models import Sequential
from keras.layers import Dense,Conv2D,Flatten,Dropout,MaxPooling2D,BatchNormalization
from keras.callbacks import EarlyStopping
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
import seaborn as sns
from keras import optimizers
from sklearn.metrics import classification_report<prepare_x_and_y>
train = train.dropna(axis='rows')
 display(train.isna().sum() )
path_train='.. /input/train.csv'
path_test=".. /input/test.csv"
train=pd.read_csv(path_train)
 test=pd.read_csv(path_test)
 X_train=train.drop("label",axis=1 ).values
Y_train=train["label"].values
X_test=test.values
X_train=X_train/X_train.max()
X_test=X_test/X_test.max()<categorify>
def delete_cheaters(df):
df.drop(df[df['roadKills'] >= 10].index, inplace=True)
 df.drop(df[df['kills'] >= 50].index, inplace=True)
 df.drop(df[df['longestKill'] >= 1000].index, inplace=True)
 df.drop(df[df['walkDistance'] >= 13000].index, inplace=True)
 df.drop(df[df['rideDistance'] >= 25000].index, inplace=True)
 df.drop(df[df['swimDistance'] >= 1500].index, inplace=True)
 df.drop(df[df['weaponsAcquired'] >= 50].index, inplace=True)
 df.drop(df[df['heals'] >= 40].index, inplace=True)
 df = df.drop(df[df['killStreaks'] >= 10].index)
 df.drop(df[(df['walkDistance']<=10.0)&(df['damageDealt'] >= 1000)].index, inplace=True)
 df.drop(df[(df['walkDistance']<=10.0)&(df['kills'] >= 5)].index, inplace=True)
 df.drop(df[(df['walkDistance']<=10.0)&(df['heals'] >= 5)].index, inplace=True)
 df.drop(df[(df['walkDistance']<=10.0)&(df['headshotKills'] >= 5)].index, inplace=True)
 df.drop(df[(df['walkDistance']<=10.0)&(df['headshotKills'] >= 5)].index, inplace=True)
 return df
label=[0,1,2,3,4,5,6,7,8,9]
nc=10
Y_train_d=to_categorical(Y_train,10)
 X_train_c=X_train.reshape(-1,28,28,1)
 X_test_c=X_test.reshape(-1,28,28,1 )<choose_model_class>
def feature_engineering(df):
df['heals_and_boosts'] = df['heals']+df['boosts']
df['total_distance'] = df['walkDistance']+df['rideDistance']+df['swimDistance']
df['kills_over_walkDistance'] = df['kills'] / df['walkDistance']
df['killPlace_over_maxPlace'] = df['killPlace'] / df['maxPlace']
df['players_joined'] = df.groupby('matchId')['matchId'].transform('count')
 df['players_in_team'] = df.groupby('groupId')['matchId'].transform('count')
 df.drop(['boosts','heals'], axis=1, inplace=True)
 df.drop(['rideDistance','swimDistance','matchDuration'], axis=1, inplace=True)
 df.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True)
 df.drop(['headshotKills','roadKills','vehicleDestroys','teamKills'], axis=1, inplace=True)
 match = df.groupby('matchId')
 df['killsPerc'] = match['kills'].rank(pct=True ).values
df['killPlacePerc'] = match['killPlace'].rank(pct=True ).values
df['walkDistancePerc'] = match['walkDistance'].rank(pct=True ).values
df['walkPerc_killsPerc'] = df['walkDistancePerc'] / df['killsPerc']
check_cols = ['kills_over_walkDistance', 'killPlace_over_maxPlace', 'walkPerc_killsPerc']
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
for c in check_cols: df[c].fillna(0, inplace=True)
 numcols = df.select_dtypes(include='number' ).columns
use_cols = numcols[numcols != 'winPlacePerc']
rank_cols = use_cols.drop(['numGroups', 'maxPlace', 'players_joined', 'players_in_team'])
 stat_cols = use_cols.drop(['numGroups', 'maxPlace', 'players_joined', 'players_in_team',
'assists', 'longestKill', 'weaponsAcquired','heals_and_boosts',
'total_distance', 'kills_over_walkDistance'])
 group = df.groupby(['matchId', 'groupId'])
 df = df.merge(group[stat_cols].median() , suffixes=['', '_team_median'], how='left',
on=['matchId', 'groupId'])
 df = reduce_mem_usage(df)
 df = df.merge(group[stat_cols].max() , suffixes=['', '_team_max'], how='left',
on=['matchId', 'groupId'])
 df = reduce_mem_usage(df)
 df = df.merge(group[stat_cols].min() , suffixes=['', '_team_min'], how='left',
on=['matchId', 'groupId'])
 df = reduce_mem_usage(df)
 df = df.merge(group[stat_cols].mean() , suffixes=['', '_team_mean'], how='left',
on=['matchId', 'groupId'])
 df = reduce_mem_usage(df)
 df = df.merge(group[stat_cols].sum() , suffixes=['', '_team_sum'], how='left',
on=['matchId', 'groupId'])
 df = reduce_mem_usage(df)
 df = df.merge(group[rank_cols].mean().groupby('matchId')[rank_cols].rank(pct=True),
suffixes=['', '_team_mean_rank'], how='left', on=['matchId', 'groupId'])
 df = reduce_mem_usage(df)
 mapper = lambda x: 'solo' if 'solo' in x else 'duo' if('duo' in x)or('crash' in x)else 'squad'
df['matchType'] = df['matchType'].apply(mapper)
 df = pd.get_dummies(df, columns=['matchType'])
 df.drop(['Id', 'matchId','groupId'], axis=1, inplace=True)
 constant_column = [col for col in df.columns if df[col].nunique() == 1]
print('drop constant columns:', constant_column)
 df.drop(constant_column, axis=1, inplace=True)
 assert df.isna().sum().sum() == 0
return df
np.random.seed(2)
 m=Sequential()
m.add(Conv2D(filters=128,kernel_size=4,padding="same",activation="relu",input_shape=(28,28,1)))
 m.add(Conv2D(filters=128,kernel_size=4,padding="same",activation="relu"))
m.add(MaxPooling2D(pool_size=2,strides=2))
m.add(Dropout(0.2))
m.add(Conv2D(filters=64,kernel_size=4,padding="same",activation="relu",))
m.add(Conv2D(filters=64,kernel_size=4,padding="same",activation="relu"))
m.add(MaxPooling2D(pool_size=2,strides=2))
m.add(Dropout(0.2))
m.add(Flatten())
 m.add(Dense(1024,activation="relu"))
m.add(Dropout(0.2))
m.add(Dense(512,activation="relu"))
m.add(Dropout(0.4))
m.add(Dense(256,activation="relu"))
m.add(Dropout(0.6))
m.add(Dense(128,activation="relu"))
m.add(Dense(nc,activation='softmax'))
m.summary()<train_model>
def isolation_forest(df, contamination):
cols = list(df.columns[df.dtypes != 'object'])
 train_df = df[cols]
outlier_detect = IsolationForest(n_estimators=500,
contamination=contamination,
max_features=train_df.shape[1])
 outlier_detect.fit(train_df)
 outliers_predicted = outlier_detect.predict(train_df)
 df['outlier'] = outliers_predicted
df = df[df['outlier'] == 1]
return df
el=EarlyStopping(monitor='val_loss',min_delta=0.001,patience=5,restore_best_weights=True)
 ad=optimizers.Adam(lr=0.002,beta_1=0.9,beta_2=0.999,decay=0.004)
 m.compile(loss="categorical_crossentropy",optimizer=ad,metrics=["accuracy"])
 s=time.time()
h=m.fit(X_train_c,Y_train_d,batch_size=32,validation_split=0.4,epochs=50,callbacks=[el])
 e=time.time()
t=e-s
print("Addestramento completato in %d minuti e %d secondi" %(t/60,t*60)) 
<find_best_params>
def delete_outlier(y_pred, y_true, remain=0.99):
mse_array = np.square(np.subtract(y_pred, y_true)).mean(axis=1)
 mse_series = pd.Series(mse_array)
 check_value = mse_series.quantile(remain)
 check_outlier = np.where(mse_array <= check_value, 1, -1)
 return check_outlier, mse_series
def sampling(args):

 z_mean, z_log_var = args
batch = tf.keras.backend.shape(z_mean)[0]
dim = tf.keras.backend.int_shape(z_mean)[1]
epsilon = tf.keras.backend.random_normal(shape=(batch, dim))
return z_mean + tf.keras.backend.exp(0.5 * z_log_var)* epsilon
def VAE_model(inputs, origin_dim):
latent_dim = 2
origin_half_dim = origin_dim//2
x = tf.keras.layers.Dense(origin_half_dim, activation='relu' )(inputs)
 z_mean = tf.keras.layers.Dense(latent_dim, name='z_mean' )(x)
 z_log_var = tf.keras.layers.Dense(latent_dim, name='z_log_var' )(x)
 z = tf.keras.layers.Lambda(sampling, output_shape=(latent_dim,), name='z' )([z_mean, z_log_var])
 encoder = tf.keras.Model(inputs, [z_mean, z_log_var, z], name='encoder')
 latent_inputs = tf.keras.Input(shape=(latent_dim,), name='z_sampling')
 x = tf.keras.layers.Dense(origin_half_dim, activation='relu' )(latent_inputs)
 outputs = tf.keras.layers.Dense(origin_dim, activation='sigmoid' )(x)
 decoder = tf.keras.Model(latent_inputs, outputs, name='decoder')
 outputs = decoder(encoder(inputs)[2])
 vae = tf.keras.Model(inputs, outputs, name='vae_mlp')
 reconstruction_loss = tf.keras.losses.MSE(inputs, outputs)
 reconstruction_loss *= origin_dim
kl_loss = 1 + z_log_var - tf.keras.backend.square(z_mean)- tf.keras.backend.exp(z_log_var)
 kl_loss = tf.keras.backend.sum(kl_loss, axis=-1)
 kl_loss *= -0.5
vae_loss = tf.keras.backend.mean(reconstruction_loss + kl_loss)
 vae.add_loss(vae_loss)
 encoder.summary()
decoder.summary()
return vae
acc=h.history['acc']
val_acc=h.history['val_acc']
loss=h.history['loss']
val_loss=h.history['val_loss']<save_to_csv>
def VAE(df, remain_ratio):
np.random.seed(0)
 tf.random.set_seed(0)
 lr_sched = step_decay_schedule(initial_lr=0.001, decay_factor=0.97, step_size=1, verbose=0)
 early_stopping = EarlyStopping(monitor='val_loss', patience=10, verbose=1)
 cols = list(df.columns[df.dtypes != 'object'])
 train_df = df[cols]
input_shape =(train_df.shape[1],)
 origin_dim = train_df.shape[1]
inputs = tf.keras.Input(shape = input_shape)
 epochs = 300
batch_size = 20480
validation_split = 0.2
steps = len(train_df)*(1-validation_split)// batch_size
optimizer = tf.keras.optimizers.Adam()
callbacks_list = [lr_sched, early_stopping]
scaler = StandardScaler()
x_train = scaler.fit_transform(train_df.astype(float))
with strategy.scope() :
model = VAE_model(inputs, origin_dim)
 model.compile(optimizer=optimizer)
 model.fit(
 x_train.astype(np.float32),
epochs=epochs,
batch_size=batch_size,
validation_split=validation_split,
steps_per_epoch=steps,
callbacks=callbacks_list,
verbose=0)
 print('
VAE: Predict cheaters')
 y_pred = model.predict(x_train.astype(np.float32), verbose=1, batch_size=batch_size,
workers=strategy.num_replicas_in_sync, use_multiprocessing=True)
 print('VAE: Check cheaters')
 df['outlier'], mse_serise = delete_outlier(y_pred, y_true=x_train, remain=remain_ratio)
 df = df[df['outlier'] == 1]
df.drop(columns='outlier', inplace=True)
 return df
y_test=m.predict(X_test_c)
 y_test = np.argmax(y_test,axis = 1)
 out=pd.DataFrame({"ImageId": list(range(1,len(y_test)+1)) ,"Label": y_test})
 out.to_csv("Submission_cnn.csv", index=False, header=True )<import_modules>
def std_n_sigma(df, n, filter_):
cols = list(df.columns[df.dtypes != 'object'])
 filter_ = list(df[filter_])
 df = df[cols]
scaler = StandardScaler()
std_array = scaler.fit_transform(df.astype(float))
std_df = pd.DataFrame(std_array, columns=df.columns, index=filter_)
 remove_idx = set([])
 for col in std_df.columns:
remove_idx.update(std_df[(std_df[col] < -n)|(n < std_df[col])].index)
 print('PUBG cheaters: {}%'.format(round(len(remove_idx)/ len(df)* 100,2)))
 return remove_idx
os.listdir('.. /input/digit-recognizer')
<load_from_csv>
def data_preparation(df, train_flag=True):
origin_size = df.shape[0]
if train_flag:
print("Delete PUBG Cheaters
")
 df = VAE(df, remain_ratio=0.998)
 print('PUBG cheaters: {0}, {1}%
'.format(origin_size -len(df),
round(100 - 100 * len(df)/ origin_size,4)))
 else:
pass
print("Feature engineering")
 df = feature_engineering(df)
 return df
PATH = '.. /input/digit-recognizer'
df_train = pd.read_csv(os.path.join(PATH, 'train.csv'))
train_y = df_train['label'].values
train_x = df_train.drop(['label'], axis=1 ).values
df_test = pd.read_csv(os.path.join(PATH, 'test.csv'))
test_x = df_test.values
print(train_x.shape)
 print(train_y.shape)
 print(test_x.shape )<define_variables>
train_ = data_preparation(train)
 train_.head()
gc.collect()
IMG_SIZE = 32<data_type_conversions>
train_data = train_.drop(columns = ['winPlacePerc'])
 train_labels = train_['winPlacePerc']
train_x, val_x, train_y, val_y = train_test_split(train_data, train_labels, test_size=0.1, random_state=0)
 print(train_x.shape, train_y.shape, val_y.shape, val_x.shape)
 del train_, train_data, train_labels
gc.collect()
def resize(img_array):
tmp = np.empty(( img_array.shape[0], IMG_SIZE, IMG_SIZE))
for i in range(len(img_array)) :
img = img_array[i].reshape(28, 28 ).astype('uint8')
 img = cv2.resize(img,(IMG_SIZE, IMG_SIZE))
img = img.astype('float32')/255
tmp[i] = img
return tmp
train_x_resize = resize(train_x)
 test_x_resize = resize(test_x )<categorify>
use_features = 80
im_features = feature_importance.sort_values(by='Value', ascending=False)[:use_features].Feature
scaler = StandardScaler()
X_train = scaler.fit_transform(train_x[im_features].astype(np.float32))
Y_train = train_y.values
X_val = scaler.fit_transform(val_x[im_features].astype(np.float32))
Y_val = val_y.values
gc.collect()
train_y_final = to_categorical(train_y, num_classes=10)
 print(train_y_final.shape )<choose_model_class>
from keras import optimizers, regularizers
from keras.models import Sequential, Model, load_model
from keras.layers import Dense, Dropout, BatchNormalization
from keras.callbacks import LearningRateScheduler, EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from keras.layers import Input, Dense, Lambda
from keras.losses import mse, binary_crossentropy
from keras.models import Model
from keras import backend as K
from keras.losses import mse, binary_crossentropy
from sklearn import preprocessing
vgg16 = VGG16(weights = 'imagenet',
include_top = False,
input_shape=(IMG_SIZE, IMG_SIZE, 3)
)
 model = Sequential()
model.add(vgg16)
 model.add(Flatten())
 model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
 model.summary()<split>
def model() :
hidden_layer = tf.keras.layers.Dense(2048, kernel_initializer='he_normal', activation='relu' )(inputs)
 hidden_layer = tf.keras.layers.BatchNormalization()(hidden_layer)
 hidden_layer = tf.keras.layers.Dropout(rate=0.1, seed=1234 )(hidden_layer)
 hidden_layer = tf.keras.layers.Dense(1024, kernel_initializer='he_normal', activation='relu' )(hidden_layer)
 hidden_layer = tf.keras.layers.BatchNormalization()(hidden_layer)
 hidden_layer = tf.keras.layers.Dropout(rate=0.1, seed=1234 )(hidden_layer)
 hidden_layer = tf.keras.layers.Dense(512, kernel_initializer='he_normal', activation='relu' )(hidden_layer)
 hidden_layer = tf.keras.layers.BatchNormalization()(hidden_layer)
 hidden_layer = tf.keras.layers.Dense(256, kernel_initializer='he_normal', activation='relu' )(hidden_layer)
 hidden_layer = tf.keras.layers.BatchNormalization()(hidden_layer)
 hidden_layer = tf.keras.layers.Dense(128, kernel_initializer='he_normal', activation='relu' )(hidden_layer)
 hidden_layer = tf.keras.layers.BatchNormalization()(hidden_layer)
 hidden_layer = tf.keras.layers.Dense(64, kernel_initializer='he_normal', activation='relu' )(hidden_layer)
 hidden_layer = tf.keras.layers.BatchNormalization()(hidden_layer)
 prediction = tf.keras.layers.Dense(1, kernel_initializer='normal', activation='sigmoid' )(hidden_layer)
 return prediction
x_train, x_test, y_train, y_test = train_test_split(train_x_final, train_y_final, test_size=0.2, random_state=2019)
 print(x_train.shape)
 print(x_test.shape)
 print(y_train.shape)
 print(y_test.shape)
<choose_model_class>
np.random.seed(0)
 tf.random.set_seed(0)
 epochs = 500
batch_size = 20480
steps = len(X_train)// batch_size
optimizer = tf.keras.optimizers.Adam()
lr_sched = step_decay_schedule(initial_lr=0.001, decay_factor=0.97, step_size=1, verbose=0)
 early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_mae', mode='min', patience=10, verbose=1)
 callbacks_list = [lr_sched, early_stopping]
with strategy.scope() :
inputs = tf.keras.Input(shape=(X_train.shape[1],))
predictions = model()
model = tf.keras.Model(inputs=inputs, outputs=predictions)
 model.compile(optimizer=optimizer, loss='mae', metrics=['mae'])
 history = model.fit(
 X_train.astype(np.float32), Y_train.astype(np.float32),
shuffle=True,
epochs=epochs,
batch_size=batch_size,
validation_data =(X_val.astype(np.float32), Y_val.astype(np.float32)) ,
steps_per_epoch=steps,
callbacks=callbacks_list,
verbose=0 )
<train_model><EOS> 
del X_train, Y_train, X_val, Y_val
gc.collect()