Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
temp["Embarked"].replace(['C','Q', 'S'], [1,2,3], inplace =True )<categorify>
inittime = dt.datetime.now()
print("Capturing train database...")
 df_train = pd.read_csv(".. /input/train_V2.csv")
 print("Train Database captured in ", dt.datetime.now() - inittime, " secs")
 df_train= reduce_mem_usage(df_train)
 inittime = dt.datetime.now()
print("Capturing Test database...")
 df_test = pd.read_csv(".. /input/test_V2.csv")
 print("Test Database captured in ", dt.datetime.now() - inittime, " secs")
 df_test= reduce_mem_usage(df_test)
 print("Train data set :
",df_train.head())
 print("Test data set :
", df_test.head() )
temp["Gender"].replace(['baby','m1', 'm2', 'old', 'w1', 'w2'], [1,2,3,4,5,6], inplace =True )<create_dataframe>
df_train.loc[df_train['winPlacePerc'].isna() ==True, 'winPlacePerc']= 0.5
df = pd.DataFrame()<count_missing_values>
df_train['matchType'].value_counts()
df.isnull().sum()<create_dataframe>
df_test['winPlacePerc']= 0.0
df_train['Type']= 'Train'
df_test['Type']= 'Test'
print(df_train.shape, df_test.shape )
score = df.copy()<feature_engineering>
df= pd.concat([df_train, df_test], ignore_index=True)
 del df_train
print(df.shape )
score['Survived'] = temp['Survived']<feature_engineering>
df= reduce_mem_usage(df )
score['Score'] = 0<groupby>
def min_by_team(df,df_Group, features):
print("Working on Min by Team features...")
 inittime = dt.datetime.now()
agg = df_Group[features].min()
print("Features : Min Created in ", dt.datetime.now() - inittime)
 return df.merge(agg, suffixes=['', '_min'], how='left', on=['matchId', 'groupId'])
 def max_by_team(df,df_Group, features):
print("Working on Max by Team features...")
 inittime = dt.datetime.now()
agg = df_Group[features].max()
print("Features : MAX Created in ", dt.datetime.now() - inittime)
 return df.merge(agg, suffixes=['', '_max'], how='left', on=['matchId', 'groupId'])
 def sum_by_team(df,df_Group, features):
print("Working on Sum by Team features...")
 inittime = dt.datetime.now()
agg = df_Group[features].sum()
print("Features : SUM Created in ", dt.datetime.now() - inittime)
 return df.merge(agg, suffixes=['', '_sum'], how='left', on=['matchId', 'groupId'])
 def median_by_team(df,df_Group, features):
print("Working on Median by Team features...")
 inittime = dt.datetime.now()
agg = df_Group[features].median()
print("Features : MEDIAN Created in ", dt.datetime.now() - inittime)
 return df.merge(agg, suffixes=['', '_median'], how='left', on=['matchId', 'groupId'])
 def mean_by_team(df,df_Group, features):
print("Working on Mean by Team features...")
 inittime = dt.datetime.now()
agg = df_Group[features].mean()
print("Features : MEAN Created in ", dt.datetime.now() - inittime)
 return df.merge(agg, suffixes=['', '_mean'], how='left', on=['matchId', 'groupId'])
 def rank_by_team(df,df_Group, features):
print("Working on Rank by Team features...")
 inittime = dt.datetime.now()
agg = df_Group[features].mean()
print("Means Created")
 agg1 = agg.groupby('matchId')[features].rank(pct=True)
 print("Features : RANK Created in ", dt.datetime.now() - inittime)
 df= df.merge(agg, suffixes=['', '_mean'], how='left', on=['matchId', 'groupId'])
 return df.merge(agg1, suffixes=['', '_mean_rank'], how='left', on=['matchId', 'groupId'])
 print("Functions created" )
def see(a):
return score.groupby(a ).Survived.mean()
see('Pclass' )<feature_engineering>
Y_Column = 'winPlacePerc'
ColumnList = ['assists', 'boosts', 'damageDealt', 'DBNOs',
'headshotKills', 'heals', 'killPlace', 'killPoints', 'kills',
'killStreaks', 'longestKill', 'maxPlace', 'numGroups', 'revives',
'rideDistance', 'roadKills', 'swimDistance', 'teamKills',
'vehicleDestroys', 'walkDistance', 'weaponsAcquired', 'winPoints']
df['headshotKills_over_kills'] = df['headshotKills'] / df['kills']
df['headshotKills_over_kills'].fillna(0, inplace=True)
 df['headshotKills_over_kills'].replace(np.inf, 0, inplace=True)
 df['killPlace_over_maxPlace'] = df['killPlace'] / df['maxPlace']
df['killPlace_over_maxPlace'].fillna(0, inplace=True)
 df['killPlace_over_maxPlace'].replace(np.inf, 0, inplace=True)
 df['walkDistance_over_heals'] = df['walkDistance'] / df['heals']
df['walkDistance_over_heals'].fillna(0, inplace=True)
 df['walkDistance_over_heals'].replace(np.inf, 0, inplace=True)
 df['walkDistance_over_kills'] = df['walkDistance'] / df['kills']
df['walkDistance_over_kills'].fillna(0, inplace=True)
 df['walkDistance_over_kills'].replace(np.inf, 0, inplace=True)
 df= reduce_mem_usage(df)
 cols_to_drop = ['Id', 'groupId', 'matchId', 'winPlacePerc','Type','matchType']
features = [col for col in ColumnList if col not in cols_to_drop]
df_Group = df.groupby(['matchId','groupId'])
 df= reduce_mem_usage(df)
 df= median_by_team(df,df_Group,features)
 df= reduce_mem_usage(df)
 df= rank_by_team(df,df_Group,features)
 df= reduce_mem_usage(df)
 print("New variables created")
 for colname in ColumnList:
df[colname+ '_1']= df[colname]/(max(df[colname])- min(df[colname]))
df= df.drop(columns= [colname])
 df= reduce_mem_usage(df)
 print(colname,'_1',' created')
 df['totalDistance'] = df['rideDistance_1'] + df['walkDistance_1'] + df['swimDistance_1']
print("New variables created : totalDistance")
 df['kills_assists'] =(df['kills_1'] + df['assists_1'])
 print("New variables created : kills_assists")
 df['healthitems'] = df['heals_1'] + df['boosts_1']
print("New variables created : healthitems")
 df['kills_']= df['kills_1'] + df['longestKill_1']+ df['killStreaks_1']
print("New variables created : kills_")
 df['killtypes']= df['headshotKills_1'] + df['roadKills_1']+ df['teamKills_1']+ df['vehicleDestroys_1']
print("New variables created : killtypes")
 df['Others']= df['damageDealt_1'] + df['DBNOs_1']+ df['revives_1'] + df['weaponsAcquired_1']
print("New variables created : Others")
 df['Points_'] = df['killPoints_1'] + df['winPoints_1']
print("New variables created : Points_")
 df= reduce_mem_usage(df)
 print(df.columns)
 ColumnList = df.columns.tolist()
print(ColumnList)
 ColumnList.remove('Id')
 ColumnList.remove('groupId')
 ColumnList.remove('matchId')
 ColumnList.remove('Type')
 ColumnList.remove('winPlacePerc')
 ColumnList.remove('matchType')
 df= reduce_mem_usage(df)
 df_Train = df[df['Type']== 'Train']
df_Test = df[df['Type']== 'Test']
print("Train and Test Data sets created")
 df_Train= df_Train.set_index(['Id', 'groupId', 'matchId','Type'])
 df_Test= df_Test.set_index(['Id', 'groupId', 'matchId','Type'])
 print("Indexes set")
 X_Train = df_Train[ColumnList]
Y_Train = df_Train[Y_Column]
X_test = df_Test[ColumnList]
print(X_Train.shape, Y_Train.shape, X_test.shape)
 del df, df_Train, df_Test
score['Class'] = 0
score['CE'] = 0
score['CN'] = 0
score['CP'] = 0
for i in score:
score.loc[(( score.Embarked.values == 1)) ,'CE']=1
score.loc[(( score.Name.values == 2)) ,'CN']=1
score.loc[(( score.Name.values == 3)) ,'CN']=5
score.loc[(( score.Pclass.values == 1)) ,'Class']=1
score.loc[(( score.Pclass.values == 3)) ,'Class']=-1
score['Class'] = score['CE'] + score['CN'] + score['CP']
score.head(3 )<feature_engineering>
import lightgbm as lgb
from sklearn.metrics import mean_absolute_error
score['Wealth'] = 0
score['WC'] = 0
score['WF'] = 0
score['WT'] = 0
for i in score:
score.loc[(( score.Cabin.values == 8)) ,'WC']=-5
score.loc[(( score.Cabin.values == 11)) ,'WC']=-1
score.loc[(( score.Cabin.values == 3)) ,'WC']=1
score.loc[(( score.Cabin.values == 6)) ,'WC']=1
score.loc[(( score.Cabin.values == 7)) ,'WC']=1
score.loc[(( score.Cabin.values == 2)) ,'WC']=3
score.loc[(( score.Cabin.values == 4)) ,'WC']=3
score.loc[(( score.Cabin.values == 5)) ,'WC']=3
score.loc[(( score.Fare.values <= 5)) ,'WF']=-5
score.loc[(( score.Fare.values == 9.5)) ,'WF']=-3
score.loc[(( score.Fare.values == 7.5)) ,'WF']=-1
score.loc[(( score.Fare.values == 62)) ,'WF']=1
score.loc[(( score.Fare.values >= 100)) ,'WF']=3
score.loc[(( score.Ticket.values >= 4)&(score.Ticket.values <= 7)) ,'WT']=-5
score.loc[(( score.Ticket.values >= 8)&(score.Ticket.values <= 9)) ,'WT']=-3
score.loc[(( score.Ticket.values == 10)) ,'WT']=-1
score.loc[(( score.Ticket.values == 1)) ,'WT']=1
score.loc[(( score.Ticket.values >= 13)&(score.Ticket.values <= 17)) ,'WT']=1
score.loc[(( score.Ticket.values >= 18)) ,'WT']=5
score['Wealth'] = score['WC'] + score['WF'] + score['WT']
score.head(20 )<feature_engineering>
%%time
starttime = dt.datetime.now()
d_train1 = lgb.Dataset(X_Train, label=Y_Train.values)
 params = {}
params['learning_rate'] = 0.09
params['boosting_type'] = 'gbdt'
params['objective'] = 'regression'
params['metric'] = 'mae'
params['sub_feature'] = 0.8
params['num_leaves'] = 1000
params['min_data'] = 1
params['max_depth'] = 400
params['min_gain_to_split']= 0.0000001
clf1 = lgb.train(params, d_train1, 1000)
 print("Model created in ", dt.datetime.now() - starttime)
 y_pred=clf1.predict(X_test)
 print(y_pred)
score['Priority'] = 0
score['PA'] = 0
score['PN'] = 0
score['PS'] = 0
for i in score:
score.loc[(( score.Age.values == 1)) ,'PA']=5
score.loc[(( score.Age.values == 13)) ,'PA']=1
score.loc[(( score.Age.values == 2)) ,'PA']=1
score.loc[(( score.Age.values == 31)) ,'PA']=-1
score.loc[(( score.Age.values == 7)) ,'PA']=1
score.loc[(( score.Age.values == 78)) ,'PA']=5
score.loc[(( score.Name.values == 4)) ,'PN']=-1
score.loc[(( score.Name.values == 5)) ,'PN']=3
score.loc[(( score.Name.values == 8)) ,'PN']=1
score.loc[(( score.Name.values == 9)) ,'PN']=1
score.loc[(( score.Sex.values == 1)) ,'PS']=3
score.loc[(( score.Sex.values == 3)) ,'PS']=-3
score.loc[(( score.Sex.values == 4)) ,'PS']=5
score.loc[(( score.Sex.values == 5)) ,'PS']=5
score.loc[(( score.Sex.values >= 6)) ,'PS']=1
score['Priority'] = score['PA'] + score['PN'] + score['PS']
score.head(3 )<feature_engineering>
del X_Train
X_test = X_test.reset_index()
X_test["winPlacePerc"] = y_pred
X_test = X_test[['Id',"winPlacePerc"]]
df_test = df_test[['Id','groupId']]
df_test= df_test.merge(X_test, on = ['Id'])
 del X_test
df_test["winPlacePerc"]= df_test["winPlacePerc"].clip(lower = 0.0, upper= 1.0)
 print(df_test.head() )
score['Situation'] = 0
score['SA'] = 0
score['SF'] = 0
for i in score:
score.loc[(( score.Age.values == 36)) ,'SA']=1
score.loc[(( score.Family.values == 2)) ,'SF']=1
score.loc[(( score.Family.values == 3)) ,'SF']=1
score.loc[(( score.Family.values == 4)) ,'SF']=3
score['Situation'] = score['SA'] + score['SF']
score.head(20 )<feature_engineering>
df_test[['Id',"winPlacePerc"]].to_csv("submission.csv", index=False )