Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<import_modules>
pd.set_option('display.max_columns', None )
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.preprocessing import StandardScaler
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.preprocessing import OneHotEncoder
from sklearn.linear_model import SGDClassifier
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score<categorify>
train = pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv')
 test = pd.read_csv('.. /input/pubg-finish-placement-prediction/test_V2.csv' )
class CabinExtractor(BaseEstimator, TransformerMixin):

 def __init__(self):
pass
def fit(self, X, y=None):
return self
def __get_deck(self, cabin)-> int:
return 0 if cabin == 0 else 1
def transform(self, X, y=None):
X.Cabin.fillna(0, inplace=True)
 X.Cabin = X.Cabin.apply(self.__get_deck)
 return X<categorify>
train[train['winPlacePerc'].isnull() ]
class TitleExtractor(BaseEstimator, TransformerMixin):

 def __init__(self, regex: str):
self.regex = regex
pass
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X['Title'] = X.Name.str.extract(self.regex)[0].replace(['Lady', 'the Countess','Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 return X.drop('Name', axis=1 )<prepare_x_and_y>
train.drop(2744604, inplace=True )
class DataFrameSelector(BaseEstimator, TransformerMixin):

 def __init__(self, column_names):
self.column_names = column_names
pass
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
return X[self.column_names]<prepare_x_and_y>
train['totalDistance'] = train['rideDistance'] + train['walkDistance'] + train['swimDistance']
train['killsWithoutMoving'] =(( train['kills'] > 0)&(train['totalDistance'] == 0))
class DataFrameToValueConverter(BaseEstimator, TransformerMixin):

 def __init__(self):
pass
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
return X.values<prepare_x_and_y>
train['headshot_rate'] = train['headshotKills'] / train['kills']
train['headshot_rate'] = train['headshot_rate'].fillna(0 )
class FamilyAttributeCombiner(BaseEstimator, TransformerMixin):

 def __init__(self):
pass
def fit(self, X, y=None):
return self
def get_val(self, amount):
if amount > 0:
return 1
else:
return 0
def transform(self, X, y=None):
X['HasFamily'] = X[['SibSp','Parch']].sum(axis=1 ).apply(self.get_val)
 return X.drop(['SibSp','Parch'], axis=1 )<train_on_grid>
train.drop(train[train['killsWithoutMoving'] == True].index, inplace=True)
 train.drop('killsWithoutMoving', axis=1, inplace=True )
class GridSearchHelper() :
def __init__(self, preprocesing_pipeline: Pipeline, X: pd.DataFrame, y: pd.DataFrame):
self.preprocesing_pipeline = preprocesing_pipeline
self.X = X
self.y = y
pass
def __get_grid_search(self, clf, param_grid: list, kwargs)-> GridSearchCV:
return GridSearchCV(clf, param_grid, **kwargs)
 def __get_best_estimator(self, clf, param_grid: list, kwargs)-> BaseEstimator:
grid_search = self.__get_grid_search(clf, param_grid, kwargs)
 grid_search.fit(self.preprocesing_pipeline.fit_transform(self.X), self.y)
 return grid_search.best_estimator_
def get_best_classifier(self, clf, param_grid, **kwargs):
return self.__get_best_estimator(clf, param_grid, kwargs )<define_variables>
train[train['roadKills'] >= 10][['Id', 'kills', 'roadKills', 'rideDistance', 'walkDistance']]
not_needed_attribs = ['PassengerId', 'Name', 'Ticket','Cabin', 'Embarked']
cat_attribs = ['Sex', 'Pclass']
num_attribs = ['SibSp', 'Parch', 'Age', 'Fare']<define_variables>
train.drop(train[train['roadKills'] >= 10].index, inplace=True )
title_regex = '([A-Za-z]+)\.'<create_dataframe>
train[(train['kills'] > 30)&(train['totalDistance'] < 2000)][['kills','totalDistance']]
num_pipeline = Pipeline([ 
('selector', DataFrameSelector(num_attribs)) , 
('df_to_value_converter',DataFrameToValueConverter()), 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('std_scaler', StandardScaler()),
] )<categorify>
train.drop(train[(train['kills'] > 30)&(train['totalDistance'] < 2000)].index, inplace=True)
 train.drop('totalDistance', axis=1, inplace=True )
cat_pipeline = Pipeline([ 
('selector', DataFrameSelector(cat_attribs)) , 
('df_to_value_converter',DataFrameToValueConverter()), 
('one_hot_encoder', OneHotEncoder()),
] )<concatenate>
train.drop(train[(train['headshot_rate'] == 1)&(train['kills'] >= 10)].index, inplace=True )
feature_union = FeatureUnion(transformer_list=[ 
('num_pipeline', num_pipeline), 
('cat_pipeline', cat_pipeline),
] )<create_dataframe>
train.drop(train[train['longestKill'] >= 1000].index, inplace=True )
preprocessing_pipeline = Pipeline([ 
('selector', DataFrameSelector(num_attribs + cat_attribs)) , 
('feature_union', feature_union),
] )<prepare_x_and_y>
v = 22.5 / 3.6
X_train, y_train = dataHelper.get_train_data(['Survived'])
 X_test = dataHelper.get_test_data()<train_on_grid>
train[train['walkDistance'] >= v * train['matchDuration']]
preprocessing_pipeline.fit(X_train)
 gridSearchHelper = GridSearchHelper(preprocessing_pipeline, X_train, y_train.values.ravel() )<compute_train_metric>
train.drop(train[train['walkDistance'] >= v * train['matchDuration']].index, inplace=True )
def get_estimator_and_print_score(clf, param_grid):
X_prepared = preprocessing_pipeline.transform(X_train)
 estimator = gridSearchHelper.get_best_classifier(clf, param_grid, cv=2, scoring='accuracy', refit=True)
 estimator.fit(X_prepared, y_train.values.ravel())
 scores = cross_val_score(estimator, X_prepared, y_train.values.ravel() , cv=2, scoring='accuracy')
 print(type(clf ).__name__)
 print('Crossval:', scores.mean())
 print('Accuracy:',accuracy_score(y_train, estimator.predict(X_prepared)))
 print('F1-score:', f1_score(y_train, estimator.predict(X_prepared)))
 return estimator<define_search_space>
v = 100 / 3.6
train[train['rideDistance'] >= v * train['matchDuration']]
svc_param_grid = [
{
'C':[0.9,1.0,1.1],
'gamma':[1]
},
]<define_search_space>
train.drop(train[train['rideDistance'] >= 20000].index, inplace=True )
rfc_param_grid = [
{
'max_depth' : [10], 'max_features' : ['log2'], 'max_leaf_nodes' : [None],
'min_impurity_decrease' : [0.0], 'min_impurity_split' : [None],
'min_samples_leaf' : [1], 'min_samples_split' : [30],
'min_weight_fraction_leaf' : [0.0], 'n_estimators' : [100], 'n_jobs' : [1],
'oob_score' : [False], 'random_state' : [None], 'verbose' : [0], 'warm_start' : [False]
}
]<define_search_space>
train[train['swimDistance'] >= 2000]
dtc_param_grid = [
{
'max_depth': [4, 5],
}
]<define_search_space>
train.drop(train[train['swimDistance'] >= 2000].index, inplace=True )
sgdc_param_grid = [
{
'shuffle': [True],
'max_iter':[np.ceil(10**6 / len(X_train)) ,],
},
]<define_search_space>
train.drop(train[train['weaponsAcquired'] >= 50].index, inplace=True )
knc_param_grid = [
{
'n_neighbors':[2,3,4,5,7],
'weights':['uniform','distance'],
},
{
}
]<choose_model_class>
train.drop(train[train['heals'] >= 40].index, inplace=True )
final_pipeline = Pipeline([ 
('preprocessing', preprocessing_pipeline), 
('estimator', current_estimator)
 ] )<train_model>
train['killsNorm'] = train['kills']*(( 100-train['playersJoined'])/100 + 1)
 train['damageDealtNorm'] = train['damageDealt']*(( 100-train['playersJoined'])/100 + 1)
 train['maxPlaceNorm'] = train['maxPlace']*(( 100-train['playersJoined'])/100 + 1)
 train['matchDurationNorm'] = train['matchDuration']*(( 100-train['playersJoined'])/100 + 1)
 to_show = ['Id', 'kills','killsNorm','damageDealt', 'damageDealtNorm', 'maxPlace', 'maxPlaceNorm', 'matchDuration', 'matchDurationNorm', 'playersJoined']
train[to_show][0:11]
final_pipeline.fit(X_train, y_train.values.ravel() )<predict_on_test>
train['groupId'] = train['groupId'].astype('category')
 train['matchId'] = train['matchId'].astype('category')
 train['groupId_cat'] = train['groupId'].cat.codes
train['matchId_cat'] = train['matchId'].cat.codes
train.drop(columns=['groupId', 'matchId'], inplace=True)
 train[['groupId_cat', 'matchId_cat']].head()
dataHelper.save_prediction(X_test['PassengerId'], final_pipeline.predict(X_test))<import_modules>
train.drop(columns = ['Id'], inplace=True )
print(os.listdir(".. /input")) 
<load_from_csv>
from sklearn.preprocessing import LabelEncoder
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<count_values>
label_encoder = LabelEncoder()
mapped_education = pd.Series(label_encoder.fit_transform(train['matchType']))
mapped_education.value_counts().plot.barh()
print(dict(enumerate(label_encoder.classes_)) )
Y_train = train["label"]
X_train = train.drop(labels="label",axis=1)
 Y_train.value_counts()<feature_engineering>
categorical_columns = train.columns[train.dtypes == 'object'].union(['matchType'])
 for column in categorical_columns:
train[column] = label_encoder.fit_transform(train[column])
 train.head(10 )
X_train = X_train/255.0
test = test/255.0<train_model>
target = 'winPlacePerc'
features = list(train.columns)
 y_train = np.array(train[target])
 features.remove(target)
 x_train = train[features]
print(x_train.shape,y_train.shape )
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )<categorify>
test['headshot_rate'] = test['headshotKills'] / test['kills']
test['headshot_rate'] = test['headshot_rate'].fillna(0)
 test['playersJoined'] = test.groupby('matchId')['matchId'].transform('count')
 test['killsNorm'] = test['kills']*(( 100-test['playersJoined'])/100 + 1)
 test['damageDealtNorm'] = test['damageDealt']*(( 100-test['playersJoined'])/100 + 1)
 test['maxPlaceNorm'] = test['maxPlace']*(( 100-train['playersJoined'])/100 + 1)
 test['matchDurationNorm'] = test['matchDuration']*(( 100-test['playersJoined'])/100 + 1)
 test['healsandboosts'] = test['heals'] + test['boosts']
test['groupId'] = test['groupId'].astype('category')
 test['matchId'] = test['matchId'].astype('category')
 test['groupId_cat'] = test['groupId'].cat.codes
test['matchId_cat'] = test['matchId'].cat.codes
categorical_columns = test.columns[test.dtypes == 'object'].union(['matchType'])
 for column in categorical_columns:
test[column] = label_encoder.fit_transform(test[column])
 x_test = test[features].copy()
x_test.fillna(0, inplace=True)
 print(x_test.shape )
Y_train = to_categorical(Y_train,num_classes=10 )<split>
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state = 1 )
X_train, X_test, Y_train, Y_test = train_test_split(X_train, Y_train, test_size = 0.05 )<define_search_model>
rf = RandomForestRegressor(n_estimators = 80, min_samples_leaf = 3, max_depth = 26, max_features = 0.5,
n_jobs = -1 )
def conv_layer(x,concat_axis,nb_filter,dropout_rate=None,weight_decay=1E-4):
x = BatchNormalization(axis=concat_axis,
gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(x)
 x = Activation('relu' )(x)
 x = Conv2D(nb_filter,(3,3),padding='same',kernel_regularizer=l2(weight_decay),use_bias=False )(x)
 if dropout_rate:
x = Dropout(dropout_rate )(x)
 return x
def transition_layer(x,concat_axis,nb_filter,dropout_rate=None,weight_decay=1E-4):
x = BatchNormalization(axis=concat_axis,
gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(x)
 x = Activation('relu' )(x)
 x = Conv2D(nb_filter,(1,1),padding='same',kernel_regularizer=l2(weight_decay),use_bias=False )(x)
 if dropout_rate:
x = Dropout(dropout_rate )(x)
 x = AveragePooling2D(( 2,2),strides=(2,2))(x)
 return x
def denseblock(x,concat_axis,nb_filter,nb_layers,growth_rate,dropout_rate=None,weight_decay=1E-4):
list_features = [x]
for i in range(nb_layers):
x = conv_layer(x,concat_axis,growth_rate,dropout_rate=None,weight_decay=1E-4)
 list_features.append(x)
 x = Concatenate(axis=concat_axis )(list_features)
 nb_filter += growth_rate
return x,nb_filter
def Densenet(nb_classes,img_dim,depth,nb_dense_block,nb_filter,growth_rate,
dropout_rate=None,weight_decay=1E-4):
if K.image_dim_ordering() == "th":
concat_axis = 1
elif K.image_dim_ordering() == "tf":
concat_axis = -1
model_input = Input(shape=img_dim)
 assert(depth-4)%3 == 0 , "Depth must be 4*N +3"
nb_layers = int(( depth-4)/ 3)
 x = Conv2D(nb_filter,(3,3),padding='same',use_bias=False,
kernel_regularizer=l2(weight_decay))(model_input)
 for block_id in range(nb_dense_block-1):
x,nb_filter = denseblock(x,concat_axis,nb_filter,nb_layers,growth_rate,
dropout_rate=None,weight_decay=1E-4)
 x = transition_layer(x,concat_axis,nb_filter,dropout_rate=None,weight_decay=1E-4)
 x = BatchNormalization(axis=concat_axis,
gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(x)
 x = Activation('relu' )(x)
 x = GlobalAveragePooling2D(data_format=K.image_data_format() )(x)
 x = Dense(nb_classes,activation='softmax',kernel_regularizer=l2(weight_decay),
bias_regularizer=l2(weight_decay))(x)
 densenet = Model(inputs=[model_input], outputs=[x], name="DenseNet")
 return densenet 
<choose_model_class>
%%time
rf.fit(x_train, y_train )
model = Densenet(nb_classes=10,
img_dim=(28,28,1),
depth = 34,
nb_dense_block = 5,
growth_rate=12,
nb_filter=32,
dropout_rate=0.2,
weight_decay=1E-4)
 model.summary() 
<choose_model_class>
print('mae train: ', mean_absolute_error(rf.predict(x_train), y_train))
print('mae val: ', mean_absolute_error(rf.predict(x_val), y_val))
<train_model><EOS> 
%%time
pred = rf.predict(x_test)
 test['winPlacePerc'] = pred
submission = test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )