Kaggle competition: Titanic - Machine Learning from Disaster
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>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head(5 )
train['headshotrate'] = train['kills'] /(train['headshotKills'] + 1e-9)
 test['headshotrate'] = test['kills'] /(test['headshotKills'] + 1e-9)
 train['killStreakrate'] = train['killStreaks'] /(train['kills'] + 1e-9)
 test['killStreakrate'] = test['killStreaks'] /(test['kills'] + 1e-9 )<feature_engineering>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head(5 )
train['TotalDamage'] = train['damageDealt'] + train['teamKills']*100
test['TotalDamage'] = test['damageDealt'] + test['teamKills']*100<feature_engineering>
data_dict = {}
data_dict['train data'] = train_data
data_dict['test data'] = test_data;
train['Noob']=(train['matchDuration'] < train['matchDuration'].mean())
 test['Noob']=(test['matchDuration'] < train['matchDuration'].mean() )<feature_engineering>
compare_features(train_data, 'Pclass','Age' )
train['Sniper']=(train['longestKill']>=250)
 test['Sniper']=(test['longestKill']>=250 )<feature_engineering>
compare_features(train_data, 'SibSp', 'Survived' )
train['ProAim']=(train['headshotKills']/(train['kills']+1e-9))
test['ProAim']=(test['headshotKills']/(test['kills']+1e-9))<feature_engineering>
compare_features(train_data, 'Parch', 'Survived' )
train['distance'] =(train['rideDistance']+train['swimDistance']+train['walkDistance'])
 test['distance'] =(test['rideDistance']+test['swimDistance']+test['walkDistance'])
 train['distance'] = np.log1p(train['distance'])
 test['distance'] = np.log1p(test['distance'] )<define_variables>
compare_features(train_data, 'Embarked', 'Survived' )
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>
train_data.groupby(['Embarked'] ).mean().drop(['PassengerId', 'SibSp', 'Parch'], axis=1 )
train=train.drop(list(sets))
y_train=y_train.drop(list(sets)) 
<feature_engineering>
def concat_data(data_1, data_2):
return pd.concat([data_1, data_2], sort=False ).reset_index(drop=True)
 def divide_data(all_data):
return all_data.loc[:890], all_data.loc[891:].drop(['Survived'], axis=1)
 data_all = concat_data(train_data, test_data)
 data_fs = [train_data, test_data]
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>
miss_data_dict = {}
for key, dataset in data_dict.items() :
miss_abs = dataset.isnull().sum()
miss_rel = miss_abs / dataset.isnull().count()
col_abs = '{}: missing values(absolut)'.format(key)
 col_rel = '{}: missing values(relative in %)'.format(key)
 if key == 'test data':
miss_data_dict[key] = pd.concat([miss_abs.sort_values(ascending=False),
miss_rel.sort_values(ascending=False)*100],
axis=1, keys=[col_abs, col_rel])
 elif key == 'train data':
miss_data_dict[key] = pd.concat([miss_abs.sort_values(ascending=False),
miss_rel.sort_values(ascending=False)*100],
axis=1, keys=[col_abs, col_rel])
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>
msno.matrix(test_data,figsize=(9,2),width_ratios=(10,1))
miss_data_dict['test data'].head()
modes={'crash':1,
'duo':2,
'flare':3,
'solo':4,
'squad':5
}
train['matchType']=train['matchType'].map(modes)
 test['matchType']=test['matchType'].map(modes )<categorify>
data_all.groupby(['Pclass', 'Sex'])['Age'].mean()
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>
index = data_all['Age'].index[data_all['Age'].apply(np.isnan)]
data_all.loc[index, ['Pclass', 'Sex', 'Age']]
scaler = MinMaxScaler()
scaler.fit(train)
 train=scaler.transform(train)
 test=scaler.transform(test )<create_dataframe>
data_all['Age'] = data_all.groupby(['Pclass', 'Sex'])['Age'].apply(lambda x: x.fillna(x.mean()))
train_data, test_data = divide_data(data_all)
 data_dict['train data'] = train_data
data_dict['test data'] = test_data
data_all.loc[index, ['Pclass', 'Sex', 'Age']]
df = pd.DataFrame(train)
 df.isnull().sum()<split>
data_all.Cabin = data_all.Cabin.fillna('Unknown')
 data_all['Deck'] = data_all['Cabin'].str[0]
X_train,X_test,y_train,y_test= train_test_split(train,y_train,test_size=0.3 )<train_model>
data_all.groupby(['Pclass'] ).Deck.value_counts()
lm = Lasso(alpha=1e-5)
 lm.fit(X_train,y_train )<compute_test_metric>
data_all.groupby(['Deck'] ).mean().drop(['PassengerId', 'SibSp', 'Parch'], axis=1 )
train_mse =(mean_absolute_error(y_train,lm.predict(X_train)))
 test_mse =(mean_absolute_error(y_test, lm.predict(X_test)))
 train_mse,test_mse<predict_on_test>
deck_avg_fare = data_all.groupby(['Deck'] ).Fare.mean().drop('U')
 deck_avg_fare
y_train = y_train - lm.predict(X_train)
 y_test = y_test - lm.predict(X_test )<define_variables>
indices_U = data_all[data_all['Deck'] == 'U'].index
for i in indices_U:
fare = data_all.iloc[i].Fare
nearest_avg_fare = min(deck_avg_fare, key=lambda x:abs(x-fare))
deck = deck_avg_fare[deck_avg_fare == nearest_avg_fare].index[0]
data_all['Deck'].iloc[i] = deck
train_data, test_data = divide_data(data_all)
 data_dict['train data'] = train_data
data_dict['test data'] = test_data
train_pool = Pool(X_train, y_train)
 test_pool = Pool(X_test, y_test )<choose_model_class>
data_all.loc[indices_U, ['Fare', 'Deck']]
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>
def handle_missing_data(data, miss_abs):
for col in data:
miss_prop = data[col].isna().sum() /len(data)
 if miss_prop < 0.5:
if data[col].dtype == "float64" and data[col].isnull().sum() > 0:
mean = data[col].mean()
data[col] = data[col].fillna(mean)
 print("Filling {} missing values in {} with mean value {:.0f}.".format(miss_abs[col], col, mean))
elif data[col].dtype == "object" and data[col].isnull().sum() > 0:
top = data[col].describe().top
data[col] = data[col].fillna(top)
 print("Filling {} missing values in {} with top value {}.".format(miss_abs[col], col, top))
return data
model.fit(train_pool, eval_set=test_pool )<compute_test_metric>
for key, dataset in data_dict.items() :
print('{} operations:'.format(key))
miss_abs = miss_data_dict[key]['{}: missing values(absolut)'.format(key)]
miss_rel = miss_data_dict[key]['{}: missing values(relative in %)'.format(key)]
data_dict[key] = handle_missing_data(dataset, miss_abs )
train_mse =(mean_absolute_error(y_train,lm.predict(X_train)+ model.predict(X_train)))
 test_mse =(mean_absolute_error(y_test, lm.predict(X_test)+ model.predict(X_test)))
 print('Train error= ',train_mse)
 print('Test error= ',test_mse)
<save_to_csv>
def title_extract(data):
data['Title'] = data['Name'].str.split(',', expand=True)[1].str.split('.', expand=True)[0]
subm = pd.read_csv('.. /input/sample_submission_V2.csv')
 predictions = model.predict(test)+ lm.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)
<define_variables>
for key, dataset in data_dict.items() :
title = title_extract(dataset)
 data_all = concat_data(train_data, test_data)
 data_all['Title'].value_counts()
kernel_start_time = datetime.datetime.now()
train_path = '.. /input/train_V2.csv'
test_path = '.. /input/test_V2.csv'
print(os.listdir(".. /input")) 
<data_type_conversions>
title_names =(data_all['Title'].value_counts() < 10)
 for key, dataset in data_dict.items() :
dataset['Title'] = dataset['Title'].apply(lambda x: 'Misc' if title_names.loc[x] == True else x)
 data_all = concat_data(train_data, test_data)
 print(data_all['Title'].value_counts() )
data_types = {'Id':str,'groupId':str,'matchId':str,'assists':np.int8,'boosts':np.int8,'damageDealt':np.float16,'DBNOs':np.int8,
'headshotKills':np.int8,'heals':np.int8,'killPlace':np.int8,'killPoints':np.int16,'kills':np.int8,'killStreaks':np.int8,'longestKill':np.float16,
'matchDuration':np.int16,'matchType':str,'maxPlace':np.int8,'numGroups':np.int8,'rankPoints':np.int16,'revives':np.int8,'rideDistance':np.float16,
'roadKills':np.int8,'swimDistance':np.float16,'teamKills':np.int8,'vehicleDestroys':np.int8,'walkDistance':np.float16,'weaponsAcquired':np.int16,
'winPoints':np.int16,'winPlacePerc':np.float16}
data_types['walkDistance'] = np.float32
data_types['rideDistance'] = np.float32
data_types['damageDealt'] = np.float32<define_variables>
for key, dataset in data_dict.items() :
dataset['Age'] = dataset['Age'].astype('int', copy=True)
 age_labels = [1,2,3,4,5]
dataset['Age interval'] = pd.cut(data_all['Age'].astype('int'),5)
 dataset['Age Code'] = pd.cut(data_all['Age'],5, labels=age_labels ).astype('int64')
 data_all = concat_data(train_data, test_data)
 print('Number of cases within age intervals in datast:')
 data_all['Age interval'].value_counts()
zero_stdev_cols = ['median_match_roadKills', 'median_match_vehicleDestroys', 'median_match_road_kills_per_rideDistance', 'std_match_longestKill']
ulesess_stat_cols = ['median_match_revives','median_match_teamKills','median_match_swimDistance_norm',
'max_match_kill_streak_rate','max_group_roadKills','min_group_roadKills','median_match_assists',
'sum_group_roadKills','sum_group_vehicleDestroys','min_group_vehicleDestroys']
useless_match_type_names = ['normal-duo','flarefpp','normal-solo','crashtpp','normal-squad','flaretpp','normal-duo-fpp',
'normal-squadfpp', 'normal-squad-fpp', 'crashfpp','duo','squad-fpp','normal-solo-fpp']
all_useless_cols = zero_stdev_cols + ulesess_stat_cols + useless_match_type_names
all_useless_cols = set(all_useless_cols)
 min_match_useful_cols = ['min_match_rankPoints', 'min_match_winPoints', 'min_match_killPoints']<feature_engineering>
for key, dataset in data_dict.items() :
fare_labels = [1,2,3,4,5]
dataset['Fare interval'] = pd.qcut(data_all['Fare'], 5)
 dataset['Fare Code'] = pd.qcut(data_all['Fare'], 5, labels=fare_labels ).astype('int64')
 data_all = concat_data(train_data, test_data)
 print('Number of cases per fare category in dataset(approx.equal):')
 data_all['Fare interval'].value_counts()
def fix_missing_ranks(X, mean_ranks=None, rank_stds=None, rank_cols=['rankPoints', 'winPoints']):
if(mean_ranks is None)or(rank_stds is None):
mean_ranks = {}
rank_stds = {}
for rank_col in rank_cols:
mean_ranks[rank_col] = X.loc[X[rank_col] > 1, rank_col].mean()
rank_stds[rank_col] = X.loc[X[rank_col] > 1, rank_col].std()
rank_deltas =(X.loc[(X['rankPoints'] > 1)&(X['winPoints'] <= 1), 'rankPoints'] - mean_ranks['rankPoints'])/ rank_stds['rankPoints']
X.loc[(X['rankPoints'] > 1)&(X['winPoints'] <= 1), 'winPoints'] = mean_ranks['winPoints'] + rank_stds['winPoints']*rank_deltas
win_deltas =(X.loc[(X['rankPoints'] <= 1)&(X['winPoints'] > 1), 'winPoints'] - mean_ranks['winPoints'])/ rank_stds['winPoints']
X.loc[(X['rankPoints'] <= 1)&(X['winPoints'] > 1), 'rankPoints'] = mean_ranks['rankPoints'] + rank_stds['rankPoints']*win_deltas
X.loc[(X['rankPoints'] <= 1)&(X['winPoints'] <= 1), 'rankPoints'] = mean_ranks['rankPoints']
X.loc[(X['rankPoints'] <= 1)&(X['winPoints'] <= 1), 'winPoints'] = mean_ranks['winPoints']
return X, mean_ranks, rank_stds<feature_engineering>
for key, dataset in data_dict.items() :
dataset['Family Size'] = dataset ['SibSp'] + dataset['Parch'] + 1
data_all = concat_data(train_data, test_data )
def add_player_features(X):
X['headshot_rate'] = X['headshotKills'] /(X['kills'] + 0.00001)
 X['kill_streak_rate'] = X['killStreaks'] /(X['kills'] + 0.00001)
 X['kills_assists'] = X['kills'] + X['assists']
X['heals_boosts'] = X['heals'] + X['boosts']
X['total_distance'] = X['walkDistance'] + X['rideDistance'] + X['swimDistance']
X['kills_assists_per_heal_boost'] = X['kills_assists'] /(X['heals_boosts'] + 1)
 X['damageDealt_per_heal_boost'] = X['damageDealt'] /(X['heals_boosts'] + 1)
 X['road_kills_per_rideDistance'] = X['roadKills'] /(X['rideDistance'] + 0.01)
 X['maxPlace_per_numGroups'] = X['maxPlace'] / X['numGroups']
X['assists_per_kill'] = X['assists'] /(X['kills'] + X['assists'] + 0.0001)
 X['killPlace'] = X['killPlace'] - 1
return X<groupby>
family_mapping =(lambda s: 1 if s == 1 else(2 if s == 2 else(3 if 3 <= s <= 4 else(4 if s >= 5 else 0))))
for key, dataset in data_dict.items() :
dataset['Family Size Code'] = dataset['Family Size'].map(family_mapping)
 data_all = concat_data(train_data, test_data)
 data_all['Family Size Code'].value_counts()
def create_basic_group_info(X):
group_cols = ['matchId', 'groupId', 'matchDuration', 'matchType', 'maxPlace', 'numGroups', 'maxPlace_per_numGroups', 'winPlacePerc', 'killPlace']
if 'winPlacePerc' not in X.columns:
group_cols.remove('winPlacePerc')
 pl_data_grouped = X[group_cols].groupby(['matchId', 'groupId'])
 gr_data = pl_data_grouped.first()
gr_data.drop(columns='killPlace', inplace=True)
 gr_data['raw_groupSize'] = pl_data_grouped['numGroups'].count()
gr_data['groupSize'] = gr_data['raw_groupSize']
gr_data['group_size_overflow'] =(gr_data['groupSize'] > 4 ).astype(np.int8)
 gr_data.loc[gr_data['groupSize'] > 4, ['groupSize']] = 2
gr_data['meanGroupSize'] = gr_data.groupby('matchId')['groupSize'].transform(np.mean)
 gr_data['medianGroupSize'] = gr_data.groupby('matchId')['groupSize'].transform(np.median)
 gr_data['maxKillPlace'] = pl_data_grouped['killPlace'].max().groupby('matchId' ).transform(np.max)
 gr_data['totalPlayers'] = gr_data.groupby('matchId')['groupSize'].transform(sum)
 gr_data['totalPlayersAdjusted'] = gr_data['maxPlace'].astype(float)* gr_data['totalPlayers'] /(gr_data['numGroups'] + 0.01)
 gr_data['totalPlayersAdjusted'] = gr_data['totalPlayersAdjusted'].apply(lambda x: np.minimum(100.0, x))
gr_data['num_opponents'] = gr_data['totalPlayersAdjusted'] - gr_data['groupSize']
X = X.merge(gr_data[['num_opponents', 'totalPlayersAdjusted', 'groupSize', 'raw_groupSize', 'maxKillPlace']], on=['matchId', 'groupId'])
 print('group size counts:')
 print(X['raw_groupSize'].value_counts())
 X['revives_per_groupSize'] = X['revives'] /(X['groupSize'] - 1 + 0.001)
 X['kills_assists_norm_both'] = X['kills_assists'].astype(np.float32)/ X['num_opponents'] / X['matchDuration']
X['killPlace_norm'] = X['killPlace']/(X['maxKillPlace'] + 0.000001)
 X['damageDealt_norm_both'] = X['damageDealt'].astype(np.float32)/ X['num_opponents'] / X['matchDuration']
X['DBNOs_norm'] = X['DBNOs'].astype(np.float32)/ X['num_opponents'] / X['matchDuration']
X['heals_norm'] = X['heals'].astype(np.float32)/ X['matchDuration']
X['boosts_norm'] = X['boosts'].astype(np.float32)/ X['matchDuration']
X['walkDistance_norm'] = X['walkDistance'].astype(np.float32)/ X['matchDuration']
X['rideDistance_norm'] = X['rideDistance'].astype(np.float32)/ X['matchDuration']
X['swimDistance_norm'] = X['swimDistance'].astype(np.float32)/ X['matchDuration']
gr_data = reduce_mem_usage(gr_data)
 gr_data.drop(columns=list(set(gr_data.columns)&all_useless_cols))
return gr_data, X<groupby>
for key, dataset in data_dict.items() :
dataset['Mother'] = np.where(( dataset.Title == 'Mrs')&(dataset.Parch >0),1,0)
 data_all = concat_data(train_data, test_data )
def create_group_and_match_stats(data, gr_data):
group_stats_cols = ['assists','boosts','DBNOs','killPoints','longestKill','rankPoints', 'road_kills_per_rideDistance',
'kills_assists_norm_both', 'damageDealt_norm_both', 'DBNOs_norm', 'heals_boosts', 'assists_per_kill', 'killPlace_norm',
'revives','roadKills','teamKills','vehicleDestroys','weaponsAcquired','winPoints','headshot_rate','kill_streak_rate',
'kills_assists', 'heals_norm','walkDistance_norm','rideDistance_norm','swimDistance_norm',
'damageDealt_per_heal_boost','kills_assists_per_heal_boost']
match_stats_cols = ['assists','boosts','DBNOs','killPoints','longestKill','rankPoints', 'road_kills_per_rideDistance',
'kills_assists_norm_both', 'damageDealt_norm_both', 'DBNOs_norm', 'heals_boosts', 'assists_per_kill',
'revives','roadKills','teamKills','vehicleDestroys','weaponsAcquired','winPoints','headshot_rate','kill_streak_rate',
'kills_assists', 'heals_norm','walkDistance_norm','rideDistance_norm','swimDistance_norm',
'damageDealt_per_heal_boost','kills_assists_per_heal_boost']
pl_data_grouped_by_group = data.groupby(['matchId', 'groupId'])
 pl_data_grouped_by_match = data.groupby(['matchId'])
 group_funcs = {'min':np.min, 'max':np.max, 'sum':np.sum, 'median': np.mean}
match_funcs = {'min':np.min,'max':np.max, 'sum':np.sum, 'median': np.median, 'std':np.std}
extra_group_stats = pl_data_grouped_by_group[['matchId', 'groupId']].first().reset_index(drop=True)
 extra_match_stats = pl_data_grouped_by_match[['matchId']].first().reset_index(drop=True)
 for colname in group_stats_cols:
for f_name, func in group_funcs.items() :
gr_col_name = f_name + '_group_' + colname
if(gr_col_name not in all_useless_cols)or(( gr_col_name + '_rank')not in all_useless_cols):
if func is not sum:
extra_group_stats[gr_col_name] = pl_data_grouped_by_group[colname].agg(func ).values
else:
extra_group_stats[gr_col_name] = pl_data_grouped_by_group[colname].agg(func ).values * sum_multipliers
for colname in match_stats_cols:
for f_name, func in match_funcs.items() :
m_col_name = f_name + '_match_' + colname
if m_col_name not in all_useless_cols:
if func is np.std:
extra_match_stats[m_col_name] = pl_data_grouped_by_match[colname].agg(func ).fillna(0 ).values
elif func is np.min:
if m_col_name in min_match_useful_cols:
extra_match_stats[m_col_name] = pl_data_grouped_by_match[colname].agg(func ).values
else:
extra_match_stats[m_col_name] = pl_data_grouped_by_match[colname].agg(func ).values
extra_group_stats.set_index(['matchId', 'groupId'], inplace=True)
 extra_match_stats.set_index(['matchId'], inplace=True)
 pl_data_grouped_by_group = None
pl_data_grouped_by_match = None
select_cols = []
for col in extra_group_stats.columns:
if(( col + '_rank')not in all_useless_cols)and(col not in ['matchId', 'groupId']):
select_cols.append(col)
 rank_data = extra_group_stats.groupby(['matchId'])
 rank_data = rank_data[select_cols].rank() - 1
gc.collect()
max_rank_data = rank_data.groupby(['matchId'] ).transform(np.max)
 rank_data = rank_data /(max_rank_data + 0.0001)
 max_rank_data = None
gc.collect()
print('rank data created')
 gr_col_to_drop = list(set(extra_group_stats.columns)& all_useless_cols)
 extra_group_stats.drop(columns=gr_col_to_drop, inplace=True)
 gc.collect()
extra_group_stats = extra_group_stats.join(rank_data, on=['matchId', 'groupId'], rsuffix='_rank')
 extra_group_stats.reset_index(level=1, inplace=True)
 rank_data = None
gc.collect()
print('rank data merged')
 extra_group_stats = reduce_mem_usage(extra_group_stats)
 extra_match_stats = reduce_mem_usage(extra_match_stats)
 merged_features = extra_group_stats.merge(extra_match_stats, on=['matchId'])
 extra_group_stats = None
extra_match_stats = None
gc.collect()
print('extra match and group stats merged')
 merged_features = merged_features.merge(gr_data, on=['matchId', 'groupId'])
 gr_data = None
gc.collect()
print('group data and stats merged')
 cats = merged_features['matchType'].unique()
cats = set(cats)- set(all_useless_cols)
 encoded_data = np.empty(shape=(merged_features.shape[0], 0), dtype=np.int8)
 for category in cats:
encoded_data = np.c_[encoded_data,(merged_features[['matchType']] == category ).values.reshape(-1,1 ).astype(np.int8)]
encoded_data = pd.DataFrame(encoded_data, columns=cats, index=merged_features.index, dtype=np.int8)
 print('matchType data created')
 for col in encoded_data.columns:
merged_features[col] = encoded_data[col]
encoded_data = None
gc.collect()
print('match type data merged')
 cols_to_drop = ['matchType']
merged_features = merged_features.drop(columns=cols_to_drop)
 return merged_features<filter>
for key, dataset in data_dict.items() :
dataset['Ticket Frequency'] = data_all.groupby('Ticket')['Ticket'].transform('count')
 data_all = concat_data(train_data, test_data )
def remove_outliers(X):
outliers =(X['walkDistance'] > 10000)|(X['rideDistance'] > 15000)|(X['swimDistance'] > 1000)|(( X['kills'] > 0)&(X['total_distance'] == 0))
outliers = outliers |(X['kills'] > 30)|(X['longestKill'] > 800)|(X['weaponsAcquired'] > 40)
 X = X.loc[~outliers]
outlier_data = X.loc[outliers]
return X, outlier_data<load_from_csv>
X_train = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[ features])
 X_test.head()
class DataPipeline() :
def __init__(self, pipeline=None):
if pipeline is not None:
self.rank_means = pipeline.rank_means
self.rank_stds = pipeline.rank_stds
return
def fit_transform(self, data_path):
data = pd.read_csv(data_path)
 for col_name, col_type in data_types.items() :
data[col_name] = data[col_name].astype(col_type)
 data = data.loc[~data['winPlacePerc'].isna() ]
data = data.loc[data['maxPlace'] > 1]
data, self.rank_means, self.rank_stds = fix_missing_ranks(data)
 data = add_player_features(data)
 gr_data, data = create_basic_group_info(data)
 cols_to_drop = ['kills', 'headshotKills', 'killStreaks', 'walkDistance', 'rideDistance', 'swimDistance', 'heals']
data.drop(columns=cols_to_drop, inplace=True)
 data_ids = data[['Id', 'matchId', 'groupId']]
merged_features = create_group_and_match_stats(data, gr_data)
 gc.collect()
print('final feature dataframe shape:', merged_features.shape)
 return merged_features.reset_index() , data_ids
def transform(self, data_path):
data = pd.read_csv(data_path)
 for col_name, col_type in data_types.items() :
if col_name != 'winPlacePerc':
data[col_name] = data[col_name].astype(col_type)
 data = fix_missing_ranks(data, self.rank_means, self.rank_stds)[0]
data = add_player_features(data)
 gr_data, data = create_basic_group_info(data)
 cols_to_drop = ['kills', 'headshotKills', 'killStreaks', 'walkDistance', 'rideDistance', 'swimDistance', 'heals']
data.drop(columns=cols_to_drop, inplace=True)
 data_ids = data[['Id', 'matchId', 'groupId']]
merged_features = create_group_and_match_stats(data, gr_data)
 print('final feature dataframe shape:', merged_features.shape)
 return merged_features.reset_index() , data_ids<create_dataframe>
y = train_data['Survived']
<data_type_conversions><EOS> 
model = RandomForestClassifier(n_estimators=1800,
max_depth=8,
min_samples_split=6,
min_samples_leaf=6,
max_features='auto',
oob_score=True,
random_state=42,
n_jobs=-1,
verbose=1)
 model.fit(X_train, y)
 predictions = model.predict(X_test)
 acc_random_forest = round(model.score(X_train, y)* 100, 2)
 print(acc_random_forest)
 output = pd.DataFrame({'PassengerID': test_data.PassengerId, 'Survived': predictions})
 output = output.convert_dtypes()
output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!")
 output.head()