Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<set_options>
pd.set_option('display.max_columns', None )
%matplotlib inline
sns.set()
warnings.filterwarnings('ignore' )<load_from_csv>
test_size = 0.05
batch_size = 20000
env = 'kaggle'
if env == 'kaggle':
test_size = 0.0
batch_size = 100000
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv' )<count_missing_values>
class timeit:
def __enter__(self):
self.start = time()
def __exit__(self, type, value, traceback):
print(f'Done in {time() - self.start:.2f} sec' )
train.isnull().sum()<count_missing_values>
def load_data(file):

 print('Loading data...')
 with timeit() :
df = pd.read_csv(f'{path}/{file}')
 print(f'Rows loaded: {len(df.index)}')
 print('
Reducing mem usage...')
 with timeit() :
df = reduce_mem_usage(df)
 return df
test.isnull().sum()<drop_column>
def ds_solo_split(df):

 print('Extracting Duo-Squad data...')
 with timeit() :
df_ds = df[df['matchType'].str.contains('squad')| df['matchType'].str.contains('duo')]
print(f'Duo-Squad: {len(df_ds.index)} rows
')
 print('Extracting Solo data...')
 with timeit() :
df_solo = df[~df.Id.isin(set(df_ds.Id.values.flatten())) ]
print(f'Solo: {len(df_solo.index)} rows')
 return df_ds, df_solo
train.drop(columns=['PassengerId','Name','Ticket','Cabin'], axis=1, inplace=True)
 test.drop(columns=['PassengerId','Name','Ticket','Cabin'], axis=1, inplace=True )<feature_engineering>
def train_test_split_by_match(df, test_size):

 print('Splitting dataframe to train and test...')
 with timeit() :
train_matches, test_matches = train_test_split(df.matchId.unique() , test_size=test_size)
 df_train = df[df.matchId.isin(train_matches)]
df_test = df[df.matchId.isin(test_matches)]
print(f'Splitted: train={len(df_train.index)}, test={len(df_test.index)}')
 return df_train, df_test
train['Sex'] = [1 if gender=='male' else 0 for gender in train['Sex']]
test['Sex'] = [1 if gender=='male' else 0 for gender in test['Sex']]<data_type_conversions>
def finalize(df, has_y=True):

 df = df.drop([
'Id',
'matchId',
'groupId',
'matchType',
], axis=1)
 df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
df = df.fillna(0)
 if not has_y:
return df
X = df.drop('winPlacePerc', axis=1)
 y = df[['winPlacePerc']].values.ravel()
return X, y
train.Age.fillna(train.Age.median() , inplace=True)
 test.Age.fillna(test.Age.median() , inplace=True )<drop_column>
def fit(model, *args, **kwargs):
if env == 'kaggle':
kwargs.pop('validation_data', None)
 model.fit(*args, **kwargs )
train.drop(columns=['Age'], inplace=True)
 test.drop(columns=['Age'], inplace=True )<data_type_conversions>
def postprocess(df, y_pred):

 df = df.copy()
df['winPlacePercPred'] = y_pred
df['meanWinPlacePred'] = df.groupby('groupId' ).winPlacePercPred.transform(np.mean)
 df['placement'] = df.groupby('matchId' ).meanWinPlacePred.rank(method='dense', ascending=False)
 y_final =(df.numGroups - df.placement)/(df.numGroups - 1)
 y_final[y_final.isna() ] = 0.0
return y_final.values
test.Fare.fillna(test.Fare.median() , inplace=True )<drop_column>
df = load_data('train_V2.csv' )
train.drop(columns=['Fare'], axis=1, inplace=True)
 test.drop(columns=['Fare'], axis=1, inplace=True )<data_type_conversions>
df_ds, df_solo = ds_solo_split(df )
train.Embarked.fillna(train.Embarked.mode() , inplace=True )<categorify>
env = 'kaggle'
train = pd.get_dummies(train, columns=['Pclass','AgeGroup','Embarked'])
 test = pd.get_dummies(test, columns=['Pclass','AgeGroup','Embarked'] )<drop_column>
df_ds_train, df_ds_test = train_test_split_by_match(df_ds, test_size=test_size )
predictors = train.drop(columns=['Survived'], axis=1 )<prepare_x_and_y>
print('Processing train Duo-Squad dataset...')
 with timeit() :
X_ds_train, y_ds_train = process_ds(df_ds_train)
 print('
Processing test Duo-Squad dataset...')
 with timeit() :
X_ds_test, y_ds_test = process_ds(df_ds_test )
target = train[['Survived']]<import_modules>
scaler_ds = StandardScaler()
print('Fitting Duo-Squad standard scaling...')
 with timeit() :
scaler_ds.fit(X_ds_train )
from sklearn.model_selection import train_test_split<import_modules>
def get_model(input_shape):
model = keras.models.Sequential()
model.add(L.InputLayer(input_shape=input_shape))
model.add(L.Dense(64, activation='relu'))
model.add(L.BatchNormalization())
 model.add(L.Dense(32, activation='relu'))
model.add(L.BatchNormalization())
 model.add(L.Dense(16, activation='relu'))
model.add(L.Dense(1, activation='sigmoid', kernel_initializer='normal'))
model.compile(loss='mean_absolute_error', optimizer='adam')
 return model
from sklearn.model_selection import train_test_split<split>
model_ds = get_model([len(X_ds_train.columns)] )
x_train,x_val,y_train,y_val = train_test_split(predictors,target,test_size=0.2,random_state=123 )<import_modules>
validation_data = []
if env != 'kaggle':
validation_data = [
scaler_ds.transform(X_ds_test),
y_ds_test
]
fit(
 model_ds,
scaler_ds.transform(X_ds_train),
y_ds_train,
epochs=100,
validation_data=validation_data,
batch_size=batch_size,
callbacks=[
EarlyStopping('loss', min_delta=0.0001, patience=20)
 ]
)
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.svm import SVC<import_modules>
if env != 'kaggle':
y_ds_pred = model_ds.predict(scaler_ds.transform(X_ds_test))
y_ds_pred_post = postprocess(df_ds_test, y_ds_pred)
 print('MAE DS:', mean_absolute_error(y_ds_test, y_ds_pred_post))
from sklearn.metrics import accuracy_score<compute_train_metric>
df_solo_train, df_solo_test = train_test_split_by_match(df_solo, test_size=test_size )
lr = LogisticRegression()
lr.fit(x_train,y_train)
 preds = lr.predict(x_val)
 lr_accuracy = accuracy_score(y_val,preds)
 print(f'Logistic Regression accuracy: {lr_accuracy*100}' )<compute_train_metric>
print('Processing train Solo dataset...')
 with timeit() :
X_solo_train, y_solo_train = process_solo(df_solo_train)
 print('
Processing train Solo dataset...')
 with timeit() :
X_solo_test, y_solo_test = process_solo(df_solo_test )
knn = KNeighborsClassifier()
knn.fit(x_train,y_train)
 preds = knn.predict(x_val)
 knn_accuracy = accuracy_score(y_val, preds)
 print(f'KNN accuracy: {knn_accuracy*100}' )<compute_train_metric>
scaler_solo = StandardScaler()
scaler_solo.fit(X_solo_train )
dt = DecisionTreeClassifier()
dt.fit(x_train,y_train)
 preds = dt.predict(x_val)
 dt_accuracy = accuracy_score(y_val, preds)
 print(f'Decission Tree accuracy: {dt_accuracy*100}' )<compute_train_metric>
model_solo = get_model([len(X_solo_train.columns)] )
rf = RandomForestClassifier()
rf.fit(x_train,y_train)
 preds = rf.predict(x_val)
 rf_accuracy = accuracy_score(y_val, preds)
 print(f'RandomForest accuracy: {rf_accuracy*100}' )<compute_train_metric>
validation_data = []
if env != 'kaggle':
validation_data = [
scaler_solo.transform(X_solo_test),
y_solo_test
]
fit(
 model_solo,
scaler_solo.transform(X_solo_train),
y_solo_train,
epochs=200,
validation_data=validation_data,
batch_size=batch_size,
callbacks=[
EarlyStopping('loss', min_delta=0.0001, patience=20)
 ]
)
gbc = GradientBoostingClassifier()
gbc.fit(x_train,y_train)
 preds = gbc.predict(x_val)
 gbc_accuracy = accuracy_score(y_val, preds)
 print(f'GradientBoostClassifier accuracy: {gbc_accuracy*100}' )<compute_train_metric>
if env != 'kaggle':
y_solo_pred = model_solo.predict(scaler_solo.transform(X_solo_test))
y_solo_pred_post = postprocess(df_solo_test, y_solo_pred)
 print('MAE SOLO:', mean_absolute_error(y_solo_test, y_solo_pred_post))
svc = SVC()
svc.fit(x_train,y_train)
 preds = svc.predict(x_val)
 svc_accuracy = accuracy_score(y_val, preds)
 print(f'SVC accuracy: {svc_accuracy*100}' )<create_dataframe>
if env != 'kaggle':
y_all_pred = np.hstack([y_ds_pred_post.flatten() , y_solo_pred_post.flatten() ])
 y_all_test = np.hstack([y_ds_test, y_solo_test])
 print('MAE ALL:', mean_absolute_error(y_all_test, y_all_pred))
models = pd.DataFrame({'Model':['LogisticRegression','KNN','DecissionTree','RandomForest','GradientBoostClassifier','SVM'],
'Accuracy':[lr_accuracy*100,knn_accuracy*100,dt_accuracy*100,rf_accuracy*100,gbc_accuracy*100,svc_accuracy*100]})
 models<load_from_csv>
df_final = load_data('test_V2.csv')
 print()
df_final_ds, df_final_solo = ds_solo_split(df_final )
data = pd.read_csv('/kaggle/input/titanic/test.csv')
 ids = data['PassengerId']<predict_on_test>
X_final_ds = process_ds(df_final_ds, False)
 y_final_ds_pred = model_ds.predict(scaler_ds.transform(X_final_ds))
df_final_ds_pred = df_final_ds.assign(winPlacePerc=postprocess(df_final_ds, y_final_ds_pred))
preds = dt.predict(test )<create_dataframe>
X_final_solo = process_solo(df_final_solo, False)
 y_final_solo_pred = model_solo.predict(scaler_solo.transform(X_final_solo))
df_final_solo_pred = df_final_solo.assign(winPlacePerc=postprocess(df_final_solo, y_final_solo_pred))
output = pd.DataFrame({'PassengerId':ids, 'Survived':preds} )<save_to_csv>
df_final_all = df_final_ds_pred[['Id', 'winPlacePerc']].append(df_final_solo_pred[['Id', 'winPlacePerc']])
 df_result =(df_final[['Id']].set_index('Id' ).join(df_final_all.set_index('Id'), on='Id'))
<set_options><EOS> 
df_result.to_csv('submission.csv' )