Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
train.isnull().sum()<correct_missing_values>
%load_ext autoreload
%autoreload 2
%matplotlib inline
train.fillna(method = 'ffill',inplace = True )<count_missing_values>
from fastai.imports import *
from fastai.structured import *
from pandas_summary import DataFrameSummary
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from IPython.display import display
from sklearn import metrics
train.isnull().sum()<count_missing_values>
df_raw = pd.read_csv('.. /input/train_V2.csv' )
train.isnull().sum()<count_missing_values>
df_raw = df_raw.dropna()
test.isnull().sum()<count_missing_values>
df_raw['totalDistance'] = df_raw['rideDistance'] + df_raw['walkDistance'] + df_raw['swimDistance']
test.isnull().sum()<import_modules>
ranking_cats = ['killPlace', 'damageDealt', 'kills', 'walkDistance', 'rankPoints', 'weaponsAcquired', 'totalDistance']
for c in ranking_cats: df_raw[c+'_ranking'] = df_raw.groupby('matchId')[c].rank(ascending=False )
import tensorflow.keras as keras
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Input,LeakyReLU, Conv2D,Flatten, BatchNormalization, Dense, Dropout, GlobalAveragePooling2D,MaxPool2D
from tensorflow.keras import optimizers
import tensorflow as tf
from keras.utils import np_utils
from keras import applications
from keras.layers import concatenate
import time
from skimage.transform import resize<choose_model_class>
df_raw = df_raw.sort_values(['matchId'] )
model = Sequential()
model.add(Conv2D(32,(5,5), padding='same', use_bias=True, input_shape=(96,96,1)))
 model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(32,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(32,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Conv2D(64,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(64,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(64,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Conv2D(96,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(96,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(96,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Conv2D(128,(5,5),padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(128,(5,5),padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Conv2D(256,(5,5),padding='same',use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(256,(5,5),padding='same',use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Conv2D(512,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(512,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Flatten())
 model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(Dropout(0.1))
model.add(Dense(30))
model.summary()<prepare_x_and_y>
train_cats(df_raw)
 df, y, nas = proc_df(df_raw, 'winPlacePerc', max_n_cat=5 )
<train_model>
def split_vals(a,n): return a[:n].copy() , a[n:].copy()
n_valid = 444647
n_trn = len(df)-n_valid
checkpointer = ModelCheckpoint(filepath = 'best_modelV8.hdf5', monitor='val_mae', verbose=1, save_best_only=True, mode='min')
 model.compile(optimizer="adam", loss='mean_squared_error', metrics=['mae', 'acc'])
 history = model.fit(train_images, train_keypoints, epochs=500, batch_size=256, validation_split=0.1, callbacks=[checkpointer])
<save_model>
def rmse(x,y): return math.sqrt(((x-y)**2 ).mean())
 def print_score(m):
res = [rmse(m.predict(X_train), y_train), rmse(m.predict(X_valid), y_valid),
m.score(X_train, y_train), m.score(X_valid, y_valid)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )
model.save('best_modelV5x5_V1.hdf5')
<predict_on_test>
set_rf_samples(100000 )
%%time
model = load_model('best_modelV5x5_V1.hdf5')
 test_preds = model.predict(test_images )<save_to_csv>
drop_cols = ['totalDistance', 'walkDistance', 'groupId', 'matchId', 'Id']
feature_names = list(idlookup_data['FeatureName'])
 image_ids = list(idlookup_data['ImageId']-1)
 row_ids = list(idlookup_data['RowId'])
 feature_list = []
for feature in feature_names:
feature_list.append(feature_names.index(feature))
predictions = []
for x,y in zip(image_ids, feature_list):
predictions.append(test_preds[x][y])
 row_ids = pd.Series(row_ids, name = 'RowId')
 locations = pd.Series(predictions, name = 'Location')
 locations = locations.clip(0.0,96.0)
 submission_result = pd.concat([row_ids,locations],axis = 1)
 submission_result.to_csv('charlin_version_5X5.csv',index = False )<set_options>
df = df.drop(drop_cols, axis=1 )
aug = ImageDataGenerator(
 		rotation_range=20,
		zoom_range=0.15,
		width_shift_range=0.2,
		height_shift_range=0.2,
		shear_range=0.15,
		horizontal_flip=True,
		fill_mode="nearest" )<concatenate>
X_train, X_valid = split_vals(df, n_trn)
 y_train, y_valid = split_vals(y, n_trn)
 X_train.shape
trainX=np.swapaxes(trainX,0,1)
 trainX=np.swapaxes(trainX,1,2)
 trainX=np.swapaxes(trainX,2,3)
 trainX.shape<concatenate>
m = RandomForestRegressor(n_estimators=100, max_features=0.5, min_samples_leaf=3, n_jobs=-1)
 %time m.fit(X_train, y_train)
 print_score(m )
testX=np.swapaxes(testX,0,1)
 testX=np.swapaxes(testX,1,2)
 testX=np.swapaxes(testX,2,3)
 testX.shape<import_modules>
fi = rf_feat_importance(m, df); fi[:]
import numpy as np
import sklearn as sk
import tensorflow as tf
import matplotlib.pyplot as plt
import pandas as pd
import sklearn.model_selection as ms
import sklearn.preprocessing as p
import math<import_modules>
to_keep = fi[fi.imp>0.0025].cols; len(to_keep )
tf.version.VERSION<load_from_csv>
df_keep = df[to_keep].copy()
X_train, X_valid = split_vals(df_keep, n_trn )
mnist = pd.read_csv('.. /input/digit-recognizer/train.csv' )<define_variables>
np.save('keep_cols.npy', np.array(df_keep.columns))
height = 28
width = 28
channels = 1<define_variables>
keep_cols = np.load('keep_cols.npy')
 df_keep = df[keep_cols]
n_outputs = 10<feature_engineering>
X_train, X_valid = split_vals(df_keep, n_trn )
mnist.loc[:3].apply(show_digit_and_print_label, axis=1)
<prepare_x_and_y>
m = RandomForestRegressor(n_estimators=70, min_samples_leaf=3, max_features=0.5, n_jobs=-1, oob_score=True)
 %time m.fit(X_train, y_train)
 print_score(m )
X_data = mnist.drop(columns='label')
 y_data = mnist['label']<concatenate>
df_test = pd.read_csv('.. /input/test_V2.csv' )
y_data = tf.keras.utils.to_categorical(y_data, num_classes = n_outputs)
 y_data.shape<split>
df_test['totalDistance'] = df_test['rideDistance'] + df_test['walkDistance'] + df_test['swimDistance']
ranking_cats = ['killPlace', 'damageDealt', 'kills', 'walkDistance', 'rankPoints', 'weaponsAcquired', 'totalDistance']
for c in ranking_cats: df_test[c+'_ranking'] = df_test.groupby('matchId')[c].rank(ascending=False)
 df_test.head()
X_train, X_val, y_train, y_val = ms.train_test_split(X_data, y_data, test_size=0.15 )<normalization>
train_cats(df_test )
scaler = p.StandardScaler()
X_train = scaler.fit_transform(X_train)
 X_train = X_train.reshape(-1, height, width, channels)
 X_val = scaler.transform(X_val)
 X_val = X_val.reshape(-1, height, width, channels )<define_variables>
df_test, y, nas = proc_df(df=df_test, y_fld=None )
batch_size = 250<define_variables>
df_submit = df_test[keep_cols]
a = m.predict(df_submit)
 a = pd.Series(a)
 submission = pd.Series(( pd.read_csv('.. /input/test_V2.csv', low_memory=False)) ['Id'])
 submission = pd.concat([submission, a], axis=1)
 submission = submission.rename(columns={0:'winPlacePerc'} )
train_data_gen = image_gen.flow(X_train, y=y_train, batch_size=batch_size )<choose_model_class>
submission.to_csv('submission.csv', index=False )
class CosineAnnealingLearningRateCallback(tf.keras.callbacks.Callback):
def __init__(self, n_epochs, n_cycles, lrate_max, n_epochs_for_saving, verbose=0):
self.epochs = n_epochs
self.cycles = n_cycles
self.lr_max = lrate_max
self.n_epochs_for_saving = n_epochs_for_saving
self.best_val_acc_per_cycle = float('-inf')
 def is_save_range(self, epoch, epochs_per_cycle, n_epochs_for_saving):
epoch += 1
f, d = math.modf(epoch / epochs_per_cycle)
 next_end = epochs_per_cycle *(d +(1 if f > 0 else 0))
need_to_save = epoch >(next_end - n_epochs_for_saving)
 return need_to_save
def cosine_annealing(self, epoch, n_epochs, n_cycles, lrate_max):
epochs_per_cycle = math.floor(n_epochs/n_cycles)
 cos_inner =(math.pi *(epoch % epochs_per_cycle)) /(epochs_per_cycle)
 return lrate_max/2 *(math.cos(cos_inner)+ 1)
 def on_epoch_begin(self, epoch, logs=None):
lr = self.cosine_annealing(epoch, self.epochs, self.cycles, self.lr_max)
 tf.keras.backend.set_value(self.model.optimizer.lr, lr)
 def on_epoch_end(self, epoch, logs={}):
epochs_per_cycle = math.floor(self.epochs / self.cycles)
 if epoch % epochs_per_cycle == 0:
self.best_val_acc_per_cycle = float('-inf')
 isr = self.is_save_range(epoch, epochs_per_cycle, self.n_epochs_for_saving)
 last_val_acc = logs['val_accuracy']
if epoch != 0 and isr and last_val_acc > self.best_val_acc_per_cycle:
self.best_val_acc_per_cycle = last_val_acc
filename = f'snapshot_model_{epoch // epochs_per_cycle}.h5'
self.model.save(filename)
 print(f'saved snapshot {filename}, epoch: {epoch}, val_accuracy: {last_val_acc:.5f}')
 n_epochs = 300
n_cycles = n_epochs / 50
n_epochs_for_saving = 20
calrc = CosineAnnealingLearningRateCallback(n_epochs, n_cycles, 0.01, n_epochs_for_saving )<choose_model_class>
submission.to_csv('submission.csv', index=False )