Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
df_all = concat_df(df_train, df_test)
 df_all.head()<feature_engineering>
print('train')
 train_df = import_data('.. /input/train.csv')
 print('test')
 test_df = import_data('.. /input/test.csv' )
df_all['Fare'] = pd.qcut(df_all['Fare'], 13 )<feature_engineering>
def featureEngineering(df):
df_size = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_mean = df.groupby(['matchId','groupId'] ).mean().reset_index()
df_max = df.groupby(['matchId','groupId'] ).max().reset_index()
df_min = df.groupby(['matchId','groupId'] ).min().reset_index()
df_match_mean = df.groupby(['matchId'] ).mean().reset_index()
df_train_max_PG = df.groupby(['matchId','groupId'])['kills'].count().reset_index().groupby('matchId')['kills'].max().reset_index()
df_train_max_PG.columns = ['matchId','max_players_in_group']
df = pd.merge(df, df_mean, suffixes=["", "_mean"], how='left', on=['matchId', 'groupId'])
 df = pd.merge(df, df_max, suffixes=["", "_max"], how='left', on=['matchId', 'groupId'])
 df = pd.merge(df, df_min, suffixes=["", "_min"], how='left', on=['matchId', 'groupId'])
 df = pd.merge(df, df_match_mean, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 df = pd.merge(df, df_size, how='left', on=['matchId', 'groupId'])
 df = pd.merge(df, df_train_max_PG, how='left', on=['matchId'])
 return df
train_df = featureEngineering(train_df)
 test_df = featureEngineering(test_df )
df_all['Age'] = pd.qcut(df_all['Age'], 10 )<categorify>
print('Old size: %d' % len(train_df))
train_df = train_df[(train_df.kills <= 35)&(train_df.assists <= 13)&(train_df.boosts <= 16)&(train_df.damageDealt <= 3500)
 &(train_df.DBNOs <= 35)&(train_df.headshotKills <= 22)&(train_df.killStreaks <= 10)&(train_df.assists <= 13)
 &(train_df.longestKill <= 1000)&(train_df.revives <= 20)&(train_df.rideDistance <= 28000)&(train_df.roadKills <= 15)
 &(train_df.swimDistance <= 3500)&(train_df.teamKills <= 10)&(train_df.vehicleDestroys <= 3)&(train_df.walkDistance <= 13000)
 &(train_df.weaponsAcquired <= 60)]
print('New size: %d' % len(train_df))
def headshot_precent(data):
data['headPerc']=data['headshotKills']/data['kills']
data['roadPerc']=data['roadKills']/data['kills']
data['totalDistance']=data['rideDistance']+data['swimDistance']+data['walkDistance']
data['rideDistancePerc']=data['rideDistance']/data['totalDistance']
data['swimDistancePerc']=data['swimDistance']/data['totalDistance']
data['walkDistancePerc']=data['walkDistance']/data['totalDistance']
data.fillna(0,axis=1,inplace=True)
 return data.head()
headshot_precent(train_df)
 headshot_precent(test_df )
df_all['Ticket_Frequency'] = df_all.groupby('Ticket')['Ticket'].transform('count' )<feature_engineering>
features_not2use = ['Id', 'groupId', 'matchId','winPlacePerc_mean','winPlacePerc_max','winPlacePerc_min','winPlacePerc_match_mean']
for df in [train_df]:
df.drop(features_not2use, axis=1, inplace=True)
 features_not2use = ['Id', 'groupId', 'matchId']
for df in [test_df]:
df.drop(features_not2use, axis=1, inplace=True)
 X_train = train_df.sample(frac=0.8)
 X_val = train_df.loc[~train_df.index.isin(X_train.index)]
df_all['Title'] = df_all['Name'].str.split(', ', expand=True)[1].str.split('.', expand=True)[0]
df_all['Is_Married'] = 0
df_all['Is_Married'].loc[df_all['Title'] == 'Mrs'] = 1<string_transform>
y_train = X_train['winPlacePerc']
X_train.drop('winPlacePerc', axis=1, inplace=True)
 y_val = X_val['winPlacePerc']
X_val.drop('winPlacePerc', axis=1, inplace=True )
def extract_surname(data):
families = []
for i in range(len(data)) :
name = data.iloc[i]
if '(' in name:
name_no_bracket = name.split('(')[0]
else:
name_no_bracket = name
family = name_no_bracket.split(',')[0]
title = name_no_bracket.split(',')[1].strip().split(' ')[0]
for c in string.punctuation:
family = family.replace(c, '' ).strip()
families.append(family)
 return families
df_all['Family'] = extract_surname(df_all['Name'])
 df_train = df_all.loc[:890]
df_test = df_all.loc[891:]
dfs = [df_train, df_test]<define_variables>
start_time = time.time()
model = LGBMRegressor(iterations=250, learning_rate=0.05, loss_function='MAE',eval_metric='MAE', depth = -1,
use_best_model=True, od_type="Iter", od_wait=10, thread_count=128, random_seed = 123, num_leaves= 144,n_estimators= 800,
bagging_fraction= 0.8, bagging_freq= 5, feature_fraction= 0.9, objective= 'regression_l2')
 model.fit(X_train, y_train, eval_set=(X_val, y_val))
end_time = time.time()
print('The training time = {}'.format(end_time - start_time))
mean_survival_rate = np.mean(df_train['Survived'])
 train_family_survival_rate = []
train_family_survival_rate_NA = []
test_family_survival_rate = []
test_family_survival_rate_NA = []
for i in range(len(df_train)) :
if df_train['Family'][i] in family_rates:
train_family_survival_rate.append(family_rates[df_train['Family'][i]])
 train_family_survival_rate_NA.append(1)
 else:
train_family_survival_rate.append(mean_survival_rate)
 train_family_survival_rate_NA.append(0)
 for i in range(len(df_test)) :
if df_test['Family'].iloc[i] in family_rates:
test_family_survival_rate.append(family_rates[df_test['Family'].iloc[i]])
 test_family_survival_rate_NA.append(1)
 else:
test_family_survival_rate.append(mean_survival_rate)
 test_family_survival_rate_NA.append(0)
 df_train['Family_Survival_Rate'] = train_family_survival_rate
df_train['Family_Survival_Rate_NA'] = train_family_survival_rate_NA
df_test['Family_Survival_Rate'] = test_family_survival_rate
df_test['Family_Survival_Rate_NA'] = test_family_survival_rate_NA
train_ticket_survival_rate = []
train_ticket_survival_rate_NA = []
test_ticket_survival_rate = []
test_ticket_survival_rate_NA = []
for i in range(len(df_train)) :
if df_train['Ticket'][i] in ticket_rates:
train_ticket_survival_rate.append(ticket_rates[df_train['Ticket'][i]])
 train_ticket_survival_rate_NA.append(1)
 else:
train_ticket_survival_rate.append(mean_survival_rate)
 train_ticket_survival_rate_NA.append(0)
 for i in range(len(df_test)) :
if df_test['Ticket'].iloc[i] in ticket_rates:
test_ticket_survival_rate.append(ticket_rates[df_test['Ticket'].iloc[i]])
 test_ticket_survival_rate_NA.append(1)
 else:
test_ticket_survival_rate.append(mean_survival_rate)
 test_ticket_survival_rate_NA.append(0)
 df_train['Ticket_Survival_Rate'] = train_ticket_survival_rate
df_train['Ticket_Survival_Rate_NA'] = train_ticket_survival_rate_NA
df_test['Ticket_Survival_Rate'] = test_ticket_survival_rate
df_test['Ticket_Survival_Rate_NA'] = test_ticket_survival_rate_NA<feature_engineering>
pred = model.predict(test_df )
<categorify><EOS> 
test_new = import_data('.. /input/test.csv')
 test_new['winPlacePercPred'] = pred
aux = test_new.groupby(['matchId','groupId'])['winPlacePercPred'].agg('mean' ).groupby('matchId' ).rank(pct=True ).reset_index()
aux.columns = ['matchId','groupId','winPlacePerc']
test_new = test_new.merge(aux, how='left', on=['matchId','groupId'])
 submission = import_data('.. /input/sample_submission.csv')
 submission = test_new[['Id','winPlacePerc']]
submission.to_csv('sample_submission.csv', index=False )