Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<predict_on_test>
train_data_df = pd.read_csv('.. /input/train_V2.csv')
 test_data_df = pd.read_csv('.. /input/test_V2.csv' )
def every_predict(model,submissionloader=submissionloader):
all_batchs = []
with torch.no_grad() :
model.eval()
for images in submissionloader:
if train_on_gpu:
images = images.cuda()
ps = model(images)
 all_batchs.append(ps.to('cpu' ).detach().numpy())
 return all_batchs<choose_model_class>
train_data_df.insert(loc=28, column='totalDistance', value=train_data_df['swimDistance'] + train_data_df['walkDistance'] + train_data_df['rideDistance'])
 test_data_df.insert(loc=28, column='totalDistance', value=test_data_df['swimDistance'] + test_data_df['walkDistance'] + test_data_df['rideDistance'] )
five_predict = []
all_train_losses, all_valid_losses = [], []
FOLD = 1
for i in train_valid_loaders:
model = Net()
if train_on_gpu:
model.cuda()
train_loader, valid_loader = i
LEARNING_RATE = 0.01
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters() ,lr=LEARNING_RATE)
 epochs = 120
valid_loss_min = np.Inf
train_losses, valid_losses = [], []
history_accuracy = []
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer,factor=0.7,patience=2)
 model.train()
for e in range(1, epochs+1):
running_loss = 0
for images, labels in train_loader:
if train_on_gpu:
images, labels = images.cuda() , labels.cuda()
optimizer.zero_grad()
ps = model(images)
 loss = criterion(ps, labels)
 loss.backward()
optimizer.step()
running_loss += loss.item()
else:
valid_loss = 0
accuracy = 0
with torch.no_grad() :
model.eval()
for images, labels in valid_loader:
if train_on_gpu:
images, labels = images.cuda() , labels.cuda()
ps = model(images)
 _, top_class = ps.topk(1, dim=1)
 equals = top_class == labels.view(*top_class.shape)
 valid_loss += criterion(ps, labels)
 accuracy += torch.mean(equals.type(torch.FloatTensor))
model.train()
train_losses.append(running_loss/len(train_loader))
valid_losses.append(valid_loss/len(valid_loader))
history_accuracy.append(accuracy/len(valid_loader))
network_learned = valid_loss < valid_loss_min
if e == 1 or e % 5 == 0 or network_learned:
print(f"Epoch: {e}/{epochs}.. ",
f"Training Loss: {running_loss/len(train_loader):.4f}.. ",
f"Validation Loss: {valid_loss/len(valid_loader):.4f}.. ",
f"Valid Accuracy: {accuracy/len(valid_loader):.4f}")
 if network_learned:
valid_loss_min = valid_loss
torch.save(model.state_dict() , f'best_model_fold{FOLD}.pt')
 print('Detected network improvement, saving current model')
 scheduler.step(running_loss)
 all_train_losses.append(train_losses)
 all_valid_losses.append(valid_losses)
 model.load_state_dict(torch.load(f'best_model_fold{FOLD}.pt'))
model.eval()
five_predict.append(every_predict(model))
model.train()
FOLD +=1 
<save_to_csv>
train_data_df = train_data_df.replace({'matchType' : {'crashfpp':0, 'crashtpp':1, 'duo':2, 'duo-fpp':3, 'flarefpp':4, 'flaretpp':5, 'normal-duo':6, 'normal-duo-fpp':7, 'normal-solo':8, 'normal-solo-fpp':9, 'normal-squad':10, 'normal-squad-fpp':11, 'solo':12, 'solo-fpp':13, 'squad':14, 'squad-fpp':15}})
 test_data_df = test_data_df.replace({'matchType' : {'crashfpp':0, 'crashtpp':1, 'duo':2, 'duo-fpp':3, 'flarefpp':4, 'flaretpp':5, 'normal-duo':6, 'normal-duo-fpp':7, 'normal-solo':8, 'normal-solo-fpp':9, 'normal-squad':10, 'normal-squad-fpp':11, 'solo':12, 'solo-fpp':13, 'squad':14, 'squad-fpp':15}} )
flat_list = []
for sublist in five_predict:
for item in sublist:
for i in item:
flat_list.append(i)
 final = []
for i in range(0,28000):
numbers = [i+a*28000 for a in range(N_FOLDS)]
final.append(sum(flat_list[C] for C in numbers))
subm = np.argmax(( final),axis=1)
 sample_subm = pd.read_csv(f'{PATH}sample_submission.csv')
 sample_subm['Label'] = subm
sample_subm.to_csv('submission.csv',index=False )<import_modules>
train_data_df = reduce_mem_usage(train_data_df)
 test_data_df = reduce_mem_usage(test_data_df )
WORKERS = 2
CHANNEL = 3
warnings.filterwarnings("ignore")
 SIZE = 128
NUM_CLASSES = 10
%config InlineBackend.figure_format = 'retina'
%matplotlib inline<load_from_csv>
groupedGroups = train_data_df.groupby(['matchId','groupId'])[columns]
print("Add group mean features")
 group_features = groupedGroups.agg('mean')
 group_rank_features = group_features.groupby(['matchId'])[columns].rank(pct=True ).reset_index()
features = pd.merge(group_features, group_rank_features, on=['matchId', 'groupId'], how='right', suffixes=['_group_mean', '_group_mean_rank'])
 print("Add group max features")
 group_features = groupedGroups.agg('max')
 group_rank_features = group_features.groupby(['matchId'])[columns].rank(pct=True ).reset_index()
features = pd.merge(features, group_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_max'])
 features = pd.merge(features, group_rank_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_max_rank'])
 print("Add group min features")
 group_features = groupedGroups.agg('min')
 group_rank_features = group_features.groupby(['matchId'])[columns].rank(pct=True ).reset_index()
features = pd.merge(features, group_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_min'])
 features = pd.merge(features, group_rank_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_min_rank'])
 print("Add group size features")
 group_features = train_data_df.groupby(['matchId','groupId'] ).size()
group_features.columns = ['matchId', 'groupId', 'group_size']
group_features = pd.DataFrame(group_features)
 features = pd.merge(features, group_features, on=['matchId', 'groupId'], how='right', suffixes=['', ''] )
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )<feature_engineering>
groupedMatches = train_data_df.groupby(['matchId'])[columns]
print("Add match mean features")
 match_features = groupedMatches.agg('mean')[columns].reset_index()
features = pd.merge(features, match_features, on=['matchId'], how='right', suffixes=['', '_match_mean'])
 print("Add match size features")
 match_features = train_data_df.groupby(['matchId'] ).size().reset_index()
match_features.columns = ['matchId', 'match_size']
features = pd.merge(features, match_features, on=['matchId'], how='right', suffixes=['', ''] )
x = x / 255.0
test = test / 255.0<categorify>
features = features.fillna(0.0 )
y = to_categorical(y, num_classes = 10 )<split>
train_targets = train_data_df.groupby(['matchId', 'groupId'])['winPlacePerc'].agg('mean' ).reset_index() ['winPlacePerc']
x_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size = 0.1, random_state=2, stratify = y, shuffle = True )<import_modules>
train_targets = train_targets.values
BatchNormalization, Input, Conv2D, GlobalAveragePooling2D)
<choose_model_class>
import sklearn
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_features = features.drop(['matchId', 'groupId'], axis=1 ).values
scaler = MinMaxScaler(feature_range=(-1, 1)).fit(train_features)
 train_features = scaler.transform(train_features )
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )<choose_model_class>
train_features = np.delete(train_features, 266069, 0)
 train_targets = np.delete(train_targets, 266069, 0 )
EarlyStopping, ReduceLROnPlateau)
 epochs = 80; batch_size = 1024
checkpoint = ModelCheckpoint('.. /working/Resnet50-visible.h5', monitor='val_loss', verbose=1,
save_best_only=True, mode='min', save_weights_only = True)
 reduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=4,
verbose=1, mode='min', epsilon=0.0001)
 early = EarlyStopping(monitor="val_loss",
mode="min",
patience=9)
 callbacks_list = [checkpoint, reduceLROnPlat, early]<choose_model_class>
import ultimate
from ultimate.mlp import MLP
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )<init_hyperparams>
epoch_train = 15
model = MLP(layer_size=[train_features.shape[1], 32, 32, 32, 1], regularization=1, output_shrink=0.1, output_range=[0,1], loss_type="hardmse")
 model.train(train_features, train_targets, iteration_log=20000, rate_init=0.08, rate_decay=0.8, epoch_train=epoch_train, epoch_decay=1 )
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>
groupedGroups = test_data_df.groupby(['matchId','groupId'])[columns]
print("Add group mean features")
 group_features = groupedGroups.agg('mean')
 group_rank_features = group_features.groupby(['matchId'])[columns].rank(pct=True ).reset_index()
features = pd.merge(group_features, group_rank_features, on=['matchId', 'groupId'], how='right', suffixes=['_group_mean', '_group_mean_rank'])
 print("Add group max features")
 group_features = groupedGroups.agg('max')
 group_rank_features = group_features.groupby(['matchId'])[columns].rank(pct=True ).reset_index()
features = pd.merge(features, group_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_max'])
 features = pd.merge(features, group_rank_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_max_rank'])
 print("Add group min features")
 group_features = groupedGroups.agg('min')
 group_rank_features = group_features.groupby(['matchId'])[columns].rank(pct=True ).reset_index()
features = pd.merge(features, group_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_min'])
 features = pd.merge(features, group_rank_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_min_rank'])
 print("Add group size features")
 group_features = test_data_df.groupby(['matchId','groupId'] ).size()
group_features.columns = ['matchId', 'groupId', 'group_size']
group_features = pd.DataFrame(group_features)
 features = pd.merge(features, group_features, on=['matchId', 'groupId'], how='right', suffixes=['', ''] )
batch_size = 1024
epochs = 80
history = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data =(x_valid,y_valid),
verbose = 1, steps_per_epoch=x_train.shape[0] // batch_size
, callbacks=callbacks_list )<predict_on_test>
groupedMatches = test_data_df.groupby(['matchId'])[columns]
print("Add match mean features")
 match_features = groupedMatches.agg('mean')[columns].reset_index()
features = pd.merge(features, match_features, on=['matchId'], how='right', suffixes=['', '_match_mean'])
 print("Add match size features")
 match_features = test_data_df.groupby(['matchId'] ).size().reset_index()
match_features.columns = ['matchId', 'match_size']
features = pd.merge(features, match_features, on=['matchId'], how='right', suffixes=['', ''] )
model.load_weights('.. /working/Resnet50-visible.h5')
 results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
test_features = features.drop(['matchId', 'groupId'], axis=1 ).values
test_features = scaler.transform(test_features )
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("submission.csv",index=False )<import_modules>
predictions = model.predict(test_features )
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator<load_from_csv>
features['winPlacePercPred'] = predictions
group_preds = features.groupby(['matchId', 'groupId'])['winPlacePercPred'].agg('mean' ).groupby(['matchId'] ).rank(pct=True )
np.random.seed(1)
 X_raw = pd.read_csv(".. /input/digit-recognizer/train.csv")
 X_test_raw = pd.read_csv(".. /input/digit-recognizer/test.csv")
 y = X_raw["label"]
X = X_raw.drop(labels = ["label"],axis = 1)
 X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.25, random_state=0)
 WIDTH=28
HEIGHT=28
NUM_CLASSES=10
X_train = X_train.values
y_train = y_train.values
X_valid = X_valid.values
y_valid = y_valid.values
y_train_oh = to_categorical(y_train, num_classes = NUM_CLASSES)
 y_valid_oh = to_categorical(y_valid, num_classes = NUM_CLASSES)
 X_test = X_test_raw.values<normalization>
dictionary = dict(zip(features['groupId'].values, group_preds.values))
def scaleData(X):
n_max = X_train.max()
X = X/n_max
return X
def reshape_channel(X):
return np.expand_dims(X.reshape(-1,HEIGHT,WIDTH),-1)
 def preprocessData(X):
X = scaleData(X)
 X = reshape_channel(X)
 return X<choose_model_class>
individual_preds = []
for i in test_data_df['groupId'].values:
individual_preds.append(dictionary[i])
 test_data_df['winPlacePercPred'] = individual_preds
optimizer = Adam(learning_rate=0.0001, beta_1=0.9, beta_2=0.9999, amsgrad=True)
 model = Sequential()
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'same',activation ='relu',use_bias=True,input_shape =(HEIGHT,WIDTH,1)))
 model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'same',activation ='relu',use_bias=True))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'same',activation ='relu',use_bias=True))
model.add(MaxPool2D(pool_size=(3,3), strides=(3,3)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'same',activation ='relu',use_bias=True))
model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'same',activation ='relu',use_bias=True))
model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'same',activation ='relu',use_bias=True))
model.add(MaxPool2D(pool_size=(5,5), strides=(5,5)))
 model.add(Flatten())
 model.add(Dropout(0.2))
model.add(Dense(64, activation = "relu"))
model.add(Dense(64, activation = "relu"))
model.add(Dense(NUM_CLASSES, activation = "softmax"))
model.compile(optimizer ='adam', loss = "categorical_crossentropy", metrics=["accuracy"])
 model.summary()<define_variables>
predictions = pd.DataFrame(np.transpose(np.array([test_data_df['Id'], test_data_df['winPlacePercPred']])))
 predictions.columns = ['Id', 'winPlacePerc']
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1, 
)
 it_train = datagen.flow(preprocessData(X_train), y_train_oh)
 it_valid = datagen.flow(preprocessData(X_valid), y_valid_oh )<train_model>
maxPlaces = test_data_df['maxPlace'].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
hist = model.fit_generator(it_train,validation_data=it_valid,callbacks=[lrate],epochs=n_epoch)
<save_to_csv>
predictions['winPlacePerc'] = new_predictions
<load_from_csv><EOS> 
predictions.to_csv('PUBG_preds.csv', index=False )