Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<compute_test_metric>
!pip install ultimate
gc.enable()
loss, acc = model.evaluate(x_dev, y_dev)
 print("Validation loss: {:.4f}".format(loss))
print("Validation accuracy: {:.4f}".format(acc))<save_to_csv>
INPUT_DIR = ".. /input/"
y_pred = model.predict(x_test, batch_size=batch_size)
 y_pred = np.argmax(y_pred, axis=1 ).reshape(( -1, 1))
idx = np.reshape(np.arange(1, len(y_pred)+ 1),(len(y_pred), -1))
y_pred = np.hstack(( idx, y_pred))
y_pred = pd.DataFrame(y_pred, columns=['ImageId', 'Label'])
 y_pred.to_csv('y_pred.csv', index=False )<set_options>
def feature_engineering(is_train=True):
if is_train:
print("processing train_V2.csv")
 df = pd.read_csv(INPUT_DIR + 'train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test_V2.csv")
 df = pd.read_csv(INPUT_DIR + 'test_V2.csv')
 state('totalDistance')
 s = timer()
df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
e = timer()
state('totalDistance', False, e - s)
 state('rankPoints')
 s = timer()
df['rankPoints'] = np.where(df['rankPoints'] <= 0 ,0 , df['rankPoints'])
 e = timer()
state('rankPoints', False, e-s)
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchDuration")
 features.remove("matchType")
 y = None
if is_train:
y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
if is_train: df_out = agg.reset_index() [['matchId','groupId']]
else: df_out = df[['matchId','groupId']]
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_mean", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 print("get group max feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('max')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_max", "_max_rank"], how='left', on=['matchId', 'groupId'])
 print("get group min feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('min')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_min", "_min_rank"], how='left', on=['matchId', 'groupId'])
 print("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = np.array(df_out, dtype=np.float64)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
print(os.listdir("/kaggle/input/digit-recognizer"))
N_FOLDS = 5
BATCH_SIZE = 256<define_variables>
%%time
x_train, y = feature_engineering(True)
 scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1), copy=False ).fit(x_train )
PATH = '/kaggle/input/digit-recognizer/'<train_model>
print("x_train", x_train.shape, x_train.max() , x_train.min())
 scaler.transform(x_train)
 print("x_train", x_train.shape, x_train.max() , x_train.min() )
train_on_gpu = torch.cuda.is_available()
if not train_on_gpu:
print('Training on CPU...')
 else:
print('Training on GPU...' )<data_type_conversions>
%%time
NN_model = Sequential()
NN_model.add(Dense(x_train.shape[1], input_dim = x_train.shape[1], activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(1, activation='linear'))
NN_model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['mean_absolute_error'])
 NN_model.summary()
class DatasetMNIST(torch.utils.data.Dataset):
def __init__(self, data, augmentations=None):
self.data = data
self.augmentations = augmentations
def __len__(self):
return len(self.data)
 def __getitem__(self, index):
item = self.data.iloc[index]
image = item[1:].values.astype(np.uint8 ).reshape(( 28, 28, 1))
label = item[0]
if self.augmentations is not None:
augmented = self.augmentations(image=image)
 return augmented['image'], label
else:
return image, label<load_from_csv>
%%time
NN_model.fit(x=x_train, y=y, batch_size=1000,
epochs=30, verbose=1, callbacks=callbacks_list,
validation_split=0.15, validation_data=None, shuffle=True,
class_weight=None, sample_weight=None, initial_epoch=0,
steps_per_epoch=None, validation_steps=None)
 del x_train, y
gc.collect()
dataset = pd.read_csv(f'{PATH}train.csv')
 dataset.head(1 )<drop_column>
x_test, _ = feature_engineering(False)
 scaler.transform(x_test)
 print("x_test", x_test.shape, x_test.max() , x_test.min())
 np.clip(x_test, out=x_test, a_min=-1, a_max=1)
 print("x_test", x_test.shape, x_test.max() , x_test.min() )
def custom_folds(dataset,n_folds=N_FOLDS):

 train_valid_id = []
start = 0
size = len(dataset)
 split = size // n_folds
valid_size = split
for i in range(n_folds):
train_data = dataset.drop(dataset.index[start:split] ).index.values
valid_data = dataset.loc[start:split-1].index.values
train_valid_id.append(( train_data,valid_data))
start += valid_size
split += valid_size
return train_valid_id<categorify>
%%time
pred = NN_model.predict(x_test)
 del x_test
gc.collect()
train_valid = custom_folds(dataset=dataset )<normalization>
pred = pred.reshape(-1)
 pred =(pred + 1)/ 2
transform_train = A.Compose([
A.ShiftScaleRotate(shift_limit=0.1, scale_limit=0.1, rotate_limit=10),
A.Normalize(mean=(0.485,), std=(0.229,)) ,
ToTensor() ,
])
 transform_valid = A.Compose([
A.Normalize(mean=(0.485,), std=(0.229,)) ,
ToTensor() ,
] )<create_dataframe>
df_test = pd.read_csv(INPUT_DIR + 'test_V2.csv' )
train_data = DatasetMNIST(dataset, augmentations=transform_train)
 valid_data = DatasetMNIST(dataset, augmentations=transform_valid)
 train_valid_loaders = []
for i in train_valid:
train_idx, valid_idx = i
train_sampler = SubsetRandomSampler(train_idx)
 valid_sampler = SubsetRandomSampler(valid_idx)
 train_loader = torch.utils.data.DataLoader(train_data, batch_size=BATCH_SIZE, sampler=train_sampler)
 valid_loader = torch.utils.data.DataLoader(valid_data, batch_size=BATCH_SIZE, sampler=valid_sampler)
 train_valid_loaders.append(( train_loader,valid_loader))<define_search_model>
%%time
print("fix winPlacePerc")
 for i in range(len(df_test)) :
winPlacePerc = pred[i]
maxPlace = int(df_test.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
pred[i] = winPlacePerc
class Net(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=3)
 self.bn1 = nn.BatchNorm2d(32)
 self.conv2 = nn.Conv2d(32, 32, kernel_size=3)
 self.bn2 = nn.BatchNorm2d(32)
 self.conv3 = nn.Conv2d(32, 32, kernel_size=5, stride=2, padding=2)
 self.bn3 = nn.BatchNorm2d(32)
 self.conv4 = nn.Conv2d(32, 64, kernel_size=3)
 self.bn4 = nn.BatchNorm2d(64)
 self.conv5 = nn.Conv2d(64, 64, kernel_size=3)
 self.bn5 = nn.BatchNorm2d(64)
 self.conv6 = nn.Conv2d(64, 64, kernel_size=5, stride=2, padding=2)
 self.bn6 = nn.BatchNorm2d(64)
 self.conv7 = nn.Conv2d(64, 128, kernel_size=4)
 self.bn7 = nn.BatchNorm2d(128)
 self.lin1 = nn.Linear(128,10)
 def forward(self, xb):
x = xb.view(-1, 1, 28, 28)
 x = self.bn1(F.relu(self.conv1(x)))
 x = self.bn2(F.relu(self.conv2(x)))
 x = self.bn3(F.relu(self.conv3(x)))
 x = F.dropout2d(x, 0.25)
 x = self.bn4(F.relu(self.conv4(x)))
 x = self.bn5(F.relu(self.conv5(x)))
 x = self.bn6(F.relu(self.conv6(x)))
 x = F.dropout2d(x, 0.25)
 x = self.bn7(F.relu(self.conv7(x)))
 x = torch.flatten(x, start_dim=1)
 x = F.dropout2d(x, 0.25)
 x = self.lin1(x)
 x = F.softmax(x, dim=1)
 return x
model = Net()
print(model)
 if train_on_gpu:
model.cuda()<load_from_csv>
df_test['winPlacePerc'] = pred
<load_from_csv><EOS> 
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )