Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
df_data["Last_Name"] = df_data['Name'].apply(lambda x: str.split(x, ",")[0])
 DEFAULT_SURVIVAL_VALUE = 0.5
df_data["Family_Survival"] = DEFAULT_SURVIVAL_VALUE
for grp, grp_df in df_data[['Survived','Name', 'Last_Name', 'Fare', 'Ticket', 'PassengerId',
'SibSp', 'Parch', 'Age', 'Cabin']].groupby(['Last_Name', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
df_data.loc[df_data['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
df_data.loc[df_data['PassengerId'] == passID, 'Family_Survival'] = 0
print("Number of passengers with family survival information:",
df_data.loc[df_data['Family_Survival']!=0.5].shape[0])
 for _, grp_df in df_data.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['Family_Survival'] == 0)|(row['Family_Survival']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
df_data.loc[df_data['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
df_data.loc[df_data['PassengerId'] == passID, 'Family_Survival'] = 0
print("Number of passenger with family/group survival information: "
+str(df_data[df_data['Family_Survival']!=0.5].shape[0]))
df_train["Family_Survival"] = df_data['Family_Survival'][:891]
df_test["Family_Survival"] = df_data['Family_Survival'][891:]<data_type_conversions>
%%time
def preProcess(is_train=True,debug=True):
test_idx = None
if is_train:
print("processing train.csv")
 if debug == True:
df = pd.read_csv('.. /input/train_V2.csv', nrows=10000)
 else:
df = pd.read_csv('.. /input/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 df = pd.read_csv('.. /input/test_V2.csv')
 test_idx = df.Id
df['totalDistance'] = df['rideDistance'] + df['walkDistance'] + df['swimDistance']
df['kills_assists'] =(df['kills'] + df['assists'])
 df['killsWithoutMoving'] =(( df['kills'] > 0)&(df['totalDistance'] == 0))
df['healthitems'] = df['heals'] + df['boosts']
df = reduce_mem_usage(df)
 print("remove some columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 y = None
if is_train:
print("get target")
 y = np.array(df[target])
 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()
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 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'])
 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)
 print("get match Type feature")

df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = df_out
feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y, feature_names, test_idx
x_train, y_train, train_columns, _ = preProcess(True,False)
 x_test, _, _ , test_idx = preProcess(False,True)
df_data["HadCabin"] =(df_data["Cabin"].notnull().astype('int'))
df_train["HadCabin"] = df_data["HadCabin"][:891]
df_test["HadCabin"] = df_data["HadCabin"][891:]
print('HasCabin feature created' )<categorify>
random_seed=1
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state=random_seed)
df_data["Deck"] = df_data.Cabin.str.extract('([A-Za-z])', expand=False)
 deck_mapping = {"0":0,"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}
df_data['Deck'] = df_data['Deck'].map(deck_mapping)
 df_data["Deck"] = df_data["Deck"].fillna("0")
 df_data["Deck"]=df_data["Deck"].astype('int')
 df_train["Deck"] = df_data['Deck'][:891]
df_test["Deck"] = df_data['Deck'][891:]
print('Deck feature created')
 dummies=pd.get_dummies(df_train[['Deck']], prefix_sep='_')
 df_train = pd.concat([df_train, dummies], axis=1)
 dummies=pd.get_dummies(df_test[['Deck']], prefix_sep='_')
 df_test = pd.concat([df_test,dummies], axis=1)
 print('Deck Categories created' )<categorify>
model = RandomForestRegressor(n_estimators=20, min_samples_leaf=5, max_features=0.5,
n_jobs=-1,verbose=2)
( df_train['SibSp'])=(df_train['SibSp'] ).astype('category')
 dummies=pd.get_dummies(df_train[['SibSp']], prefix_sep='_')
 df_train = pd.concat([df_train, dummies], axis=1)
(df_test['SibSp'])=(df_test['SibSp'] ).astype('category')
 dummies=pd.get_dummies(df_test[['SibSp']], prefix_sep='_')
 df_test = pd.concat([df_test,dummies], axis=1)
 print('Sibsp Categories created' )<categorify>
%%time
model.fit(x_train, y_train)
( df_train['Parch'])=(df_train['Parch'] ).astype('category')
 dummies=pd.get_dummies(df_train[['Parch']], prefix_sep='_')
 df_train = pd.concat([df_train, dummies], axis=1)
(df_test['Parch'])=(df_test['Parch'] ).astype('category')
 dummies=pd.get_dummies(df_test[['Parch']], prefix_sep='_')
 df_test = pd.concat([df_test,dummies], axis=1)
 print('Parch Categories created' )<drop_column>
y_train_pred = model.predict(x_train)
 y_val_pred = model.predict(x_val)
 print('mae train: ', mean_absolute_error(y_train_pred, y_train))
print('mae validation: ', mean_absolute_error(y_val_pred, y_val))
<define_variables><EOS> 
df_test = pd.read_csv('.. /input/' + 'test_V2.csv')
 pred = model.predict(x_test)
 df_test['winPlacePerc'] = pred
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission_rf_feat1.csv', index=False )