Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<save_to_csv>
def get_sample(df,n):

 idxs = sorted(np.random.permutation(len(df)) [:n])
 return df.iloc[idxs].copy()
def proc_df(df, y_fld, skip_flds=None, do_scale=False, na_dict=None,
preproc_fn=None, max_n_cat=None, subset=None, mapper=None):

 if not skip_flds: skip_flds=[]
if subset: df = get_sample(df,subset)
 df = df.copy()
if preproc_fn: preproc_fn(df)
 y = df[y_fld].values
df.drop(skip_flds+[y_fld], axis=1, inplace=True)
 if na_dict is None: na_dict = {}
for n,c in df.items() : na_dict = fix_missing(df, c, n, na_dict)
 if do_scale: mapper = scale_vars(df, mapper)
 for n,c in df.items() : numericalize(df, c, n, max_n_cat)
 res = [pd.get_dummies(df, dummy_na=True), y, na_dict]
if do_scale: res = res + [mapper]
return res
def rf_feat_importance(m, df):
return pd.DataFrame({'cols':df.columns, 'imp':m.feature_importances_}
).sort_values('imp', ascending=False)
 def set_rf_samples(n):

 forest._generate_sample_indices =(lambda rs, n_samples:
forest.check_random_state(rs ).randint(0, n_samples, n))
def reset_rf_samples() :

 forest._generate_sample_indices =(lambda rs, n_samples:
forest.check_random_state(rs ).randint(0, n_samples, n_samples))
text_clf.fit(X_train_embeddings['outputs'], target.values)
 pred_test = text_clf.predict(X_test_embeddings['outputs'])
 ssub["target"] = pred_test
ssub.to_csv('submission.csv',index=False )<import_modules>
train = pd.read_csv(KAGGLE_DIR + 'train_V2.csv')
 test = pd.read_csv(KAGGLE_DIR + 'test_V2.csv' )
import numpy as np
import pandas as pd
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from sklearn.metrics import confusion_matrix,accuracy_score
from sklearn.model_selection import train_test_split
import matplotlib
import matplotlib.pyplot as plt<train_model>
train.info()
print('Shape of training set: ', train.shape )
def print_metrics(y_train,y_pred):
conf_mx = confusion_matrix(y_train,y_pred)
 print(conf_mx)
 print("------------------------------------------")
 print(" Accuracy : ", accuracy_score(y_train,y_pred))
print("------------------------------------------")
 def shift_image(X, dx, dy,length=28):
X=X.reshape(length,length)
 X = np.roll(X, dy, axis=0)
 X = np.roll(X, dx, axis=1)
 return X.reshape([-1])
 def print_image(flat_image,length=28):
plt.imshow(flat_image.reshape(length, length), cmap = matplotlib.cm.binary,interpolation="nearest")
 plt.axis("off")
 plt.show() 
<load_from_csv>
train[train['winPlacePerc'].isnull() ]
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv" ).values
y = train["label"].values
X = train.drop(labels = ["label"],axis = 1 ).values
print("Value Counts :")
 print(train["label"].value_counts())
 del train
X = X / 255.0
test = test / 255.0
print("dim(X)= ",X.shape)
 print("dim(y)= ",y.shape)
 print("dim(test)= ",test.shape )<define_variables>
train.drop(2744604, inplace=True )
DATA_AUGMENTED_WITH_SHIFT = False<randomize_order>
train[train['winPlacePerc'].isnull() ]
if DATA_AUGMENTED_WITH_SHIFT:
X_augmented = [image for image in X]
y_augmented = [label for label in y]
for dx, dy in(( 1,1),(-1,-1),(-1,1),(1,-1)) :
for image, label in zip(X, y):
X_augmented.append(shift_image(image, dx, dy))
y_augmented.append(label)
 X_augmented = np.array(X_augmented)
 y_augmented = np.array(y_augmented)
 print(" X_augmented Dimension : ",X_augmented.shape)
 shuffle_idx = np.random.permutation(len(X_augmented))
X_augmented = X_augmented[shuffle_idx]
y_augmented = y_augmented[shuffle_idx]
X_train = X_augmented.reshape(-1,28,28,1)
 test = test.reshape(-1,28,28,1)
 Y_train = to_categorical(y_augmented, num_classes = 10)
 else:
X_train = X.reshape(-1,28,28,1)
 test = test.reshape(-1,28,28,1)
 Y_train = to_categorical(y, num_classes = 10)
 print("dim(X_train)= ",X_train.shape)
 print("dim(Y_train)= ",Y_train.shape)
 print("dim(test)= ",test.shape )<split>
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']
train[to_show][0:11]
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size=0.1, random_state=42)
 datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<choose_model_class>
train['healsandboosts'] = train['heals'] + train['boosts']
train[['heals', 'boosts', 'healsandboosts']].tail()
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))<choose_model_class>
train['totalDistance'] = train['rideDistance'] + train['walkDistance'] + train['swimDistance']
train['killsWithoutMoving'] =(( train['kills'] > 0)&(train['totalDistance'] == 0))
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
<train_model>
train['headshot_rate'] = train['headshotKills'] / train['kills']
train['headshot_rate'] = train['headshot_rate'].fillna(0 )
epochs = 30
batch_size = 71
model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 1, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction])
<compute_train_metric>
train.drop(train[train['killsWithoutMoving'] == True].index, inplace=True )
Y_pred = model.predict(X_val)
 Y_pred_classes = np.argmax(Y_pred,axis = 1)
 Y_true = np.argmax(Y_val,axis = 1)
 print_metrics(Y_true, Y_pred_classes )<save_to_csv>
train[train['roadKills'] > 10]
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("submission.csv",index=False )<install_modules>
train.drop(train[train['roadKills'] > 10].index, inplace=True )
!pip install simpletransformers<import_modules>
train.drop(train[train['kills'] > 30].index, inplace=True )
import os, re, string
import random
import numpy as np
import pandas as pd
import sklearn
import torch
from simpletransformers.classification import ClassificationModel
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold<set_options>
train.drop(train[train['longestKill'] >= 1000].index, inplace=True )
seed = 1337
random.seed(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True<load_from_csv>
train.drop(train[train['walkDistance'] >= 10000].index, inplace=True )
train_data = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 train_data = train_data[['text', 'target']]<choose_model_class>
train.drop(train[train['rideDistance'] >= 20000].index, inplace=True )
bert_uncased = ClassificationModel('bert', 'bert-large-uncased')
 bert_uncased.args<init_hyperparams>
train[train['swimDistance'] >= 2000]
custom_args = {'fp16': False,
'train_batch_size': 4,
'gradient_accumulation_steps': 2,
'do_lower_case': True,
'learning_rate': 1e-05,
'overwrite_output_dir': True,
'num_train_epochs': 2}<load_pretrained>
train.drop(train[train['swimDistance'] >= 2000].index, inplace=True )
n=5
kf = KFold(n_splits=n, random_state=seed, shuffle=True)
 results = []
for train_index, val_index in kf.split(train_data):
train_df = train_data.iloc[train_index]
val_df = train_data.iloc[val_index]
model = ClassificationModel('bert', 'bert-base-uncased', args=custom_args)
 model.train_model(train_df)
 result, model_outputs, wrong_predictions = model.eval_model(val_df, acc=sklearn.metrics.accuracy_score)
 print(result['acc'])
 results.append(result['acc'] )<compute_test_metric>
train.drop(train[train['weaponsAcquired'] >= 80].index, inplace=True )
for i, result in enumerate(results, 1):
print(f"Fold-{i}: {result}")
 print(f"{n}-fold CV accuracy result: Mean: {np.mean(results)} Standard deviation:{np.std(results)}" )<train_model>
train.drop(train[train['heals'] >= 40].index, inplace=True )
model = ClassificationModel('bert', 'bert-base-uncased', args=custom_args)
 model.train_model(train_data )<save_to_csv>
print('There are {} different Match types in the dataset.'.format(train['matchType'].nunique()))
test_data = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 predictions, raw_outputs = model.predict(test_data['text'])
 sample_submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 sample_submission["target"] = predictions
sample_submission.to_csv("submission.csv", index=False )<load_from_csv>
train = pd.get_dummies(train, columns=['matchType'])
 matchType_encoding = train.filter(regex='matchType')
 matchType_encoding.head()
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
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()
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<prepare_x_and_y>
train.drop(columns = ['Id'], inplace=True )
Y_train = train['label']
X_train = train.drop(labels=['label'],axis=1)
 fig, ax = plt.subplots(figsize=(16,8))
sns.countplot(Y_train,ax=ax )<count_values>
df = df_sample.drop(columns = ['winPlacePerc'])
 y = df_sample['winPlacePerc']
Y_train.value_counts()<prepare_x_and_y>
def split_vals(a, n : int):
return a[:n].copy() , a[n:].copy()
val_perc = 0.12
n_valid = int(val_perc * sample)
 n_trn = len(df)-n_valid
raw_train, raw_valid = split_vals(df_sample, n_trn)
 X_train, X_valid = split_vals(df, n_trn)
 y_train, y_valid = split_vals(y, n_trn)
 print('Sample train shape: ', X_train.shape,
'Sample target shape: ', y_train.shape,
'Sample validation shape: ', X_valid.shape )
X_train = X_train / 255.
 test = test / 255.
 X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )<categorify>
def print_score(m : RandomForestRegressor):
res = ['mae train: ', mean_absolute_error(m.predict(X_train), y_train),
'mae val: ', mean_absolute_error(m.predict(X_valid), y_valid)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )
Y_train = to_categorical(Y_train,num_classes = 10 )<split>
m1 = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features='sqrt',
n_jobs=-1)
 m1.fit(X_train, y_train)
 print_score(m1 )
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1 )<choose_model_class>
fi = rf_feat_importance(m1, df); fi[:10]
model = Sequential()
model.add(Conv2D(filters = 128, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(Conv2D(filters = 32, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
model.compile(optimizer=RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0),loss = "categorical_crossentropy",
metrics=["accuracy"])
 model.summary()<init_hyperparams>
to_keep = fi[fi.imp>0.005].cols
print('Significant features: ', len(to_keep))
to_keep
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<train_model>
df_keep = df[to_keep].copy()
X_train, X_valid = split_vals(df_keep, n_trn )
callbacks_list = [
ModelCheckpoint(filepath='./my_model.h5',monitor='val_loss'),
ReduceLROnPlateau(monitor='val_acc', patience=5, verbose=2, factor=0.5, min_lr=0.00001),
TensorBoard("logs")]
epochs = 20
batch_size =64
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=callbacks_list )<set_options>
m2 = RandomForestRegressor(n_estimators=80, min_samples_leaf=3, max_features='sqrt',
n_jobs=-1)
 m2.fit(X_train, y_train)
 print_score(m2 )
%load_ext tensorboard.notebook
%tensorboard --logdir logs<predict_on_test>
val_perc_full = 0.12
n_valid_full = int(val_perc_full * len(train))
n_trn_full = len(train)-n_valid_full
df_full = train.drop(columns = ['winPlacePerc'])
 y = train['winPlacePerc']
df_full = df_full[to_keep]
X_train, X_valid = split_vals(df_full, n_trn_full)
 y_train, y_valid = split_vals(y, n_trn_full)
 print('Sample train shape: ', X_train.shape,
'Sample target shape: ', y_train.shape,
'Sample validation shape: ', X_valid.shape )
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
m3 = RandomForestRegressor(n_estimators=70, min_samples_leaf=3, max_features=0.5,
n_jobs=-1)
 m3.fit(X_train, y_train)
 print_score(m3 )
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("predict.csv",index=False )<import_modules>
test['headshot_rate'] = test['headshotKills'] / test['kills']
test['headshot_rate'] = test['headshot_rate'].fillna(0)
 test['totalDistance'] = test['rideDistance'] + test['walkDistance'] + test['swimDistance']
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['killsWithoutMoving'] =(( test['kills'] > 0)&(test['totalDistance'] == 0))
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
test_pred = test[to_keep].copy()
test_pred.fillna(0, inplace=True)
 test_pred.head()
<load_from_csv><EOS> 
predictions = np.clip(a = m3.predict(test_pred), a_min = 0.0, a_max = 1.0)
 pred_df = pd.DataFrame({'Id' : test['Id'], 'winPlacePerc' : predictions})
 pred_df.to_csv("submission.csv", index=False )