Kaggle competition: Titanic - Machine Learning from Disaster
train=pd.read_csv('.. /input/train_V2.csv')
 test=pd.read_csv('.. /input/test_V2.csv')
 ID=test['Id']<correct_missing_values>
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import *
from sklearn.model_selection import *
from sklearn.svm import *
from sklearn.linear_model import *
from sklearn.ensemble import *
from sklearn.neighbors import KNeighborsClassifier
train=train.dropna(axis=0 )<prepare_x_and_y>
csv_names = ['PassengerId',	'Survived',	'Pclass',	'Name',	'Sex',	'Age',	'SibSp',	'Parch',	'Ticket',	'Fare',	'Cabin', 'Embarked']
y_train=train['winPlacePerc']
train=train.drop(['winPlacePerc'],axis=1 )<categorify>
dataset = pd.read_csv('/kaggle/input/titanic/train.csv', names= csv_names , header = 0 ,skipinitialspace=True )
train["players_Match"] = train.groupby("matchId")["Id"].transform("count")
 train["players_Group"] = train.groupby("groupId")["Id"].transform("count")
 test["players_Match"] = test.groupby("matchId")["Id"].transform("count")
 test["players_Group"] = test.groupby("groupId")["Id"].transform("count" )<categorify>
dataset.isnull().sum()
train['Total_Kills'] = train.groupby('groupId')['kills'].transform('sum')
 test['Total_Kills'] = test.groupby('groupId')['kills'].transform('sum' )<categorify>
dataset1 = dataset.dropna(axis= 1 )
train['First_Man'] = train.groupby('groupId')['matchDuration'].transform('min')
 test['First_Man'] = test.groupby('groupId')['matchDuration'].transform('min' )<categorify>
dataset1.isnull().sum()
train['Last_Man'] = train.groupby('groupId')['matchDuration'].transform('max')
 test['Last_Man'] = test.groupby('groupId')['matchDuration'].transform('max' )<feature_engineering>
dataset1['Age'] = dataset['Age'].fillna(round(np.mean(dataset['Age'])) )
train['Time_Survival'] = train['Last_Man'] - train['First_Man']
test['Time_Survival'] = test['Last_Man'] - test['First_Man']<feature_engineering>
dataset1.isnull().sum()
train['Kill_Percentile'] = train['killPlace'] /(train['maxPlace'] + 1e-9)
 test['Kill_Percentile'] = test['killPlace'] /(test['maxPlace'] + 1e-9 )<drop_column>
for i in dataset1.keys() :
print('the unique values for {} is {}'.format(i , len(dataset1[i].unique())) )
train.drop(["matchId","groupId",'Id','killPoints', 'maxPlace', 'winPoints','vehicleDestroys'],axis=1,inplace=True)
 test.drop(["matchId","groupId",'Id','killPoints', 'maxPlace', 'winPoints','vehicleDestroys'],axis=1,inplace=True )<feature_engineering>
dataset1.groupby('Survived')['Survived'].agg('count' )
train['Headshot_rate'] = train['kills'] /(train['headshotKills'] + 1e-9)
 test['Headshot_rate'] = test['kills'] /(test['headshotKills'] + 1e-9)
 train['KillStreak_rate'] = train['killStreaks'] /(train['kills'] + 1e-9)
 test['KillStreak_rate'] = test['killStreaks'] /(test['kills'] + 1e-9 )<feature_engineering>
dataset1[dataset1['Sex'] == 'male'].groupby('Survived')['Survived'].agg('count' )
train['Total_Damage'] = train['damageDealt'] + train['teamKills']*100
test['Total_Damage'] = test['damageDealt'] + test['teamKills']*100<feature_engineering>
print('Total female survivors: {}'.format(dataset1[dataset1['Sex'] == 'female'].groupby('Survived')['Survived'].agg('count')[1]))
print('Total male survivors: {}'.format(dataset1[dataset1['Sex'] == 'male'].groupby('Survived')['Survived'].agg('count')[1]))
print('Total survivors: {}'.format(dataset1.groupby('Survived')['Survived'].agg('count')[1]))
train['New']=(train['matchDuration'] < train['matchDuration'].mean())
 test['New']=(test['matchDuration'] < test['matchDuration'].mean() )<feature_engineering>
dataset2 = dataset1.copy()
data_pop = dataset2.pop('Ticket' )
train['ProKiller']=(train['headshotKills']/train['kills']+1e-9)
 test['ProKiller']=(test['headshotKills']/test['kills']+1e-9 )<feature_engineering>
dataset2['Name'] = dataset2['Name'].apply(lambda x: x.split(',')[0] )
train['Is_Sniper']=(train['longestKill']>=250)
 test['Is_Sniper']=(test['longestKill']>=250 )<feature_engineering>
dataset2['Embarked'] = dataset['Embarked']
train['killsOverWalkDistance'] = train['kills'] /(train['walkDistance'] + 1e-9)
 test['killsOverWalkDistance'] = test['kills'] /(test['walkDistance'] + 1e-9 )<feature_engineering>
dataset2['Embarked'] = dataset2['Embarked'].fillna('S' )
train['Total_Distance'] =(train['rideDistance']+train['swimDistance']+train['walkDistance'])
 test['Total_Distance'] =(test['rideDistance']+test['swimDistance']+test['walkDistance'] )<feature_engineering>
dataset2.isnull().sum()
train['killsOverDistance'] = train['kills'] /(train['distance'] + 1e-9)
 test['killsOverDistance'] = test['kills'] /(test['distance'] + 1e-9 )<feature_engineering>
dataset2.groupby('Embarked')['Embarked'].agg('count' )
train['Total_enemies'] = train['playersInMatch'] - train['playersInGroup']
test['Total_enemies'] = test['playersInMatch'] - test['playersInGroup']<feature_engineering>
female_survival_ratio = dataset2[dataset2['Sex'] == 'female'].groupby('Survived')['Survived'].agg('count')[1]/dataset2.groupby('Sex')['Sex'].agg('count')['female']
train['Team_Spirit'] = train['heals'] + train['revives'] + train['boosts']
test['Team_Spirit'] = test['heals'] + test['revives'] + test['boosts']<define_variables>
male_survival_ratio = dataset2[dataset2['Sex'] == 'male'].groupby('Survived')['Survived'].agg('count')[1]/dataset2.groupby('Sex')['Sex'].agg('count')['male']
set1=set(i for i in train[(train['kills']>40)&(train['heals']==0)].index.tolist())
 set2=set(i for i in train[(train['distance']==0)&(train['kills']>20)].index.tolist())
 set3=set(i for i in train[(train['damageDealt']>4000)&(train['heals']<2)].index.tolist())
 set4=set(i for i in train[(train['rideDistance']>25000)].index.tolist())
 set5=set(i for i in train[(train['killStreaks']>3)&(train['weaponsAcquired']> 30)].index.tolist())
 sets=set1 | set2 | set3 | set4 | set5<prepare_x_and_y>
print('the male survival ratio is :{}'.format(male_survival_ratio))
print('the female survival ratio is :{}'.format(female_survival_ratio))
train=train.drop(list(sets))
y_train=y_train.drop(list(sets))<feature_engineering>
passenger_survived =(dataset2['PassengerId'][dataset2['Survived'] == 1])-1
passenger_not_survived =(dataset2['PassengerId'][dataset2['Survived'] == 0])-1
fpp=['crashfpp','duo-fpp','flare-fpp','normal-duo-fpp','normal-solo-fpp','normal-squad-fpp','solo-fpp','squad-fpp']
train["fpp"] = np.where(train["matchType"].isin(fpp),1,0)
 test["fpp"] = np.where(test["matchType"].isin(fpp),1,0 )<define_variables>
dataset3 = dataset2.copy()
change={'crashfpp':'crash',
'crashtpp':'crash',
'duo':'duo',
'duo-fpp':'duo',
'flarefpp':'flare',
'flaretpp':'flare',
'normal-duo':'duo',
'normal-duo-fpp':'duo',
'normal-solo':'solo',
'normal-solo-fpp':'solo',
'normal-squad':'squad',
'normal-squad-fpp':'squad',
'solo-fpp':'solo',
'squad-fpp':'squad',
'solo':'solo',
'squad':'squad'
}
train['matchType']=train['matchType'].map(change)
 test['matchType']=test['matchType'].map(change )<categorify>
dataset3 = dataset3.drop(['Name' , 'Sex' , 'Embarked'] , axis=1 )
modes={'crash':1,
'duo':2,
'flare':3,
'solo':4,
'squad':5
}
train['matchType']=train['matchType'].map(modes)
 test['matchType']=test['matchType'].map(modes )<categorify>
dataset4 = dataset2.copy()
d1=pd.get_dummies(train['matchType'])
 train=train.drop(['matchType'],axis=1)
 train=train.join(d1)
 d2=pd.get_dummies(test['matchType'])
 test=test.drop(['matchType'],axis=1)
 test=test.join(d2 )<normalization>
features = ["Sex"]
scaler = MinMaxScaler()
scaler.fit(train)
 train=scaler.transform(train)
 test=scaler.transform(test )<split>
dataset5 = pd.get_dummies(dataset[features] )
X_train,X_test,y_train,y_test= train_test_split(train,y_train,test_size=0.3 )<define_variables>
dataset4['Sex_female'] = dataset5['Sex_female']
dataset4['Sex_male'] = dataset5['Sex_male']
train_pool = Pool(X_train, y_train)
 test_pool = Pool(X_test, y_test )<choose_model_class>
dataset6 = dataset4.copy()
model = CatBoostRegressor(
 iterations=5000,
depth=10,
learning_rate=0.1,
l2_leaf_reg= 2,
loss_function='RMSE',
eval_metric='MAE',
random_strength=0.1,
bootstrap_type='Bernoulli',
leaf_estimation_method='Gradient',
leaf_estimation_iterations=1,
boosting_type='Plain'
,task_type = "GPU"
,feature_border_type='GreedyLogSum'
,random_seed=1234
)<train_model>
dataset6.pop('Sex')
 dataset6.pop('Name' )
model.fit(train_pool, eval_set=test_pool, plot=True )<compute_test_metric>
dataset7 = pd.get_dummies(dataset2['Embarked'] )
train_mse =(mean_absolute_error(y_train, model.predict(X_train)))
 test_mse =(mean_absolute_error(y_test, model.predict(X_test)))
 print('Train error= ',train_mse)
 print('Test error= ',test_mse)
<save_to_csv>
dataset6['C'] = dataset7['C']
dataset6['Q'] = dataset7['Q']
dataset6['S'] = dataset7['S']
subm = pd.read_csv('.. /input/sample_submission_V2.csv')
 predictions = model.predict(test)
 test = pd.read_csv('.. /input/test_V2.csv')
 test['winPlacePerc'] = predictions
test['winPlacePerc'] = test.groupby('groupId')['winPlacePerc'].transform('median')
 subm['winPlacePerc'] = test['winPlacePerc']
subm['Id']=ID
subm.to_csv('submission.csv', index = False )<import_modules>
dataset6.pop('Embarked' )
%matplotlib inline<set_options>
dataset6.pop('Fare')
 dataset6.pop('Age' )
pd.set_option('display.float_format', '{:.2f}'.format)
 pd.set_option('display.max_columns', 200)
 pd.set_option('display.max_rows', 300)
<set_options>
dataset_test = pd.read_csv("/kaggle/input/titanic/test.csv")
 dataset_test.head()
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 )<load_from_csv>
dataset_test.isnull().sum()
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<load_from_csv>
dataset_test1 = dataset_test.dropna(axis = 1 )
train = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/train_V2.csv')
 train = reduce_mem_usage(train )<count_unique_values>
dataset_test2 = dataset_test1.copy()
for c in ['Id','groupId','matchId']:
print(f'unique [{c}] count:', train[c].nunique() )<filter>
features_test = ['Sex' , 'Embarked']
display(list(train.columns[train.dtypes != 'object']))
display(list(train.columns[train.dtypes == 'object']))<count_missing_values>
dataset_test3 = pd.get_dummies(dataset_test2[features_test] )
train = train.dropna(axis='rows')
 display(train.isna().sum() )<drop_column>
dataset_test2['Sex_female'] = dataset_test3['Sex_female']
dataset_test2['Sex_male'] = dataset_test3['Sex_male']
dataset_test2['C'] = dataset_test3['Embarked_C']
dataset_test2['Q'] = dataset_test3['Embarked_Q']
dataset_test2['S'] = dataset_test3['Embarked_S']
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<feature_engineering>
dataset_test2.pop('Name')
 dataset_test2.pop('Sex')
 dataset_test2.pop('Ticket')
 dataset_test2.pop('Embarked' )
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<choose_model_class>
dataset8 = dataset6.copy()
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<compute_test_metric>
dataset8.pop('PassengerId' )
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<choose_model_class>
dataset9 = dataset8.copy()
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<normalization>
dataset9.pop('C')
 dataset9.pop('Q')
 dataset9.pop('S' )
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<drop_column>
y = dataset8.pop('Survived' )
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<normalization>
dataset_test4 = dataset_test2.copy()
train_ = data_preparation(train)
 train_.head()
gc.collect()<split>
dataset_test4.pop('PassengerId' )
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()<prepare_x_and_y>
dataset_test5 = dataset_test4.copy()
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()<import_modules>
dataset_test5.pop('C')
 dataset_test5.pop('Q')
 dataset_test5.pop('S' )
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<choose_model_class>
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import *
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<choose_model_class>
data = dataset.copy()
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 )<drop_column>
data['Name'] = data['Name'].apply(lambda x: x.split(',')[1] )
del X_train, Y_train, X_val, Y_val
gc.collect()<import_modules>
data['Name'] = data['Name'].apply(lambda x: x.split('.')[0] )
%matplotlib inline<set_options>
data1 = pd.get_dummies(data['Name'] )
pd.set_option('display.float_format', '{:.2f}'.format)
 pd.set_option('display.max_columns', 200)
 pd.set_option('display.max_rows', 300)
<set_options>
dataset11 = dataset8.copy()
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 )<load_from_csv>
t = [' Col' , ' Dr' , ' Master' , ' Miss' ,' Mr', ' Mrs' , ' Ms' , ' Rev']
for i in t:
dataset11[i] = data1[i]
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<load_from_csv>
data1.isnull().sum()
train = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/train_V2.csv')
 train = reduce_mem_usage(train )<count_unique_values>
dataset_test6 = dataset_test.copy()
for c in ['Id','groupId','matchId']:
print(f'unique [{c}] count:', train[c].nunique() )<filter>
dataset_test6['Name'] = dataset_test6['Name'].apply(lambda x: x.split(',')[1] )
display(list(train.columns[train.dtypes != 'object']))
display(list(train.columns[train.dtypes == 'object']))<count_missing_values>
dataset_test6['Name'] = dataset_test6['Name'].apply(lambda x: x.split('.')[0] )
train = train.dropna(axis='rows')
 display(train.isna().sum() )<drop_column>
dataset_test7 = pd.get_dummies(dataset_test6['Name'] )
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<feature_engineering>
for i in dataset_test7.keys() :
dataset_test4[i] = dataset_test7[i]
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<choose_model_class>
dataset_test4.pop(' Dona' )
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<compute_test_metric>
dataset_test4.isnull().sum()
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<choose_model_class>
dataset11.isnull().sum()
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<normalization>
dataset_final = dataset_test4.copy()
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<drop_column>
dataset_final['Total_SibSp'] = dataset_test4['SibSp']+dataset_test4['Parch']
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<normalization>
dataset_final.pop('SibSp')
 dataset_final.pop('Parch' )
train_ = data_preparation(train)
 train_.head()
gc.collect()<split>
dataset12 = dataset11.copy()
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()<prepare_x_and_y>
datasettrain_final = dataset11.copy()
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()<import_modules>
datasettrain_final['Total_SibSp'] = dataset11['SibSp']+dataset11['Parch']
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<choose_model_class>
datasettrain_final.pop('SibSp')
 datasettrain_final.pop('Parch' )
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<choose_model_class>
x_train , x_test , y_train , y_test = train_test_split(datasettrain_final , y , test_size = 0.2 , random_state = 10 )
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 )<drop_column>
from sklearn.ensemble import *
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from xgboost import XGBClassifier
del X_train, Y_train, X_val, Y_val
gc.collect()<data_type_conversions>
obj = XGBClassifier(learning_rate=0.02, n_estimators=750,
max_depth= 3, min_child_weight= 1,
colsample_bytree= 0.6, gamma= 0.0,
reg_alpha= 0.001, subsample= 0.8)
 model = obj.fit(x_train, y_train)
 predicted_val = obj.predict(x_test)
 predicted_test = obj.predict(dataset_final)
 predicted_train = obj.predict(x_train )
def cleanPeople(people):
people = people.drop(['date'],axis=1)
 people['people_id'] = people['people_id'].apply(lambda x : x.split('_')[1])
 people['people_id'] = pd.to_numeric(people['people_id'] ).astype(int)
 fields = list(people.columns)
 cat_data = fields[1:11]
bool_data = fields[11:]
for data in cat_data:
people[data] = people[data].fillna('type 0')
 people[data] = people[data].apply(lambda x: x.split(' ')[1])
 people[data] = pd.to_numeric(people[data] ).astype(int)
 for data in bool_data:
people[data] = pd.to_numeric(people[data] ).astype(int)
 return people
def cleanAct(data, train=False):
data = data.drop(['date'],axis = 1)
 if train:
data = data.drop(['outcome'],axis=1)
 data['people_id'] = data['people_id'].apply(lambda x : x.split('_')[1])
 data['people_id'] = pd.to_numeric(data['people_id'] ).astype(int)
 data['activity_id'] = data['activity_id'].apply(lambda x: x.split('_')[1])
 data['activity_id'] = pd.to_numeric(data['activity_id'] ).astype(int)
 fields = list(data.columns)
 cat_data = fields[2:13]
for column in cat_data:
data[column] = data[column].fillna('type 0')
 data[column] = data[column].apply(lambda x : x.split(' ')[1])
 data[column] = pd.to_numeric(data[column] ).astype(int)
 return data<load_from_csv>
accuracy_train = accuracy_score(y_true=y_train , y_pred= predicted_train)
 accuracy_val = accuracy_score(y_true=y_test, y_pred= predicted_val )
people = pd.read_csv(".. /input/people.csv")
 people = cleanPeople(people)
 act_train = pd.read_csv(".. /input/act_train.csv",parse_dates=['date'])
 act_train_cleaned = cleanAct(act_train,train=True)
 act_test = pd.read_csv(".. /input/act_test.csv",parse_dates=['date'])
 act_test_cleaned = cleanAct(act_test)
 train = act_train_cleaned.merge(people,on='people_id', how='left')
 test = act_test_cleaned.merge(people, on='people_id', how='left' )<categorify>
output = pd.DataFrame({'PassengerId': dataset_test.PassengerId, 'Survived': predicted_test} )
train = pd.get_dummies(train,columns=['activity_category'],sparse=True,drop_first=True)
 test = pd.get_dummies(test,columns=['activity_category'],sparse=True,drop_first=True )<prepare_x_and_y>
output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )