Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
train_set=features[:len(y)]
test_set=features[len(train_set):]
train_set.head()<drop_column>
def toTapleList(list1,list2):
return list(itertools.product(list1,list2))
train_set.drop('Id', axis=1, inplace=True, errors='raise')
 test_set.drop('Id', axis=1, inplace=True, errors='raise' )<prepare_x_and_y>
%%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 )
X = train_set
test_X = test_set<compute_train_metric>
null_cnt = train.isnull().sum().sort_values()
print(null_cnt[null_cnt > 0])
 train.dropna(inplace=True )
X_train = X
X_test = test_X
y_train = y
def rmse(model):
rmse = np.sqrt(-cross_val_score(model, X_train, y_train, scoring="neg_mean_squared_error", cv = 10))
return(rmse )<train_model>
for c in ['Id','groupId','matchId']:
print(f'unique [{c}] count:', train[c].nunique() )
model_random_forest = make_pipeline(RobustScaler() , RandomForestRegressor(n_estimators = 100 , oob_score = True, n_jobs = -1))
model_random_forest.fit(X_train, y_train)
 randomforest_rmse = rmse(model_random_forest ).mean()<train_model>
for q in ['numGroups == maxPlace','numGroups != maxPlace']:
print(q, ':', len(train.query(q)) )
ridge = make_pipeline(RobustScaler() , linear_model.Ridge())
 ridge.fit(X_train, y_train)
 ridge_rmse = rmse(ridge ).mean()<train_model>
print(group['players in group'].nlargest(5))
del match,group
xg_boost = make_pipeline(RobustScaler() , xgb.XGBRegressor())
 xg_boost.fit(X_train, y_train)
 xgboost_rmse = rmse(xg_boost ).mean()<create_dataframe>
subset = train[train['matchId']=='41a634f62f86b7']
sub_grp = subset[subset['groupId']=='128b07271aa012']
print('matchId=='41a634f62f86b7' & groupId=='128b07271aa012'')
 print('-'*50)
 print('players:',len(subset))
print('groups:',subset['groupId'].nunique())
 print('numGroups:',subset['numGroups'].unique())
 print('maxPlace:',subset['maxPlace'].unique())
 print('-'*50)
 print('max-group players:',len(sub_grp))
print('max-group winPlacePerc:',sub_grp['winPlacePerc'].unique())
 print('-'*50)
 print('winPlacePerc:',subset['winPlacePerc'].sort_values().unique() )
results_df = pd.DataFrame({'Model': ['Lasso', 'RandomForest', 'Ridge', 'XGBoost'], 'Score': [lasso_rmse, randomforest_rmse,ridge_rmse, xgboost_rmse]})
 results_df = results_df.sort_values(by='Score', ascending=True ).reset_index(drop=True)
 results_df<save_to_csv>
train.groupby('matchId' ).agg({'matchDuration':'var'} ).fillna(0 ).query('matchDuration != 0' )
df = pd.DataFrame({'Id': test.Id, 'SalePrice':np.exp(model_lasso.predict(X_test)) })
 df.to_csv('Submission_final.csv', index = False )<define_variables>
cols = ['killPoints','rankPoints','winPoints']
querys = ['rankPoints <= 0 & killPoints != 0', 'rankPoints <= 0 & winPoints != 0',
'killPoints == 0 & rankPoints != 0', 'killPoints == 0 & winPoints != 0',
'winPoints == 0 & rankPoints != 0', 'winPoints == 0 & killPoints != 0']
for q in querys:
print('count(%s):' % q, len(train[cols].query(q)) )
cat_high_corr = ['HeatingQC','GarageType','MSZoning', 'Neighborhood', 'Condition2', 'MasVnrType', 'ExterQual', 'BsmtQual','CentralAir', 'KitchenQual', 'SaleType', 'SaleType']<define_variables>
num_features = train_corr.select_dtypes(include = np.number )<sort_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
edge = train[(train['maxPlace'] > 1)&(train['numGroups'] == 1)]
print('match [maxPlace>1 & numGroups==1]:', len(edge.groupby('matchId')))
 print(' - unique winPlacePerc:', edge['winPlacePerc'].unique() )
num_corr = num_features.corr() ['SalePrice'][:-1]
golden_features_list = num_corr[abs(num_corr)> 0.3].sort_values(ascending=False)
 golden_features_list<filter>
pd.concat([train[train['winPlacePerc'] == 1].head(5),
train[train['winPlacePerc'] == 0].head(5)],
keys=['winPlacePerc_1', 'winPlacePerc_0'] )
features = features_cat[cat_high_corr + list(golden_features_list.index)]<categorify>
all_data = train.append(test, sort=False ).reset_index(drop=True)
 del train, test
gc.collect()
features_cat = pd.get_dummies(features, drop_first = True )<split>
match = all_data.groupby('matchId')
 all_data['killPlacePerc'] = match['kills'].rank(pct=True ).values
train_set=features_cat[:len(y)]
test_set=features_cat[len(train_set):]
train_set.head()<prepare_x_and_y>
distance =(all_data['rideDistance'] + all_data['walkDistance'] + all_data['swimDistance'])
 all_data['zombi'] =(( distance == 0)&(all_data['kills'] == 0)
 &(all_data['weaponsAcquired'] == 0)
 &(all_data['matchType'].str.contains('solo')) ).astype(int)
 all_data['cheater'] =(( all_data['kills'] / distance >= 1)
 |(all_data['kills'] > 30)|(all_data['roadKills'] > 10)).astype(int)
 del distance
pd.concat([all_data['zombi'].value_counts() , all_data['cheater'].value_counts() ], axis=1 ).T
X = train_set
test_X = test_set<define_variables>
all_data.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True )
X_train = X
X_test = test_X
y_train = y<train_model>
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 )
model_random_forest = make_pipeline(RobustScaler() , RandomForestRegressor(n_estimators = 200 , oob_score = True, n_jobs = -1))
model_random_forest.fit(X_train, y_train)
 randomforest_rmse = rmse(model_random_forest ).mean()<train_model>
all_data['_totalDistance'] = all_data['rideDistance'] + all_data["walkDistance"] + all_data["swimDistance"]
all_data["_specialKills"] = all_data["headshotKills"] + all_data["roadKills"]
all_data['_healthItems'] = all_data['heals'] + all_data['boosts']
all_data['_headshotKillRate'] = all_data['headshotKills'] / all_data['kills']
all_data['_killsOverWalkDistance'] = all_data['kills'] / all_data['walkDistance']
all_data['_killsOverDistance'] = all_data['kills'] / all_data['_totalDistance']
all_data['_killPlaceOverMaxPlace'] = all_data['killPlace'] / all_data['maxPlace']
fillInf(all_data, 0 )
ridge = make_pipeline(RobustScaler() , linear_model.Ridge())
 ridge.fit(X_train, y_train)
 ridge_rmse = rmse(ridge ).mean()<train_model>
agg_col = list(all_data.columns)
 exclude_agg_col = ['Id','matchId','groupId','matchType','matchDuration','maxPlace','numGroups','winPlacePerc']
for c in exclude_agg_col:
agg_col.remove(c)
 print(agg_col )
xg_boost = make_pipeline(RobustScaler() , xgb.XGBRegressor())
 xg_boost.fit(X_train, y_train)
 xgboost_rmse = rmse(xg_boost ).mean()<create_dataframe>
def grouping(df):
group = df.groupby(['matchId','groupId','matchType'])
 groupCount = group.size().to_frame('member')
 groupMean = group.mean()
groupMax = group[agg_col].max().rename(columns=lambda s: '_max.' + s)
 groupMin = group[agg_col].min().rename(columns=lambda s: '_min.' + s)
 return pd.concat([groupCount, groupMean, groupMax, groupMin], axis=1 ).reset_index()
all_data = grouping(all_data)
 all_data = reduce_mem_usage(all_data)
 print(all_data.shape )
results_df = pd.DataFrame({'Model': ['Lasso', 'RandomForest', 'Ridge', 'XGBoost'], 'Score': [lasso_rmse, randomforest_rmse,ridge_rmse, xgboost_rmse]})
 results_df = results_df.sort_values(by='Score', ascending=True ).reset_index(drop=True)
 results_df<set_options>
cols = np.r_[agg_col,['matchId']]
match = all_data[cols].groupby('matchId' )
%matplotlib inline 
<load_from_csv>

 cols = agg_col
matchSum = match[cols].sum().rename(columns=lambda s: '_sum.' + s ).reset_index()
all_data = pd.merge(all_data, matchSum)
 print(all_data.shape)
 del matchSum
gc.collect()
for c in cols:
all_data['_percSum.' + c] = all_data[c] / all_data['_sum.' + c]
fillInf(all_data, 0 )
file = '.. /input/train.csv'
df = pd.read_csv(file )<drop_column>

 cols = agg_col
matchMean = match[cols].mean().rename(columns=lambda s: '_matchMean.' + s ).reset_index()
all_data = pd.merge(all_data, reduce_mem_usage(matchMean))
print(all_data.shape)
 del matchMean
gc.collect()
for c in cols:
all_data['_percMean.' + c] = all_data[c] / all_data['_matchMean.' + c]
all_data.drop(['_matchMean.' + c], axis=1, inplace=True)
 fillInf(all_data, 0 )
df = df.drop(['PassengerId', 'Name', 'Ticket'], axis=1 )<count_values>

 killMinorRank = all_data[['matchId','_min.kills','_max.killPlace']].copy()
killMinorRank['_rank.minor.killPlace'] = killMinorRank.groupby(['matchId','_min.kills'] ).rank().values
all_data = pd.merge(all_data, killMinorRank)
 del killMinorRank
gc.collect()
df['Survived'].value_counts()<count_values>
null_cnt = all_data.isnull().sum().sort_values()
print(null_cnt[null_cnt > 0])
 all_data.head()
df['SibSp'].value_counts()<count_missing_values>
mapper = lambda x: 'solo' if('solo' in x)else 'duo' if('duo' in x)or('crash' in x)else 'squad'
all_data['matchTypeCat'] = all_data['matchType'].map(mapper)
 print(all_data['matchTypeCat'].value_counts() )
df.isnull().sum(axis = 0 )<categorify>
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 )
df_hot = pd.concat([df, pd.get_dummies(df['Sex'])], axis=1)
 df_hot = df_hot.drop(['Sex'], axis=1 )<categorify>
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]
df_hot = pd.concat([df_hot, pd.get_dummies(df['Pclass'])], axis=1)
 df_hot = df_hot.drop(['Pclass'], axis=1 )<rename_columns>
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()
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 )
df_hot = df_hot.rename(columns={df_hot.columns[9]:'Pclass1', df_hot.columns[10]:'Pclass2', df_hot.columns[11]:'Pclass3'} )<count_values>
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] )
df_hot['Embarked'].value_counts()<data_type_conversions>
params={'learning_rate': 0.05,
'objective':'mae',
'metric':'mae',
'num_leaves': 31,
'verbose': 0,
'random_state':42,
'bagging_fraction': 0.7,
'feature_fraction': 0.7
}
mts = list()
fis = list()
pred = np.zeros(X_test.shape[0])
 for mt in X_train['matchTypeCat'].unique() :
idx = X_train[X_train['matchTypeCat'] == mt].index
reg = lgb.LGBMRegressor(**params, n_estimators=1000)
 reg.fit(X_train.loc[idx], Y_train.loc[idx])
 idx = X_test[X_test['matchTypeCat'] == mt].index
pred[idx] = reg.predict(X_test.loc[idx], num_iteration=reg.best_iteration_)
 mts.append(mt)
 fis.append(reg.feature_importances_ )
df_hot['Embarked'].fillna("S", inplace = True )<categorify>
X_test_grp['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)
 sub_match = X_test_grp[['matchId','_rank.winPlacePerc']].groupby(['matchId'])
 sub_group = group.count().reset_index() ['matchId'].to_frame()
X_test = pd.merge(X_test, sub_group )
pd.options.display.float_format = '{:.2f}'.format
le = LabelEncoder()
T = df_hot['Embarked']
encoded = le.fit_transform(np.ravel(T))<categorify>
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
df_hot = pd.concat([df_hot, pd.DataFrame(encoded, columns = ['Emb'])], axis=1)
 df_hot = df_hot.drop(['Embarked'], axis=1 )<drop_column>
X_test.loc[~fullgroup, '_pred.winPlacePerc'] = np.around(X_test.loc[~fullgroup, 'winPlacePerc'].values / gap)+ 1
_=
X_test.loc[~fullgroup &(X_test['matchId'] == '000b598b79aa5e'),
['matchId','groupId','winPlacePerc','maxPlace','numGroups','_pred.winPlacePerc','_rank.winPlacePerc']
].sort_values(['matchId','_pred.winPlacePerc'] )
df_hot = df_hot[['Survived', 'SibSp', 'Parch', 'Fare', 'Emb', 'Cabin', 'Age',
'female', 'male', 'Pclass1', 'Pclass2', 'Pclass3']]<drop_column>
_=
df_hot = df_hot.drop(['Cabin'], axis=1 )<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()
<create_dataframe><EOS> 
test = pd.read_csv('.. /input/test_V2.csv')
 submission = pd.merge(test, X_test[['matchId','groupId','winPlacePerc']])
 submission = submission[['Id','winPlacePerc']]
submission.to_csv("submission.csv", index=False )