Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<load_from_csv>
import gc
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout
from keras import optimizers
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
import lightgbm as lgb
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<feature_engineering>
INPUT_DIR = ".. /input/"
LABEL = 'winPlacePerc'
X_train = X_train / 255.0
test = test / 255.0<train_model>
df_train = pd.read_csv(INPUT_DIR+'train_V2.csv' )
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )<categorify>
df_train = reduce_mem_usage(df_train)
 gc.collect()
Y_train = to_categorical(Y_train, num_classes = 10 )<split>
df_train.isnull().any()
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2 )<choose_model_class>
df_train = df_train.dropna()
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', kernel_initializer='he_normal',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', kernel_initializer='he_normal',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', kernel_initializer='he_normal',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', kernel_initializer='he_normal',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(128,(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(10, activation = "softmax"))<choose_model_class>
df_train = df_train[df_train['maxPlace'] > 1]
optimizer = Adam(lr=0.003 )<choose_model_class>
def FE(df,train=True):
LABEL = 'winPlacePerc'
if train:
df_y = df.groupby(['matchId','groupId'])[LABEL].agg('mean')
 df = df.drop([LABEL],axis=1)
 else:
df_ids = df[['Id','matchId','groupId']]
df = df.drop('Id',axis=1)
 MATCH_FEATURE_part = ['numGroups','matchDuration','matchType','maxPlace']
GROUP_FEATURE = df.columns.tolist()
GROUP_FEATURE.remove('groupId')
 GROUP_FEATURE.remove('matchId')
 for fe in MATCH_FEATURE_part:
GROUP_FEATURE.remove(fe)
 MATCH_FEATURE = MATCH_FEATURE_part + GROUP_FEATURE
MATCH_FEATURE.remove('matchType')
 dm = df.groupby(['matchId','groupId'])
 df_X = dm.size().to_frame(name='groupSize')
 gp = dm[GROUP_FEATURE].agg('max')
 gp_rank = gp.reset_index().groupby(['matchId'])[GROUP_FEATURE].rank(pct=True ).set_index(df_X.index)
 df_X = df_X.join(gp ).join(gp_rank,rsuffix='_max_rank')
 gp = dm[GROUP_FEATURE].agg('min')
 gp_rank = gp.reset_index().groupby(['matchId'])[GROUP_FEATURE].rank(pct=True ).set_index(df_X.index)
 df_X = df_X.join(gp,rsuffix='_min' ).join(gp_rank,rsuffix='_min_rank')
 gp = dm[GROUP_FEATURE].agg('mean')
 gp_rank = gp.reset_index().groupby(['matchId'])[GROUP_FEATURE].rank(pct=True ).set_index(df_X.index)
 df_X = df_X.join(gp,rsuffix='_mean' ).join(gp_rank,rsuffix='_mean_rank')
 df_X = df_X.drop(columns = ['killPlace_min_rank','killPlace_max_rank','killPlace_mean_rank'])
 dm = df.groupby(['matchId'])
 df_X = df_X.join(dm[MATCH_FEATURE].agg('mean'),lsuffix='_max',rsuffix='_mean_match')
 df_X = df_X.join(pd.concat([pd.get_dummies(df.matchType),df[['matchId','groupId']]],axis=1 ).groupby(['matchId','groupId'] ).agg('min'))
if not train:
df_X_index = df_X.reset_index() [['matchId','groupId']]
lst_features = list(df_X.columns)
 del df,dm,gp,gp_rank
gc.collect()
if train:
return df_X,df_y,lst_features
else:
return df_X, df_X_index, df_ids
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )<choose_model_class>
X_train,y_train,lst_features = FE(df_train )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.000001 )<define_variables>
epochs = 35
batch_size = 64<init_hyperparams>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=15,
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>
del df_train
gc.collect()
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )<predict_on_test>
df_test = pd.read_csv(INPUT_DIR+'test_V2.csv')
 df_test = reduce_mem_usage(df_test)
 gc.collect()
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
X_test, X_test_index,df_test_ids = FE(df_test,train=False )
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )<import_modules>
folds = KFold(n_splits=3,random_state=3)
 vali_pred = np.zeros(X_train.shape[0])
 pred = np.zeros(X_test.shape[0])
 df_feature_importance = pd.DataFrame()
valid_score = 0
import gc
import re
import operator
import numpy as np
import pandas as pd
from gensim.models import KeyedVectors
from sklearn import model_selection
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Embedding, Input, Dense, CuDNNGRU,CuDNNLSTM, Concatenate, concatenate, Bidirectional, SpatialDropout1D, Conv1D, GlobalAveragePooling1D, GlobalMaxPooling1D
from keras.optimizers import RMSprop, Adam
from keras.models import Model
from keras.callbacks import EarlyStopping
import seaborn as sns<load_from_csv>
params = {"objective" : "regression", "metric" : "mae", 'n_estimators':20000, 'early_stopping_rounds':100,
"num_leaves" : 25, "learning_rate" : 0.05, "bagging_fraction" : 0.9, "feature_fraction":0.7,
"bagging_seed" : 0, "num_threads" : 4
}
train = pd.read_csv(".. /input/jigsaw-unintended-bias-in-toxicity-classification/train.csv")
 test = pd.read_csv(".. /input/jigsaw-unintended-bias-in-toxicity-classification/test.csv")
 print("Train shape : ",train.shape)
 print("Test shape : ",test.shape)
 train.head() 
<concatenate>
for n_fold,(train_idx, vali_idx)in enumerate(folds.split(X_train, y_train)) :
X_train_fold, y_train_fold = X_train.iloc[train_idx], y_train[train_idx]
X_vali, y_vali = X_train.iloc[vali_idx], y_train[vali_idx]
train_data = lgb.Dataset(data=X_train_fold, label=y_train_fold)
 valid_data = lgb.Dataset(data=X_vali, label=y_vali)
 lgb_model = lgb.train(params, train_data, valid_sets=[train_data, valid_data], verbose_eval=1000)
 pred_fold = lgb_model.predict(X_test, num_iteration=lgb_model.best_iteration)
 pred_fold[pred_fold>1] = 1
pred_fold[pred_fold<0] = 0
pred += pred_fold/ folds.n_splits
vali_pred[vali_idx] = lgb_model.predict(X_vali, num_iteration=lgb_model.best_iteration)
 vali_pred[vali_pred>1] = 1
vali_pred[vali_pred<0] = 0
print('Fold %2d MAE : %.6f' %(n_fold + 1, mean_absolute_error(y_vali, vali_pred[vali_idx])))
 df_fold_importance = pd.DataFrame()
df_fold_importance = df_fold_importance.assign(feature= lst_features)
 df_fold_importance = df_fold_importance.assign(importance = lgb_model.feature_importance())
 df_fold_importance = df_fold_importance.assign(fold = n_fold + 1)
 df_feature_importance = pd.concat([df_feature_importance, df_fold_importance])
 gc.collect()
df = pd.concat([train[['id','comment_text']], test], axis=0)
 del(train, test)
 gc.collect()<load_pretrained>
print('Full mae score %.6f' % mean_absolute_error(y_train, vali_pred))
ft_common_crawl = '.. /input/fasttext-crawl-300d-2m/crawl-300d-2M.vec'
embeddings_index = KeyedVectors.load_word2vec_format(ft_common_crawl )<set_options>
lgb_model.save_model('lgb_model.txt' )
gc.collect()<feature_engineering>
pred = np.clip(pred, a_min=0, a_max=1)
 df_pred = X_test_index.assign(winPlacePerc=pred)
 result = pd.merge(df_test_ids, df_pred, how='left', on=['matchId', 'groupId'] )
<categorify><EOS> 
submission = result[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )