Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<feature_engineering>
train_data_df = pd.read_csv('.. /input/train.csv')
 test_data_df = pd.read_csv('.. /input/test.csv' )
full_set['HasCabin'] = full_set['Cabin'].apply(lambda x: 0 if isinstance(x, float)else 1 )<data_type_conversions>
train_data_df = reduce_mem_usage(train_data_df)
 test_data_df = reduce_mem_usage(test_data_df )
full_set['Embarked'][full_set['Embarked'].isnull() ] = 'S'
full_set['Embarked'] = full_set['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int )<sort_values>
mean_match_features = train_data_df.groupby(['matchId'])[train_data_df.columns[3:-1]].agg('mean' ).reset_index().loc[:, 'assists':'winPoints']
size_match_features = pd.DataFrame(train_data_df.groupby(['matchId'])[train_data_df.columns[3]].agg('size' ).reset_index() [train_data_df.columns[3]])
 size_match_features.columns = ['match_size']
full_set[ full_set['Fare'].isnull() ]<feature_engineering>
mean_group_features = train_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('mean' ).reset_index().loc[:, 'assists':'winPoints']
mean_group_rank_features = train_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('mean' ).groupby(['matchId'])[train_data_df.columns[3:-1]].rank(pct=True ).reset_index()
mean_group_features = mean_group_features.join(mean_group_rank_features, rsuffix='_rank')
 max_group_features = train_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('max' ).reset_index().loc[:, 'assists':'winPoints']
max_group_rank_features = train_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('max' ).groupby(['matchId'])[train_data_df.columns[3:-1]].rank(pct=True ).reset_index()
max_group_features = max_group_features.join(max_group_rank_features, rsuffix='_rank')
 min_group_features = train_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('min' ).reset_index().loc[:, 'assists':'winPoints']
min_group_rank_features = train_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('min' ).groupby(['matchId'])[train_data_df.columns[3:-1]].rank(pct=True ).reset_index()
min_group_features = min_group_features.join(min_group_rank_features, rsuffix='_rank')
 size_group_features = pd.DataFrame(train_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3]].agg('size' ).reset_index() [train_data_df.columns[3]])
 size_group_features.columns = ['group_size']
full_set['Fare'][ full_set['Fare'].isnull() ] = 0 .<feature_engineering>
features_three = mean_group_features.join(max_group_features, lsuffix='_group_mean', rsuffix='_group_max')
 features_four = min_group_features.join(size_group_features, lsuffix='_group_min', rsuffix='_group_size')
 features_2 = features_three.join(features_four)
 features_2['matchId'] = train_data_df.groupby(['matchId', 'groupId'])['matchId'].agg('mean' ).values
features_2 = features_2.fillna(0.0)
 features_2 = features_2.replace([np.inf, -np.inf], 0.0 )
full_set['Title'] = full_set['Name'].apply(get_title)
 print(full_set['Title'].unique() )<categorify>
features = pd.merge(features_2, features_1, on='matchId', how='right' )
commons = ['Mr','Mrs','Miss','Mme','Ms','Mlle']
rares = list(set(full_set['Title'].unique())- set(commons))
full_set['Title'] = full_set['Title'].replace('Ms','Miss')
 full_set['Title'] = full_set['Title'].replace('Mlle','Miss')
 full_set['Title'] = full_set['Title'].replace('Mme','Mrs')
 full_set['Title'][full_set['Title'].isin(rares)] = 'Rare'
full_set['Title'] = full_set['Title'].map({'Mr':0, 'Mrs':1, 'Miss':2, 'Rare':3} )<feature_engineering>
features = features.drop(['matchId'], axis=1)
 features
full_set['FamSize'] = full_set['Parch'] + full_set['SibSp'] + 1 
<categorify>
targets = train_data_df.groupby(['matchId', 'groupId'])['winPlacePerc'].agg('mean' ).reset_index() ['winPlacePerc']
targets
full_set['Sex'] = full_set['Sex'].map({'male':0, 'female':1} )<drop_column>
full_set.drop(['Cabin','Name','Parch','PassengerId','SibSp','Ticket'], axis=1, inplace=True )<categorify>
import sklearn
def fare_bin(fare):
if fare <= 7.8958:
return 0.
 elif 7.8958 < fare <= 14.4542:
return 1.
 elif 14.4542 < fare <= 31.2750:
return 2.
 else:
return 3 .<data_type_conversions>
scaler = MinMaxScaler(feature_range=(-1, 1)).fit(features.values)
full_set['Fare'] = full_set['Fare'].apply(fare_bin ).astype(int )<data_type_conversions>
import ultimate
from ultimate.mlp import MLP
full_set['Pclass'] = full_set['Pclass'].astype('str' )<categorify>
epoch_train = 15
model = MLP(layer_size=[train_data_df.shape[1], 32, 32, 32, 1], regularization=1, output_shrink=0.1, output_range=[0,1], loss_type="hardmse")
 model.train(scaler.transform(features.values), targets.values, iteration_log=20000, rate_init=0.08, rate_decay=0.8, epoch_train=epoch_train, epoch_decay=1 )
full_w_dum = pd.get_dummies(full_set)
 full_w_dum.head()<define_variables>
mean_match_features = test_data_df.groupby(['matchId'])[train_data_df.columns[3:-1]].agg('mean' ).reset_index().loc[:, 'assists':'winPoints']
size_match_features = pd.DataFrame(test_data_df.groupby(['matchId'])[train_data_df.columns[3]].agg('size' ).reset_index() [train_data_df.columns[3]])
 size_match_features.columns = ['match_size']
full_set = full_w_dum<prepare_x_and_y>
mean_group_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('mean' ).reset_index().loc[:, 'assists':'winPoints']
mean_group_rank_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('mean' ).groupby(['matchId'])[train_data_df.columns[3:-1]].rank(pct=True ).reset_index()
mean_group_features = mean_group_features.join(mean_group_rank_features, rsuffix='_rank')
 max_group_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('max' ).reset_index().loc[:, 'assists':'winPoints']
max_group_rank_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('max' ).groupby(['matchId'])[train_data_df.columns[3:-1]].rank(pct=True ).reset_index()
max_group_features = max_group_features.join(max_group_rank_features, rsuffix='_rank')
 min_group_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('min' ).reset_index().loc[:, 'assists':'winPoints']
min_group_rank_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('min' ).groupby(['matchId'])[train_data_df.columns[3:-1]].rank(pct=True ).reset_index()
min_group_features = min_group_features.join(min_group_rank_features, rsuffix='_rank')
 size_group_features = pd.DataFrame(test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3]].agg('size' ).reset_index() [train_data_df.columns[3]])
 size_group_features.columns = ['group_size']
cols = list(set(full_set.columns)- set(['Survived']))
X_train, X_test = full_set[:train_set.shape[0]][cols], full_set[train_set.shape[0]:][cols]
y_train = full_set[:train_set.shape[0]]['Survived']<find_best_model_class>
features_three = mean_group_features.join(max_group_features, lsuffix='_group_mean', rsuffix='_group_max')
 features_four = min_group_features.join(size_group_features, lsuffix='_group_min', rsuffix='_group_size')
 features_2 = features_three.join(features_four)
 features_2['matchId'] = test_data_df.groupby(['matchId', 'groupId'])['matchId'].agg('mean' ).values
features_2 = features_2.fillna(0.0)
 features_2 = features_2.replace([np.inf, -np.inf], 0.0 )
models = [ LogisticRegression, SVC, LinearSVC, RandomForestClassifier, KNeighborsClassifier, XGBClassifier ]
mscores = []
lscores = ['f1','accuracy','recall','roc_auc']
np.random.seed(42)
 for elem in models:
mscores2 = []
model = elem()
for sc in lscores:
scores = cross_val_score(model, X_train, y_train, scoring=sc)
 mscores2.append(np.mean(scores))
mscores.append(mscores2 )<sort_values>
features = pd.merge(features_2, features_1, on='matchId', how='right' )
order = np.argsort(np.mean(np.array(mscores), axis=1))
print(order )<import_modules>
matches = test_data_df.groupby(['matchId', 'groupId'])['matchId'].agg('mean' ).values
from sklearn.model_selection import StratifiedKFold, KFold<find_best_params>
groups = test_data_df.groupby(['matchId', 'groupId'])['groupId'].agg('mean' ).values
results_kfold = []
for K in [5,6,7,8,9,10]:
model = XGBClassifier()
kfold = KFold(n_splits=K, random_state=42)
 res = cross_val_score(model, X_train, y_train, cv=kfold)
 results_kfold.append(( K,res.mean() *100, res.std() *100))<train_model>
features = features.drop(['matchId'], axis=1 )
list(map(lambda x: print('Iteration nº {:2}, with acc.{:.12} and std.dev.{:.12}'.format(*x)) ,results_kfold)) ;<find_best_params>
test_features = features.values
test_features = scaler.transform(test_features )
results_strat_kfold = []
for K in [5,6,7,8,9,10]:
model = XGBClassifier()
kfold = StratifiedKFold(n_splits=K, random_state=42)
 res = cross_val_score(model, X_train, y_train, cv=kfold)
 results_strat_kfold.append(( K,res.mean() *100, res.std() *100))<train_model>
predictions = model.predict(test_features )
list(map(lambda x: print('Iteration nº {:2}, with acc.{:.12} and std.dev.{:.12}'.format(*x)) ,results_strat_kfold)) ;<compute_train_metric>
features['winPlacePercPred'] = predictions
features['matchId'] = matches
features['groupId'] = groups
group_preds = features.groupby(['matchId', 'groupId'])['winPlacePercPred'].agg('mean' ).groupby(['matchId'] ).rank(pct=True )
xg_scores = []
kfold = StratifiedKFold(n_splits=7, random_state=42)
 for lamb in [.05,.1,.2,.3,.4,.5,.6 ]:
for eta in [.2,.19,.17,.15,.13,.11]:
model = XGBClassifier(learning_rate=eta, reg_lambda=lamb)
 res = cross_val_score(model, X_train, y_train, cv=kfold)
 xg_scores.append({'lamb':lamb, 'eta':eta, 'acc':res.mean() } )<sort_values>
test_data_df = test_data_df.sort_values(['matchId', 'groupId'] )
sorted(xg_scores,key=lambda x: x['acc'], reverse=True)[0]<predict_on_test>
dictionary = dict(zip(features['groupId'].values, group_preds.values))
model = XGBClassifier(learning_rate=.17, reg_lambda=.5)
 model.fit(X_train, y_train)
 predicted = model.predict(X_test )<save_to_csv>
new_ranking_preds = []
for i in test_data_df['groupId'].values:
new_ranking_preds.append(dictionary[i])
 test_data_df['winPlacePercPred'] = new_ranking_preds
test_set['Survived'] = predicted.astype(int)
 test_set[['PassengerId','Survived']].to_csv('submission.csv', sep=',', index=False )<install_modules>
predictions = pd.DataFrame(np.transpose(np.array([test_data_df.loc[:, 'Id'], test_data_df['winPlacePercPred']])))
 predictions.columns = ['Id', 'winPlacePerc']
predictions['Id'] = np.int32(predictions['Id'])
 predictions = predictions.sort_values(by=['Id'] )
!pip install pywaffle<import_modules>
maxPlaces = test_data_df.sort_values(by=['Id'])['maxPlace'].values
numGroups = test_data_df.sort_values(by=['Id'])['numGroups'].values
new_predictions = predictions['winPlacePerc'].values
for i in range(0, len(test_data_df)) :
if maxPlaces[i] == 0:
new_predictions[i] = 0.0
if maxPlaces[i] == 1:
new_predictions[i] = 1.0
else:
gap = 1.0 /(maxPlaces[i] - 1.0)
 new_predictions[i] = round(new_predictions[i]/gap)*gap
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import pandas_profiling
import plotly.express as px
import plotly.graph_objects as go
import sklearn.metrics as metrics
import plotly.offline as py
from sklearn.preprocessing import OneHotEncoder, LabelEncoder, StandardScaler
from sklearn.compose import ColumnTransformer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score, roc_curve,auc, confusion_matrix,precision_recall_curve,precision_recall_curve,plot_precision_recall_curve
from pywaffle import Waffle
from yellowbrick.classifier import classification_report
from plotly.subplots import make_subplots<set_options>
predictions['winPlacePerc'] = new_predictions
custom_colors = ["
customPalette = sns.set_palette(sns.color_palette(custom_colors))<set_options>
predictions.to_csv('PUBG_preds.csv', index=False )