Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<count_missing_values>
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df_test.isnull().sum()<drop_column>
df = pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv' )
df_train.drop(columns = ["Name" ,"Ticket" , "Cabin"], inplace=True)
 df_test.drop(columns = ["Name" ,"Ticket" , "Cabin"], inplace=True )<categorify>
df.isnull().sum()
df_train.Sex.replace("male" , 0 , inplace =True)
 df_train.Sex.replace("female" , 1 , inplace =True)
 df_train.Embarked.replace("C" , 0 , inplace =True)
 df_train.Embarked.replace("S" , 1 , inplace =True)
 df_train.Embarked.replace("Q" , 2 , inplace =True )<categorify>
xy=df[df['winPlacePerc']==1]
xy=xy[xy['damageDealt']<1000]
xy
df_test.Sex.replace("male" , 0 , inplace =True)
 df_test.Sex.replace("female" , 1 , inplace =True)
 df_test.Embarked.replace("C" , 0 , inplace =True)
 df_test.Embarked.replace("S" , 1 , inplace =True)
 df_test.Embarked.replace("Q" , 2 , inplace =True )<data_type_conversions>
warnings.filterwarnings('ignore' )
df_train.Age.fillna(df_train.Age.median() , inplace= True)
 df_test.Age.fillna(df_train.Age.median() , inplace = True )<correct_missing_values>
def feature_engineering(is_train=True,debug=True):
test_idx = None
if is_train:
print("processing train.csv")
 if debug == True:
df = pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv', nrows=10000)
 else:
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')
 test_idx = df.Id
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.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 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.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
df_test.Fare.fillna(df_train.Fare.median() , inplace = True )<count_missing_values>
X_train, y_train, train_columns, _ = feature_engineering(True,False)
 X_test, y, _ , test_idx = feature_engineering(False,True )
df_test.isnull().sum()<count_missing_values>
LR_model = LinearRegression(n_jobs=4, normalize=True)
 LR_model.fit(X_train,y_train )
df_train.isnull().sum()<prepare_x_and_y>
LR_model.score(X_train,y_train )
X = df_train.drop(columns = ["Survived"])
 y = df_train["Survived"]<split>
y_pred_train = LR_model.predict(X_train)
 y_pred_test = LR_model.predict(X_test )
train_X, val_X, train_y, val_y = train_test_split(X, y, test_size=0.2, random_state=0 )<train_model>
df_test = reduce_mem_usage(pd.read_csv('.. /input/pubg-finish-placement-prediction/test_V2.csv'))
df_test.shape
<save_to_csv><EOS> 
y_pred_test[y_pred_test>1] = 1
y_pred_test[y_pred_test<0] = 0
df_test['winPlacePerc'] = y_pred_test
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )