Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<set_options>
sns.set_style('darkgrid')
 sns.set_palette('bone')
 pd.options.display.float_format = '{:,.3f}'.format
print(os.listdir(".. /input"))
pd.set_option("display.max_rows",200)
 pd.set_option("display.max_columns",200)
 %matplotlib inline
sns.set_style('whitegrid')
<load_from_csv>
def toTapleList(list1,list2):
return list(itertools.product(list1,list2))
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 print(len(df_train),len(df_test))<predict_on_test>
%%time
train = pd.read_csv('.. /input/train_V2.csv')
 train = reduce_mem_usage(train)
 test = pd.read_csv('.. /input/test_V2.csv')
 test = reduce_mem_usage(test)
 print(train.shape, test.shape )
df_test.head() 
<filter>
null_cnt = train.isnull().sum().sort_values()
print('null count:', null_cnt[null_cnt > 0])
 train.dropna(inplace=True )
len(df_train[df_train['Name'].str.contains('Dr.')] )<filter>
for c in ['Id','groupId','matchId']:
print(f'unique [{c}] count:', train[c].nunique() )
len(df_train[df_train['Name'].str.contains('Sir.')] )<count_missing_values>
for q in ['numGroups == maxPlace','numGroups != maxPlace']:
print(q, ':', len(train.query(q)) )
len(df_train[df_train['Cabin'].isnull() ])
<drop_column>
print(group['players in group'].nlargest(5))
del match,group
df_train.drop(['Name','PassengerId','Ticket','Cabin'],axis=1,inplace=True)
 df_train.head()<filter>
( train.groupby('matchId')['matchDuration'].nunique() > 1 ).any()
df_train[df_train['Embarked'].isnull() ]<count_values>
df_train['Embarked'].value_counts()<correct_missing_values>
print('match count:', train['matchId'].nunique())
 maxPlacePerc = train.groupby('matchId')['winPlacePerc'].max()
print('match [not contains 1st place]:', len(maxPlacePerc[maxPlacePerc != 1]))
del maxPlacePerc
sub = train[(train['maxPlace'] > 1)&(train['numGroups'] == 1)]
print('match [maxPlace>1 & numGroups==1]:', len(sub.groupby('matchId')))
 print(' - unique winPlacePerc:', sub['winPlacePerc'].unique() )
df_train['Embarked'].fillna('S',inplace=True)
 df_train[df_train['Embarked'].isnull() ]<categorify>
pd.concat([train[train['winPlacePerc'] == 1].head(5),
train[train['winPlacePerc'] == 0].head(5)],
keys=['winPlacePerc_1', 'winPlacePerc_0'] )
df_train = pd.concat([df_train,pd.get_dummies(df_train['Embarked'],prefix='Embarked')],axis=1)
 df_train.drop('Embarked',axis=1,inplace=True)
 df_train.head()<split>
grpSolo.nlargest(5, 'kills' )
df_train[df_train['Fare'].isnull() ]<create_dataframe>
grpTeam.nlargest(5, 'kills' )
avg_fare = pd.DataFrame([fare_notsurv.mean() ,fare_surv.mean() ])
 std_fare = pd.DataFrame([fare_notsurv.std() ,fare_surv.std() ])
 print("Mean fare for not survived is {} and survived is {}".format(fare_notsurv.mean() ,\
fare_surv.mean()))<filter>
del grpSolo, grpTeam
df_train[df_train['Age'].isnull() ]<create_dataframe>
def printMatchStats(matchIds):
for mid in matchIds:
subMatch = train[train['matchId'] == mid]
print('matchType:', subMatch['matchType'].values[0])
 grp1st = subMatch[subMatch['winPlacePerc'] == 1]
grpOther = subMatch[subMatch['winPlacePerc'] != 1]
print('players'.ljust(10), ' total:{:>3} 1st:{:>3} other:{:>3}'.format(len(subMatch), len(grp1st), len(grpOther)))
 for c in ['kills','teamKills','roadKills','DBNOs','revives','assists']:
print(c.ljust(10), ' total:{:>3} 1st:{:>3} other:{:>3}'.format(subMatch[c].sum() , grp1st[c].sum() , grpOther[c].sum()))
print('-' * 30 )
avg_age = pd.DataFrame([age_notsurv.mean() ,age_surv.mean() ])
 std_age = pd.DataFrame([age_notsurv.std() ,age_surv.std() ])
 print("Mean age for not survived is {} and survived is {}".format(age_notsurv.mean() ,\
age_surv.mean()))<drop_column>
sampleMid = train['matchId'].unique() [0:5]
printMatchStats(sampleMid )
df_train['Family'] = df_train['Parch'] + df_train['SibSp']
df_train.drop(['Parch','SibSp'],axis=1,inplace=True )<count_values>
match = train.groupby(['matchId'])['Id'].count()
fullplayer = match[match == 100].reset_index()
sampleMid = fullplayer['matchId'][0:5]
printMatchStats(sampleMid )
df_train['Family'].loc[df_train['Family']>1]=1
df_train['Family'].loc[df_train['Family']==0]=0
df_train['Family'].value_counts()<feature_engineering>
all_data = train.append(test, sort=False ).reset_index(drop=True)
 del train, test
gc.collect()
def get_person(passenger):
age,sex=passenger
return 'child' if age < 12 else sex
df_train['Person'] = df_train[['Age','Sex']].apply(get_person,axis=1)
 df_train.drop('Sex',axis=1,inplace=True )<categorify>
match = all_data.groupby('matchId')
 all_data['killsPerc'] = match['kills'].rank(pct=True ).values
all_data['killPlacePerc'] = match['killPlace'].rank(pct=True ).values
all_data['walkDistancePerc'] = match['walkDistance'].rank(pct=True ).values
all_data['walkPerc_killsPerc'] = all_data['walkDistancePerc'] / all_data['killsPerc']
df_train = pd.concat([df_train,pd.get_dummies(df_train['Person'],prefix='Person')],axis=1)
 df_train.drop(['Person'],axis=1,inplace=True )<categorify>
all_data['_totalDistance'] = all_data['rideDistance'] + all_data['walkDistance'] + all_data['swimDistance']
df_test.drop(['Name','Ticket','Cabin'],axis=1,inplace=True)
 df_test['Embarked'].fillna('S',inplace=True)
 df_test = pd.concat([df_test,pd.get_dummies(df_test['Embarked'],prefix='Embarked')],axis=1)
 df_test.drop('Embarked',axis=1,inplace=True)
 df_test['Fare'].fillna(df_test['Fare'].mean() ,inplace=True)
 df_test['Age'].fillna(df_test['Age'].dropna().median() ,inplace=True)
 df_test['Family'] = df_test['Parch'] + df_test['SibSp']
df_test.drop(['Parch','SibSp'],axis=1,inplace=True)
 df_test['Family'].loc[df_test['Family']>1]=1
df_test['Family'].loc[df_test['Family']==0]=0
def get_person(passenger):
age,sex=passenger
return 'child' if age < 12 else sex
df_test['Person'] = df_test[['Age','Sex']].apply(get_person,axis=1)
 df_test.drop('Sex',axis=1,inplace=True)
 df_test = pd.concat([df_test,pd.get_dummies(df_test['Person'],prefix='Person')],axis=1)
 df_test.drop(['Person'],axis=1,inplace=True)
 df_test.head()<drop_column>
def fillInf(df, val):
numcols = df.select_dtypes(include='number' ).columns
cols = numcols[numcols != 'winPlacePerc']
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
for c in cols: df[c].fillna(val, inplace=True )
features_train = df_train.drop(['Survived'],axis=1)
 target_train= df_train['Survived']
features_test = df_test.drop(['PassengerId'],axis=1 )<split>
all_data['_healthItems'] = all_data['heals'] + all_data['boosts']
all_data['_headshotKillRate'] = all_data['headshotKills'] / all_data['kills']
all_data['_killPlaceOverMaxPlace'] = all_data['killPlace'] / all_data['maxPlace']
all_data['_killsOverWalkDistance'] = all_data['kills'] / all_data['walkDistance']
fillInf(all_data, 0 )
train_x,test_x,train_y,test_y = train_test_split(features_train,target_train,test_size=0.2,random_state=42)
<predict_on_test>
all_data.drop(['boosts','heals','killStreaks','DBNOs'], axis=1, inplace=True)
 all_data.drop(['headshotKills','roadKills','vehicleDestroys'], axis=1, inplace=True)
 all_data.drop(['rideDistance','swimDistance','matchDuration'], axis=1, inplace=True)
 all_data.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True )
clf_nb = GaussianNB()
clf_nb.fit(features_train,target_train)
 target_test_nb = clf_nb.predict(features_test )<save_to_csv>
match = all_data.groupby(['matchId'])
 group = all_data.groupby(['matchId','groupId','matchType'])
 agg_col = list(all_data.columns)
 exclude_agg_col = ['Id','matchId','groupId','matchType','maxPlace','numGroups','winPlacePerc']
for c in exclude_agg_col:
agg_col.remove(c)
 print(agg_col)
 sum_col = ['kills','killPlace','damageDealt','walkDistance','_healthItems']
df_test['Survived'] = target_test_nb
df_test[['PassengerId','Survived']].to_csv('gaussnb-kaggle.csv',index=False )<predict_on_test>

 minKills = all_data.sort_values(['matchId','groupId','kills','killPlace'] ).groupby(
 ['matchId','groupId','kills'] ).first().reset_index().copy()
for n in np.arange(4):
c = 'kills_' + str(n)+ '_Place'
nKills =(minKills['kills'] == n)
 minKills.loc[nKills, c] = minKills[nKills].groupby(['matchId'])['killPlace'].rank().values
match_data = pd.merge(match_data, minKills[nKills][['matchId','groupId',c]], how='left')
 match_data = reduce_mem_usage(match_data)
 del minKills, nKills
print(match_data.shape )
clf_nb.fit(train_x,train_y)
 pred_gnb_y = clf_nb.predict(test_x)
 print('Accuracy score of Gaussian NB is {}'.format(metrics.accuracy_score(pred_gnb_y,test_y)) )<train_on_grid>
svc = SVC(kernel='rbf',class_weight='balanced')
 param_grid_svm = {'C': [1, 5, 10, 50,100],
'gamma': [0.0001, 0.0005, 0.001, 0.005,0.01]}
grid_svm = GridSearchCV(estimator=svc, param_grid=param_grid_svm)
 grid_svm.fit(train_x,train_y)
 grid_svm.best_params_<predict_on_test>

 all_data = pd.merge(all_data, match_data)
 del match_data
gc.collect()
all_data['enemy.players'] = all_data['m.players'] - all_data['players']
for c in sum_col:
all_data['p.max_msum.' + c] = all_data['max.' + c] / all_data['m.sum.' + c]
all_data['p.max_mmax.' + c] = all_data['max.' + c] / all_data['m.max.' + c]
all_data.drop(['m.sum.' + c, 'm.max.' + c], axis=1, inplace=True)
 fillInf(all_data, 0)
 print(all_data.shape )
clf_svm = grid_svm.best_estimator_
clf_svm.fit(features_train,target_train)
 target_test_svm = clf_svm.predict(features_test )<save_to_csv>

 match = all_data.groupby('matchId')
 matchRank = match[numcols].rank(pct=True ).rename(columns=lambda s: 'rank.' + s)
 all_data = reduce_mem_usage(pd.concat([all_data, matchRank], axis=1))
rank_col = matchRank.columns
del matchRank
gc.collect()
match = all_data.groupby('matchId')
 matchRank = match[rank_col].max().rename(columns=lambda s: 'max.' + s ).reset_index()
all_data = pd.merge(all_data, matchRank)
 for c in numcols:
all_data['rank.' + c] = all_data['rank.' + c] / all_data['max.rank.' + c]
all_data.drop(['max.rank.' + c], axis=1, inplace=True)
 del matchRank
gc.collect()
print(all_data.shape )
df_test['Survived'] = target_test_svm
df_test[['PassengerId','Survived']].to_csv('svm-kaggle.csv',index=False )<predict_on_test>

 killMinorRank = all_data[['matchId','min.kills','max.killPlace']].copy()
group = killMinorRank.groupby(['matchId','min.kills'])
 killMinorRank['rank.minor.maxKillPlace'] = group.rank(pct=True ).values
all_data = pd.merge(all_data, killMinorRank)
 killMinorRank = all_data[['matchId','max.kills','min.killPlace']].copy()
group = killMinorRank.groupby(['matchId','max.kills'])
 killMinorRank['rank.minor.minKillPlace'] = group.rank(pct=True ).values
all_data = pd.merge(all_data, killMinorRank)
 del killMinorRank
gc.collect()
clf_svm.fit(train_x,train_y)
 pred_svm_y = clf_svm.predict(test_x)
 print('Accuracy score of SVM is {}'.format(metrics.accuracy_score(pred_svm_y,test_y)) )<train_on_grid>
constant_column = [col for col in all_data.columns if all_data[col].nunique() == 1]
print('drop columns:', constant_column)
 all_data.drop(constant_column, axis=1, inplace=True )
rf = RandomForestClassifier(criterion='entropy')
 param_grid_rf = {'n_estimators':[10,100,250,500,1000],
'max_features':['sqrt','log2'],'min_samples_split':[2,5,10,50,100]}
grid_rf = GridSearchCV(estimator=rf,param_grid=param_grid_rf)
 grid_rf.fit(train_x,train_y)
 grid_rf.best_params_<predict_on_test>

 all_data['matchType'] = all_data['matchType'].apply(mapper)
 all_data = pd.concat([all_data, pd.get_dummies(all_data['matchType'])], axis=1)
 all_data.drop(['matchType'], axis=1, inplace=True)
 all_data['matchId'] = all_data['matchId'].apply(lambda x: int(x,16))
all_data['groupId'] = all_data['groupId'].apply(lambda x: int(x,16))
clf_rf = grid_rf.best_estimator_
clf_rf.fit(features_train,target_train)
 target_test_rf = clf_rf.predict(features_test )<save_to_csv>
null_cnt = all_data.isnull().sum().sort_values()
print(null_cnt[null_cnt > 0] )
df_test['Survived'] = target_test_rf
df_test[['PassengerId','Survived']].to_csv('rf-kaggle.csv',index=False )<predict_on_test>
cols = [col for col in all_data.columns if col not in ['Id','matchId','groupId']]
for i, t in all_data.loc[:, cols].dtypes.iteritems() :
if t == object:
all_data[i] = pd.factorize(all_data[i])[0]
all_data = reduce_mem_usage(all_data)
 all_data.head()
clf_rf.fit(train_x,train_y)
 pred_rf_y = clf_rf.predict(test_x)
 print('Accuracy score of RF is {}'.format(metrics.accuracy_score(pred_rf_y,test_y)) )<save_to_csv>
X_train = all_data[all_data['winPlacePerc'].notnull() ].reset_index(drop=True)
 X_test = all_data[all_data['winPlacePerc'].isnull() ].drop(['winPlacePerc'], axis=1 ).reset_index(drop=True)
 del all_data
gc.collect()
Y_train = X_train.pop('winPlacePerc')
 X_test_grp = X_test[['matchId','groupId']].copy()
train_matchId = X_train['matchId']
X_train.drop(['matchId','groupId'], axis=1, inplace=True)
 X_test.drop(['matchId','groupId'], axis=1, inplace=True)
 print(X_train.shape, X_test.shape )
target_avg = 0.2 * target_test_nb + 0.3 * target_test_svm + 0.5 * target_test_rf
df_test['Survived'] = target_test_rf
df_test[['PassengerId','Survived']].to_csv('avg-kaggle.csv',index=False )<load_from_csv>
print(pd.DataFrame([[val for val in dir() ], [sys.getsizeof(eval(val)) for val in dir() ]],
index=['name','size'] ).T.sort_values('size', ascending=False ).reset_index(drop=True)[:10] )
train_dataset = pd.read_csv('.. /input/train.csv')
 test_dataset = pd.read_csv('.. /input/test.csv')
 train_dataset.describe()<prepare_x_and_y>
params={'learning_rate': 0.05,
'objective':'mae',
'metric':'mae',
'num_leaves': 128,
'verbose': 1,
'random_state':42,
'bagging_fraction': 0.7,
'feature_fraction': 0.7
}
reg = lgb.LGBMRegressor(**params, n_estimators=10000)
 reg.fit(X_train, Y_train)
 pred = reg.predict(X_test, num_iteration=reg.best_iteration_ )
y_train = train_dataset.iloc[:, 1].values
X_train = train_dataset.iloc[:, [0, 2, 4, 5, 6, 7, 11]].values
X_test = test_dataset.iloc[:, [0, 1, 3, 4, 5, 6, 10]].values
m = X_train.shape[0]
family_size_column = np.zeros(( m, 1))
X_train = np.append(X_train, family_size_column, axis=1)
 X_train[:, 7] = 1 + X_train[:, 4] + X_train[:, 5]
X_train[:, 1] = X_train[:, 1] * X_train[:, 7]
X_train = np.delete(X_train, [4, 5, 7], 1)
 m = X_test.shape[0]
family_size_column = np.zeros(( m, 1))
X_test = np.append(X_test, family_size_column, axis=1)
 X_test[:, 7] = 1 + X_test[:, 4] + X_test[:, 5]
X_test[:, 1] = X_test[:, 1] * X_test[:, 7]
X_test = np.delete(X_test, [4, 5, 7], 1)
 m = X_test.shape[0]
pred_column = np.zeros(( m, 1))
result = test_dataset.iloc[:, [0]].values
result = np.append(result, pred_column, axis=1)
 result = result.astype(int)
 result<define_variables>
X_test_grp['_nofit.winPlacePerc'] = pred
group = X_test_grp.groupby(['matchId'])
 X_test_grp['winPlacePerc'] = pred
X_test_grp['_rank.winPlacePerc'] = group['winPlacePerc'].rank(method='min')
 X_test = pd.concat([X_test, X_test_grp], axis=1 )
nan_age_train = train_dataset[train_dataset['Age'].isnull() ]
nan_age_train.shape[0]<define_variables>
fullgroup =(X_test['numGroups'] == X_test['maxPlace'])
 subset = X_test.loc[fullgroup]
X_test.loc[fullgroup, 'winPlacePerc'] =(subset['_rank.winPlacePerc'].values - 1)/(subset['maxPlace'].values - 1)
 subset = X_test.loc[~fullgroup]
gap = 1.0 /(subset['maxPlace'].values - 1)
 new_perc = np.around(subset['winPlacePerc'].values / gap)* gap
X_test.loc[~fullgroup, 'winPlacePerc'] = new_perc
X_test['winPlacePerc'] = X_test['winPlacePerc'].clip(lower=0,upper=1 )
nan_age_test = test_dataset[test_dataset['Age'].isnull() ]
nan_age_test.shape[0]<define_variables>
_=
nan_embarked_train = train_dataset[train_dataset['Embarked'].isnull() ]
nan_embarked_train.shape[0]<define_variables>
X_test.loc[X_test['maxPlace'] == 0, 'winPlacePerc'] = 0
X_test.loc[X_test['maxPlace'] == 1, 'winPlacePerc'] = 1
X_test.loc[(X_test['maxPlace'] > 1)&(X_test['numGroups'] == 1), 'winPlacePerc'] = 0
X_test['winPlacePerc'].describe()
nan_embarked_test = test_dataset[test_dataset['Embarked'].isnull() ]
nan_embarked_test.shape[0]<categorify>
test = pd.read_csv('.. /input/test_V2.csv')
 test['matchId'] = test['matchId'].apply(lambda x: int(x,16))
test['groupId'] = test['groupId'].apply(lambda x: int(x,16))
submission = pd.merge(test, X_test[['matchId','groupId','winPlacePerc']])
 submission = submission[['Id','winPlacePerc']]
submission.to_csv("submission.csv", index=False )