Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
warnings.filterwarnings('ignore')
 pd.options.display.max_columns = 50
plt.style.use('ggplot')
<load_from_csv>
import os
import warnings
import gc
import time
from tqdm import tqdm
import pandas as pd
import numpy as np
from sklearn.metrics import log_loss
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import LabelEncoder
from itertools import product
from sklearn.model_selection import train_test_split
import lightgbm as lgb
v_train = pd.read_csv('/kaggle/input/titanic/train.csv')
 v_test = pd.read_csv('/kaggle/input/titanic/test.csv')
 idx = len(v_train )<concatenate>
train = pd.get_dummies(train,columns=['matchType'] )
v_train.drop('PassengerId', axis=1, inplace=True)
 v_test.drop('PassengerId', axis=1, inplace=True)
 v_merged = pd.concat([v_train, v_test], sort=False ).reset_index(drop=True )<load_from_csv>
train =train.dropna()
def get_combined_data() :
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 targets = train.Survived
combined = train.append(test)
 combined.reset_index(inplace=True, drop=True)
 return combined<feature_engineering>
test = pd.get_dummies(test,columns=['matchType'] )
def process_family() :
global combined
combined['FamilySize'] = combined['Parch'] + combined['SibSp'] + 1
combined['Alone'] = combined['FamilySize'].map(lambda s: 1
if s == 1 else 0)
 status('Family')
 return combined<feature_engineering>
y_train =train['winPlacePerc']
x_train =train.drop(['Id','groupId','matchId','winPlacePerc'],axis=1)
def family_survival() :
global combined

 combined['Last_Name'] = combined['Name'].apply(
 lambda x: str.split(x, ",")[0])
 default_survival_rate = 0.5
combined['Family_Survival'] = default_survival_rate
for grp, grp_df in combined[[
'Survived', 'Name', 'Last_Name', 'Fare', 'Ticket', 'PassengerId',
'SibSp', 'Parch', 'Age', 'Cabin'
]].groupby(['Last_Name', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
combined.loc[combined['PassengerId'] ==
passID, 'Family_Survival'] = 1
elif(smin == 0.0):
combined.loc[combined['PassengerId'] ==
passID, 'Family_Survival'] = 0
for _, grp_df in combined.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['Family_Survival'] == 0)|(
 row['Family_Survival'] == 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
combined.loc[combined['PassengerId'] ==
passID, 'Family_Survival'] = 1
elif(smin == 0.0):
combined.loc[combined['PassengerId'] ==
passID, 'Family_Survival'] = 0
status('FamilySurvival')
 return combined<define_variables>
X_train = x_train.values
Y_train = y_train.values
def get_titles() :
title_dictionary = {
'Capt': 'Dr/Clergy/Mil',
'Col': 'Dr/Clergy/Mil',
'Major': 'Dr/Clergy/Mil',
'Jonkheer': 'Honorific',
'Don': 'Honorific',
'Dona': 'Honorific',
'Sir': 'Honorific',
'Dr': 'Dr/Clergy/Mil',
'Rev': 'Dr/Clergy/Mil',
'the Countess': 'Honorific',
'Mme': 'Mrs',
'Mlle': 'Miss',
'Ms': 'Mrs',
'Mr': 'Mr',
'Mrs': 'Mrs',
'Miss': 'Miss',
'Master': 'Master',
'Lady': 'Honorific'
}
combined['Title'] = combined['Name'].map(
 lambda name: name.split(',')[1].split('.')[0].strip())
 combined['Title'] = combined.Title.map(title_dictionary)
 status('Title')
 return combined<categorify>
validation_size = 0.20
seed = 1000
X_train, X_valid, Y_train, Y_valid = train_test_split(X_train, Y_train, test_size=validation_size, random_state=seed )
def process_names() :
global combined
combined.drop('Name', axis=1, inplace=True)
 titles_dummies = pd.get_dummies(combined['Title'], prefix='Title')
 combined = pd.concat([combined, titles_dummies], axis=1)
 combined.drop('Title', axis=1, inplace=True)
 status('names')
 return combined<feature_engineering>
train_data = lgb.Dataset(data=X_train, label=Y_train)
 valid_data = lgb.Dataset(data=X_valid, label=Y_valid)
 params = {
'num_leaves': 144,
"metric" : "mae",
'learning_rate': 0.1,
'n_estimators': 800,
'max_depth':13,
'max_bin':55,
'bagging_fraction':0.8,
'bagging_freq':5,
'feature_fraction':0.9
}
lgb_model = lgb.train(params, train_data, valid_sets=[train_data, valid_data], verbose_eval=1 )
def process_age() :
global combined
combined['Age'] = combined.groupby(
 ['Pclass', 'Sex'])['Age'].apply(lambda x: x.fillna(x.median()))
status('Age')
 return combined<categorify>
X_test = test.drop(["Id", "groupId", "matchId"], axis = 1 )
def age_binner() :
global combined
names = ['less2', '2-18', '18-35', '35-65', '65plus']
combined['AgeBin'] = pd.qcut(combined['Age'],q = 5, labels = names)
 age_dummies = pd.get_dummies(combined['AgeBin'], prefix='AgeBin')
 combined = pd.concat([combined, age_dummies], axis=1)
 combined.drop('AgeBin', inplace=True, axis=1)
 combined.drop('Age', inplace=True, axis=1)
 status('Age Bins')
 return combined<feature_engineering>
Y_test = lgb_model.predict(X_test)
 submission = pd.DataFrame({
"Id": test['Id'],
"winPlacePerc": Y_test
})
 submission.to_csv('submission.csv', index=False )