Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<load_from_csv>
gc.enable()
df_test= pd.read_csv('/kaggle/input/titanic-extended/test.csv')
 df_train= pd.read_csv('.. /input/titanic-extended/train.csv')
 df_train.head()<count_missing_values>
def feature_engineering(is_train=True):
if is_train:
print("processing train.csv")
 df = pd.read_csv(".. /input/pubg-finish-placement-prediction/train_V2.csv")
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 df = pd.read_csv(".. /input/pubg-finish-placement-prediction/test_V2.csv")
 df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
print("remove some columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 y = None
print("get target")
 if is_train:
y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
if is_train: df_out = agg.reset_index() [['matchId','groupId']]
else: df_out = df[['matchId','groupId']]
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_mean", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 print("get group sum feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('sum')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_sum", "_sum_rank"], how='left', on=['matchId', 'groupId'])
 df_out=reduce_mem_usage(df_out)
 print("get group max feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('max')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_max", "_max_rank"], how='left', on=['matchId', 'groupId'])
 print("get group min feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('min')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_min", "_min_rank"], how='left', on=['matchId', 'groupId'])
 df_out=reduce_mem_usage(df_out)
 print("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 df_out=reduce_mem_usage(df_out)
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = np.array(df_out)
 feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y, feature_names
df_train.isnull().sum().sort_index() /len(df_train )<groupby>
x_train, y, feature_names = feature_engineering(True )
df_train.dtypes
g_train =df_train.columns.to_series().groupby(df_train.dtypes ).groups
g_train<define_variables>
X_train, X_val, y_train, y_val = train_test_split(x_train, y, test_size=0.33, random_state=42 )
cat_names= [
'Name', 'Sex', 'Ticket', 'Cabin',
'Embarked', 'Name_wiki', 'Hometown',
'Boarded', 'Destination', 'Lifeboat',
'Body'
]
cont_names = [
'PassengerId', 'Pclass', 'SibSp', 'Parch',
'Age', 'Fare', 'WikiId', 'Age_wiki','Class'
] 
<create_dataframe>
warnings.filterwarnings("ignore")
splits = RandomSplitter(valid_pct=0.2 )(range_of(df_train))
to = TabularPandas(df_train, procs=[Categorify, FillMissing,Normalize],
cat_names = cat_names,
cont_names = cont_names,
y_names='Survived',
splits=splits )<groupby>
train_data=lgb.Dataset(X_train, label=y_train)
 val_data= lgb.Dataset(X_val, label=y_val)
 params = {
'num_leaves': 144,
'learning_rate': 0.1,
'n_estimators': 1500,
'max_depth':12,
'max_bin':55,
'bagging_fraction':0.8,
'bagging_freq':5,
'feature_fraction':0.9,
'verbose':50,
'early_stopping_rounds':100
}
params['metric'] = 'auc'
lgb_model= lgb.train(params, train_data, valid_sets=val_data, num_boost_round=3000, early_stopping_rounds=100)
 y_pred=lgb_model.predict(X_val)
 print(mean_absolute_error(y_val,y_pred))
g_train =to.train.xs.columns.to_series().groupby(to.train.xs.dtypes ).groups
g_train<train_model>
y_pred=lgb_model.predict(X_val)
 print(mean_absolute_error(y_val,y_pred))
del X_val
del y_val
to.train<train_model>
x_test, y_test, feature_names = feature_engineering(False )
to.train.xs<prepare_x_and_y>
y_test_pred=lgb_model.predict(x_test )
X_train, y_train = to.train.xs, to.train.ys.values.ravel()
X_valid, y_valid = to.valid.xs, to.valid.ys.values.ravel()<train_model>
df=pd.read_csv(".. /input/pubg-finish-placement-prediction/test_V2.csv")
 var=pd.DataFrame(columns=['Id','winPlacePerc'])
 var['Id']= df['Id']
var['winPlacePerc'] = y_test_pred
submission = var[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )