Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
train_set_known_age = train_set_age[~np.isnan(train_set_age['Age'])]
train_set_known_age = train_set_known_age[(( train_set_known_age['Pclass']==3)& 
(train_set_known_age['Is_alone']==1)) |(train_set_known_age['Pclass']==3)]
print("Percentage of single and/or third class passengers with a known age: ",
round(len(train_set_known_age.index)*100/len(train_set.index), 2), "%", sep="" )<import_modules>
def rank_by_team(df):
cols_to_drop = ['Id', 'groupId', 'matchId', 'winPlacePerc', "matchType"]
features = [col for col in df.columns if col not in cols_to_drop]
agg = df.groupby(['matchId', 'groupId'])[features].mean()
agg = agg.groupby('matchId')[features].rank(pct=True)
 return df.merge(agg, suffixes=['', '_mean_rank'], how='left', on=['matchId', 'groupId'])
 df = pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv')
 df = df.dropna()
df = rank_by_team(df)
 cols_to_drop = ['Id', 'groupId', 'matchId', 'winPlacePerc', "matchType"]
features = [col for col in df.columns if col not in cols_to_drop]
X = df.loc[:, features]
Y = df.loc[:, "winPlacePerc"]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=1)
 X_train = X
Y_train = Y
model = LinearRegression()
model.fit(X_train, Y_train)
 test_data = pd.read_csv(".. /input/pubg-finish-placement-prediction/test_V2.csv")
 test_data = rank_by_team(test_data)
 X = test_data.loc[:, features]
pred = model.predict(X)
 id = pd.read_csv(".. /input/pubg-finish-placement-prediction/sample_submission_V2.csv")
 id = id.loc[:, "Id"]
df = pd.DataFrame({"Id": id, "winPlacePerc": pred})
 df.to_csv("submission.csv", index=False, sep=',')
<categorify><EOS> 
df_sub = pd.read_csv("submission.csv")
 df_test = pd.read_csv(".. /input/pubg-finish-placement-prediction/test_V2.csv")
 df_sub = df_sub.merge(df_test[["Id", "matchId", "groupId", "maxPlace", "numGroups"]], on="Id", how="left")
 df_sub_group = df_sub.groupby(["matchId", "groupId"] ).first().reset_index()
df_sub_group["rank"] = df_sub_group.groupby(["matchId"])["winPlacePerc"].rank()
df_sub_group = df_sub_group.merge(
 df_sub_group.groupby("matchId")["rank"].max().to_frame("max_rank" ).reset_index() ,
on="matchId", how="left")
 df_sub_group["adjusted_perc"] =(df_sub_group["rank"] - 1)/(df_sub_group["numGroups"] - 1)
 df_sub = df_sub.merge(df_sub_group[["adjusted_perc", "matchId", "groupId"]], on=["matchId", "groupId"], how="left")
 df_sub["winPlacePerc"] = df_sub["adjusted_perc"]
df_sub.loc[df_sub.maxPlace == 0, "winPlacePerc"] = 0
df_sub.loc[df_sub.maxPlace == 1, "winPlacePerc"] = 1
subset = df_sub.loc[df_sub.maxPlace > 1]
gap = 1.0 /(subset.maxPlace.values - 1)
 new_perc = np.around(subset.winPlacePerc.values / gap)* gap
df_sub.loc[df_sub.maxPlace > 1, "winPlacePerc"] = new_perc
df_sub.loc[(df_sub.maxPlace > 1)&(df_sub.numGroups == 1), "winPlacePerc"] = 0
assert df_sub["winPlacePerc"].isnull().sum() == 0
df_sub[["Id", "winPlacePerc"]].to_csv("submission.csv", index=False)