Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<filter>
gc.enable()
combined_df.loc[combined_df["PassengerId"].isin([1257,980,1234])]<groupby>
print('-' * 80)
 print('train')
 train = import_data('.. /input/train_V2.csv')
combined_df.groupby(["Sex"])["Age"].agg(["median"] )<feature_engineering>
train['totalDistance'] = train['rideDistance'] + train["walkDistance"] + train["swimDistance"]
Age_values_f.loc[(Age_values_f["Age"].isnull())&(Age_values_f["Sex"]=='female'),"Age"]=27.0
Age_values_f.loc[(Age_values_f["Age"].isnull())&(Age_values_f["Sex"]=='male'),"Age"]=28.0<rename_columns>
train.isnull().sum().sum()
train_df.set_index('PassengerId',inplace=True)
 Age_values_f.set_index('PassengerId',inplace=True)
 train_df.update(Age_values_f)
 train_df.reset_index(inplace=True)
 test_df.set_index('PassengerId',inplace=True)
 test_df.update(Age_values_f)
 test_df.reset_index(inplace=True)
<groupby>
y = train['winPlacePerc']
train.drop('winPlacePerc', axis=1, inplace=True )
train_df.groupby(["Embarked"])["Embarked"].agg(["count"] )<categorify>

 train_size = train.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 train_mean = train.groupby(['matchId','groupId'] ).mean().reset_index()
train_max = train.groupby(['matchId','groupId'] ).max().reset_index()
train_min = train.groupby(['matchId','groupId'] ).min().reset_index()
train_df.loc[train_df["PassengerId"]==62.0,"Embarked"]='S'
train_df.loc[train_df["PassengerId"]==830,"Embarked"]='S'<groupby>

 train_match_mean = train.groupby(['matchId'] ).mean().reset_index()
train = pd.merge(train, train_mean, suffixes=["", "_mean"], how='left', on=['matchId', 'groupId'])
 del train_mean
train = pd.merge(train, train_max, suffixes=["", "_max"], how='left', on=['matchId', 'groupId'])
 del train_max
train = pd.merge(train, train_min, suffixes=["", "_min"], how='left', on=['matchId', 'groupId'])
 del train_min
train = pd.merge(train, train_match_mean, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 del train_match_mean
train = pd.merge(train, train_size, how='left', on=['matchId', 'groupId'])
 del train_size
train_columns = list(train.columns )
train_df["Cabin_prefix"]=train_df["Cabin"].str[0:1]
train_df.groupby(["Cabin_prefix","Pclass"])["Pclass"].agg(["count"] ).reset_index()<drop_column>

 train_columns.remove("Id")
 train_columns.remove("matchId")
 train_columns.remove("groupId")
 train_columns.remove("Id_mean")
 train_columns.remove("Id_max")
 train_columns.remove("Id_min")
 train_columns.remove("Id_match_mean")
 train_columns.remove("matchType" )
train_df.drop(columns={"Cabin","Cabin_prefix"},inplace=True )<filter>
X_train = train[train_columns]
test_df.loc[test_df["Fare"].isnull() ]<groupby>
X_train['winPlacePerc'] = y
x_train = X_train.sample(frac=0.8)
 X_val = X_train.loc[~X_train.index.isin(x_train.index)]
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)
 del X_train, train
gc.collect()
print(x_train.info(memory_usage='deep', verbose=False))
print(X_val.info(memory_usage='deep', verbose=False))
train_df.loc[train_df["Pclass"]==3].loc[train_df["Age"]>50].groupby(["Age","Sex"])["Fare"].agg(["mean"] )<groupby>
start_time = time.time()
model = CatBoostRegressor(iterations=500, learning_rate=0.05, loss_function='MAE',eval_metric='MAE', depth = 15,
use_best_model=True, od_type="Iter", od_wait=20, thread_count=128, random_seed = 123)
 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))
train_df.loc[train_df["Pclass"]==3].loc[train_df["Age"]>50].groupby(["Sex"])["Fare"].agg(["mean"] )<feature_engineering>
print('-' * 80)
 print('test')
 test = import_data('.. /input/test_V2.csv')
 test_new = test[['Id', 'matchId', 'groupId']]
test['totalDistance'] = test['rideDistance'] + test["walkDistance"] + test["swimDistance"]
test.info(memory_usage='deep', verbose=False)
 test_size = test.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 test_mean = test.groupby(['matchId','groupId'] ).mean().reset_index()
test_max = test.groupby(['matchId','groupId'] ).max().reset_index()
test_min = test.groupby(['matchId','groupId'] ).min().reset_index()
test_match_mean = test.groupby(['matchId'] ).mean().reset_index()
test = pd.merge(test, test_mean, suffixes=["", "_mean"], how='left', on=['matchId', 'groupId'])
 del test_mean
test = pd.merge(test, test_max, suffixes=["", "_max"], how='left', on=['matchId', 'groupId'])
 del test_max
test = pd.merge(test, test_min, suffixes=["", "_min"], how='left', on=['matchId', 'groupId'])
 del test_min
test = pd.merge(test, test_match_mean, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 del test_match_mean
test = pd.merge(test, test_size, how='left', on=['matchId', 'groupId'])
 del test_size
test_df.loc[test_df["Fare"].isnull() ,"Fare"]=7.518522<prepare_x_and_y>
X_test = test[train_columns]
del test
gc.collect()
related_people=combined_df[["PassengerId","Name","SibSp","Parch","Ticket","Embarked"]].copy()
related_people["Last_Name"]=""
related_people["Last_Name"]=related_people["Name"].str.split(",",expand=True)[0]
related_people["total_related"]=related_people["SibSp"]+related_people["Parch"]+1
X=related_people.loc[(related_people["SibSp"]>0)|(related_people["Parch"]>0)].groupby(["Last_Name","total_related"])["PassengerId"].agg(["count"])
 X.reset_index(inplace=True)
 Y=pd.DataFrame(X.loc[X["count"]==X["total_related"]]["Last_Name"])
 Y["RGroup"]=Y["Last_Name"]
Y.drop_duplicates(inplace=True)
 Y.set_index("Last_Name",inplace=True)
 related_people["RGroup"]=""
related_people.set_index("Last_Name",inplace=True)
 related_people.update(Y)
 related_people.reset_index(inplace=True)
 related_people.loc[related_people["Ticket"]=='113760',"RGroup"]="Carter_1"
related_people.loc[related_people["Ticket"]=='224252',"RGroup"]="Carter_2" 
<prepare_x_and_y>
pred = model.predict(X_test )
Y=pd.DataFrame(related_people.loc[(related_people["SibSp"]==0)&(related_people["Parch"]==0)][["Last_Name","Ticket"]])
 Y["RGroup"]=Y["Last_Name"]+'_'+Y["Ticket"]
Y.drop_duplicates(inplace=True)
 Y.set_index(["Last_Name","Ticket"],inplace=True)
 related_people.set_index(["Last_Name","Ticket"],inplace=True)
 related_people.update(Y)
 related_people.reset_index(inplace=True )<prepare_x_and_y>
test_new['winPlacePercPred'] = pred
print('Correcting predictions')
 test_new['prediction_mod'] = -1.0
matchId = test_new['matchId'].unique()
for match in matchId:
df_match = test_new[test_new['matchId']==match]
df_max = df_match.groupby(['groupId'] ).max()
pred_sort = sorted(df_max['winPlacePercPred'])
 for i in df_max.index:
groupPlace = pred_sort.index(df_max.loc[i]['winPlacePercPred'])
 if len(pred_sort)> 1:
df_max.at[i,'prediction_mod'] = groupPlace/(len(pred_sort)-1)
 else:
df_max.at[i,'prediction_mod'] = 1.0
for i in df_match.index:
test_new.at[i, 'prediction_mod'] = df_max['prediction_mod'].loc[df_match['groupId'].loc[i]]
y_submit_cor = test_new['prediction_mod']
print('Submission scores corrected')
 test_new.head()
<categorify><EOS> 
df_test = import_data('.. /input/sample_submission_V2.csv')
 df_test['winPlacePerc'] = y_submit_cor
df_test['Id'] = df_test['Id'].astype(str)
 submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False)
 print('Submission file made
' )