Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<set_options>
random.seed(42)
 warnings.filterwarnings("ignore", category=DeprecationWarning)
 def reload_train() :
gc.collect()
df = pd.read_csv('.. /input/train_V2.csv')
 invalid_match_ids = df[df['winPlacePerc'].isna() ]['matchId'].values
df = df[-df['matchId'].isin(invalid_match_ids)]
return df
def reload_test() :
gc.collect()
df = pd.read_csv('.. /input/test_V2.csv')
 return df
sns.set()<load_from_csv>
def run_train(preprocess):
df = reload_train()
df.drop(columns=['matchType'], inplace=True)
 df = preprocess(df)
 target = 'winPlacePerc'
cols_to_drop = ['Id', 'groupId', 'matchId', target]
cols_to_fit = [col for col in df.columns if col not in cols_to_drop]
df = SelectKBest(chi2, k=30 ).fit_transform(df[cols_to_fit], df[target])
 col = df.columns
print(df.columns)
 model = XGBRegressor()
model.fit(df[cols_to_fit], df[target],verbose=False)
 return model,col
def run_test(preprocess):
df = reload_test()
df.drop(columns=['matchType'], inplace=True)
 df = preprocess(df)
 print(df.columns)
 return df
train_data = pd.read_csv(".. /input/train.csv")
 test_data = pd.read_csv(".. /input/test.csv")
 sample_submission = pd.read_csv(".. /input/sample_submission.csv" )<prepare_x_and_y>
def rank_by_team(df):
cols_to_drop = ['Id', 'groupId', 'matchId', 'winPlacePerc']
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'] )
X_train = train_data.drop("label", axis=1)
 y_train = train_data[["label"]]
X_test = test_data.copy()<categorify>
model,col = run_train(rank_by_team )
X_train = X_train.astype("float32")/ 255
X_test = X_test.astype("float32")/ 255
X_train = X_train.values.reshape(( len(X_train), 28, 28, 1))
X_test = X_test.values.reshape(( len(X_test), 28, 28, 1))
y_train = pd.get_dummies(y_train, columns=["label"] )<choose_model_class>
test = run_test(rank_by_team )
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), padding="valid", input_shape=X_train.shape[1:]))
model.add(Activation("relu"))
model.add(Conv2D(64, kernel_size=(3, 3), padding="valid"))
model.add(Activation("relu"))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(64, kernel_size=(3, 3), padding="valid"))
model.add(Activation("relu"))
model.add(Conv2D(128, kernel_size=(3, 3), padding="valid"))
model.add(Activation("relu"))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(128))
model.add(Activation("relu"))
model.add(Dropout(0.4))
model.add(Dense(10))
model.add(Activation("softmax"))
model.compile(optimizer="adadelta", loss="categorical_crossentropy", metrics=["accuracy"])
 model.summary()<train_model>
pred = model.predict(test)
 pred.shape
epochs = 50
batch_size = 256
model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, validation_split=0.2 )<predict_on_test>
pred_df = pd.DataFrame({'Id' : test_id, 'winPlacePerc' : pred})
 pred_df.to_csv("submission.csv", index=False )