Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<categorify>
warnings.filterwarnings('ignore')
 %matplotlib inline
py.init_notebook_mode(connected=True)
 print(os.listdir(".. /input"))
class MinibatchStdDev(th.nn.Module):

 def __init__(self):

 super(MinibatchStdDev, self ).__init__()
def forward(self, x, alpha=1e-8):

 batch_size, _, height, width = x.shape
y = x - x.mean(dim=0, keepdim=True)
 y = th.sqrt(y.pow(2.).mean(dim=0, keepdim=False)+ alpha)
 y = y.mean().view(1, 1, 1, 1)
 y = y.repeat(batch_size,1, height, width)
 y = th.cat([x, y], 1)
 return y 
<define_search_model>
df_train = reduce_mem_usage(pd.read_csv('.. /input/train_V2.csv'))
df_test = reduce_mem_usage(pd.read_csv('.. /input/test_V2.csv'))
class Generator(nn.Module):
def __init__(self, nz, nfeats, nchannels):
super(Generator, self ).__init__()
self.conv1 = spectral_norm(nn.ConvTranspose2d(nz, nfeats * 8, 4, 1, 0, bias=False))
self.conv2 = spectral_norm(nn.ConvTranspose2d(nfeats * 8, nfeats * 8, 4, 2, 1, bias=False))
self.conv3 = spectral_norm(nn.ConvTranspose2d(nfeats * 8, nfeats * 4, 4, 2, 1, bias=False))
self.conv4 = spectral_norm(nn.ConvTranspose2d(nfeats * 4, nfeats * 2, 4, 2, 1, bias=False))
self.conv5 = spectral_norm(nn.ConvTranspose2d(nfeats * 2, nfeats, 4, 2, 1, bias=False))
self.conv6 = spectral_norm(nn.ConvTranspose2d(nfeats, nchannels, 3, 1, 1, bias=False))
self.pixnorm = PixelwiseNorm()
def forward(self, x):
x = F.leaky_relu(self.conv1(x))
x = self.conv2(x)
 x = F.leaky_relu(self.pixnorm(x))
x = self.conv3(x)
 x = F.leaky_relu(self.pixnorm(x))
x = self.conv4(x)
 x = F.leaky_relu(self.pixnorm(x))
x = self.conv5(x)
 x = F.leaky_relu(self.pixnorm(x))
x = torch.tanh(self.conv6(x))
return x
class Discriminator(nn.Module):
def __init__(self, nchannels, nfeats):
super(Discriminator, self ).__init__()
self.conv1 = nn.Conv2d(nchannels, nfeats, 4, 2, 1, bias=False)
 self.conv2 = spectral_norm(nn.Conv2d(nfeats, nfeats * 2, 4, 2, 1, bias=False))
self.bn2 = nn.BatchNorm2d(nfeats * 2)
 self.conv3 = spectral_norm(nn.Conv2d(nfeats * 2, nfeats * 4, 4, 2, 1, bias=False))
self.bn3 = nn.BatchNorm2d(nfeats * 4)
 self.conv4 = spectral_norm(nn.Conv2d(nfeats * 4, nfeats * 8, 4, 2, 1, bias=False))
self.bn4 = nn.MaxPool2d(2)
 self.batch_discriminator = MinibatchStdDev()
self.pixnorm = PixelwiseNorm()
self.conv5 = spectral_norm(nn.Conv2d(nfeats * 8 +1, 1, 2, 1, 0, bias=False))
def forward(self, x):
x = F.leaky_relu(self.conv1(x), 0.2)
 x = F.leaky_relu(self.bn2(self.conv2(x)) , 0.2)
 x = F.leaky_relu(self.bn3(self.conv3(x)) , 0.2)
 x = F.leaky_relu(self.bn4(self.conv4(x)) , 0.2)
 x = self.batch_discriminator(x)
 x = torch.sigmoid(self.conv5(x))
return x.view(-1, 1)
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 lr = 0.0002
lr_d = 0.0002
beta1 = 0.5
epochs = 1600
netG = Generator(100, 32, 3 ).to(device)
 netD = Discriminator(3, 48 ).to(device)
 criterion = nn.BCELoss()
optimizerD = optim.Adam(netD.parameters() , lr=lr, betas=(beta1, 0.999))
optimizerG = optim.Adam(netG.parameters() , lr=lr_d, betas=(beta1, 0.999))
lr_schedulerG = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizerG,
T_0=epochs//200, eta_min=0.00005)
 lr_schedulerD = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizerD,
T_0=epochs//200, eta_min=0.00005)
 nz = 100
fixed_noise = torch.randn(25, nz, 1, 1, device=device)
 real_label = 0.7
fake_label = 0.0
batch_size = train_loader.batch_size
step = 0
for epoch in range(epochs):
for ii,(real_images)in tqdm(enumerate(train_loader), total=len(train_loader)) :
end = time()
if(end -start)> 15000 :
break
netD.zero_grad()
real_images = real_images.to(device)
 batch_size = real_images.size(0)
 labels = torch.full(( batch_size, 1), real_label, device=device)+ np.random.uniform(-0.1, 0.1)
 output = netD(real_images)
 errD_real = criterion(output, labels)
 errD_real.backward()
D_x = output.mean().item()
noise = torch.randn(batch_size, nz, 1, 1, device=device)
 fake = netG(noise)
 labels.fill_(fake_label)+ np.random.uniform(0, 0.2)
 output = netD(fake.detach())
 errD_fake = criterion(output, labels)
 errD_fake.backward()
D_G_z1 = output.mean().item()
errD = errD_real + errD_fake
optimizerD.step()
if step % 5 != 0:
netG.zero_grad()
labels.fill_(real_label)
 output = netD(fake)
 errG = criterion(output, labels)
 errG.backward()
D_G_z2 = output.mean().item()
optimizerG.step()
if step % 5 == 0:
netG.zero_grad()
labels.fill_(fake_label)
 output = netD(fake)
 errG = criterion(output, labels)
 errG.backward()
D_G_z2 = output.mean().item()
optimizerG.step()
if step % 500 == 0:
print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f D(x): %.4f D(G(z)) : %.4f / %.4f'
%(epoch + 1, epochs, ii, len(train_loader),
errD.item() , errG.item() , D_x, D_G_z1, D_G_z2))
valid_image = netG(fixed_noise)
 step += 1
lr_schedulerG.step(epoch)
 lr_schedulerD.step(epoch)
 if epoch % 200 == 0:
show_generated_img()
def truncated_normal(size, threshold=1):
values = truncnorm.rvs(-threshold, threshold, size=size)
 return values
if not os.path.exists('.. /output_images'):
os.mkdir('.. /output_images')
 im_batch_size = 100
n_images=10000
for i_batch in range(0, n_images, im_batch_size):
z = truncated_normal(( im_batch_size, 100, 1, 1), threshold=1)
 gen_z = torch.from_numpy(z ).float().to(device)
 gen_images = netG(gen_z)
 images = gen_images.to("cpu" ).clone().detach()
images = images.numpy().transpose(0, 2, 3, 1)
 for i_image in range(gen_images.size(0)) :
save_image(( gen_images[i_image, :, :, :] +1.0)/2.0, os.path.join('.. /output_images', f'image_{i_batch+i_image:05d}.png'))
shutil.make_archive('images', 'zip', '.. /output_images' )<load_from_csv>
print('train : {}'.format(df_train.shape))
print('test : {}'.format(df_test.shape))
Train = pd.read_csv(".. /input/train.csv")
 Test = pd.read_csv(".. /input/test.csv" )<count_values>
total = df_train.isnull().sum().sort_values(ascending=False)
 percent =(df_train.isnull().sum() / df_train.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head()
y_train = Train['label']
X_train = Train.drop(labels='label', axis=1)
 y_train.value_counts()<normalization>
total = df_test.isnull().sum().sort_values(ascending=False)
 percent =(df_test.isnull().sum() /df_test.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head()
X_train = X_train/255.0
Test = Test/255.0<categorify>
df_train.dropna(axis=0, inplace=True )
y_train = to_categorical(y_train, num_classes = 10 )<split>
cm = np.corrcoef(df_train[cols].values.T)
X_train,X_val,Y_train,Y_val =train_test_split(X_train, y_train, test_size=0.1, random_state=0 )<train_model>
headshot = df_train[['kills', 'winPlacePerc', 'headshotKills']]
headshot['headshotrate'] = headshot['headshotKills'] / headshot['kills']
headshot.corr()
datagen = ImageDataGenerator(rotation_range = 15, width_shift_range = 0.1, height_shift_range = 0.1)
 datagen.fit(X_train)
 np.concatenate(( X_train,X_train),axis=0)
 random.seed(12345)
 for X_batch, Y_batch in datagen.flow(np.concatenate(( X_train,X_train),axis=0), np.concatenate(( Y_train,Y_train),axis=0), batch_size=35700):
break
X_train_aug = X_batch
Y_train_aug = Y_batch
for i in range(0, 9):
pyplot.subplot(330 + 1 + i)
 pyplot.imshow(X_train_aug[i].reshape(28, 28), cmap=pyplot.get_cmap('gray'))
pyplot.show()<concatenate>
df_train['headshotrate'] = df_train['headshotKills']/df_train['kills']
df_test['headshotrate'] = df_test['headshotKills']/df_test['kills']
del headshot
X_train_aug = np.concatenate(( X_train,X_train_aug),axis=0)
 Y_train_aug = np.concatenate(( Y_train,Y_train_aug),axis=0)
 print(X_train_aug.shape,Y_train_aug.shape )<choose_model_class>
killStreak = df_train[['kills','winPlacePerc','killStreaks']]
killStreak['killStreakrate'] = killStreak['killStreaks']/killStreak['kills']
killStreak.corr()
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>
df_train['killStreakrate'] = -(df_train['killStreaks'] / df_train['kills'])
 df_test['killStreakrate'] = -(df_test['killStreaks'] / df_test['kills'])
 del killStreak
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )<choose_model_class>
df_train['hacker_pt'] = 0
df_test['hacker_pt'] = 0
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
<choose_model_class>
pd.set_option('display.max_columns', 50 )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<define_variables>
df_train['total_Distance'] = df_train['rideDistance'] +df_train['walkDistance'] + df_train['swimDistance']
df_test['total_Distance'] = df_test['rideDistance'] + df_test['walkDistance'] + df_test['swimDistance']
df_train[(df_train['winPlacePerc'] == 1)&(df_train['total_Distance'] < 100)].head()
epochs = 10
batch_size = 86<train_model>
df_train['headshotrate'] = df_train['headshotrate'].fillna(0)
 df_train['killStreakrate'] = df_train['killStreakrate'].fillna(0)
 df_test['headshotrate'] = df_test['headshotrate'].fillna(0)
 df_test['killStreakrate'] = df_test['killStreakrate'].fillna(0)
history = model.fit(X_train_aug,
Y_train_aug,
batch_size = batch_size,
epochs = epochs,
validation_data =(X_val, Y_val),
verbose = 2,
callbacks=[learning_rate_reduction] )<predict_on_test>
df_train['hacker_pt'][(df_train['heals'] + df_train['boosts'] < 1)&(df_train['total_Distance'] < 100)&(df_train['kills'] > 20)] = 1
df_test['hacker_pt'][(df_test['heals'] + df_test['boosts'] < 1)&(df_test['total_Distance'] < 100)&(df_test['kills'] > 20)] = 1
result = model.predict(Test)
 result = np.argmax(result,axis = 1)
 result.shape<save_to_csv>
df_train['hacker_pt'][(df_train['kills'] > 10)&(df_train['weaponsAcquired'] >= 10)&(df_train['total_Distance'] == 0)] += 1
df_test['hacker_pt'][(df_test['kills'] > 10)&(df_test['weaponsAcquired'] >= 10)&(df_test['total_Distance'] == 0)] += 1
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )<import_modules>
df_train['hacker_pt'][df_train['longestKill'] >= 1000] += 1
df_test['hacker_pt'][df_train['longestKill'] >= 1000] += 1
import math, time, random, datetime<set_options>
df_train.sort_values('hacker_pt', ascending=False ).head()
%matplotlib inline
plt.style.use('seaborn-whitegrid')
 warnings.filterwarnings('ignore' )<load_from_csv>
kills = df_train[['assists','winPlacePerc','kills']]
kills['kills_assists'] =(kills['kills'] + kills['assists'])
 kills.corr()
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv' )<prepare_x_and_y>
df_train['kills_assists'] = df_train['kills'] + df_train['assists']
df_test['kills_assists'] = df_test['kills'] + df_test['assists']
del df_train['kills']
del df_test['kills']
del df_train['assists']
del df_test['assists']
del kills
ntrain = train.shape[0]
ntest = test.shape[0]
y_train = train['Survived'].values
passId = test['PassengerId']
data = pd.concat(( train, test))
print("data size is: {}".format(data.shape))<count_missing_values>
df_train = reduce_mem_usage(df_train)
 df_test = reduce_mem_usage(df_test)
 gc.collect()
data.isnull().sum()<count_missing_values>
del missing_data
del percent
del total
gc.collect()
data.Age.isnull().any()<groupby>
df_train_size = df_train.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_test_size = df_test.groupby(['matchId','groupId'] ).size().reset_index(name='group_size' )
train.groupby(['Pclass','Survived'])['Survived'].count()<groupby>
df_train_mean = df_train.groupby(['matchId','groupId'] ).mean().reset_index()
df_test_mean = df_test.groupby(['matchId','groupId'] ).mean().reset_index()
train.groupby('Pclass' ).Survived.mean()<count_values>
df_train_match_mean = df_train.groupby(['matchId'] ).mean().reset_index()
df_test_match_mean = df_test.groupby(['matchId'] ).mean().reset_index()
data.Name.value_counts()<feature_engineering>
df_train = pd.merge(df_train, df_train_mean, suffixes=["", "_mean"], how='left', on=['matchId', 'groupId'])
 df_test = pd.merge(df_test, df_test_mean, suffixes=["", "_mean"], how='left', on=['matchId', 'groupId'])
 del df_train_mean
del df_test_mean
temp = data.copy()
temp['Initial']=0
for i in train:
temp['Initial']=data.Name.str.extract('([A-Za-z]+)\.' )<groupby>
df_train = pd.merge(df_train, df_train_match_mean, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 df_test = pd.merge(df_test, df_test_match_mean, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 del df_train_match_mean
del df_test_match_mean
def survpct(a):
return temp.groupby(a ).Survived.mean()
survpct('Initial' )<groupby>
df_train = pd.merge(df_train, df_train_size, how='left', on=['matchId', 'groupId'])
 df_test = pd.merge(df_test, df_test_size, how='left', on=['matchId', 'groupId'])
 del df_train_size
del df_test_size
temp.groupby('Initial')['Age'].mean()<feature_engineering>
gc.collect()
df_train = reduce_mem_usage(df_train)
 df_test = reduce_mem_usage(df_test)
 gc.collect()
temp['Newage']=temp['Age']
def newage(k,n):
temp.loc[(temp.Age.isnull())&(temp.Initial==k),'Newage']= n
newage('Capt',int(70.000000))
newage('Col',int(54.000000))
newage('Countess',int(33.000000))
newage('Don',int(40.000000))
newage('Dona',int(39.000000))
newage('Dr',int(43.571429))
newage('Jonkheer',int(38.000000))
newage('Lady',int(48.000000))
newage('Major',int(48.500000))
newage('Master',int(5.482642))
newage('Miss',int(21.774238))
newage('Mlle',int(24.000000))
newage('Mme',int(24.000000))
newage('Mr',int(32.252151))
newage('Mrs',int(36.994118))
newage('Ms',int(28.000000))
newage('Rev',int(41.250000))
newage('Sir',int(49.000000)) 
<groupby>
warnings.filterwarnings("ignore")
 color = sns.color_palette()
groupmean('Age_Range', 'Survived' )<feature_engineering>
train_columns = list(df_test.columns)
 train_idx = df_train.Id
test_idx = df_test.Id
train_columns.remove("Id")
 train_columns.remove("matchId")
 train_columns.remove("groupId" )
temp['Gender']= temp['Sex']
for n in range(1,4):
temp.loc[(temp['Sex'] == 'male')&(temp['Pclass'] == n),'Gender']= 'm'+str(n)
 temp.loc[(temp['Sex'] == 'female')&(temp['Pclass'] == n),'Gender']= 'w'+str(n)
 temp.loc[(temp['Gender'] == 'm3'),'Gender']= 'm2'
temp.loc[(temp['Gender'] == 'w3'),'Gender']= 'w2'
temp.loc[(temp['Age'] <= 1.0),'Gender']= 'baby'
temp.loc[(temp['Age'] > 75.0),'Gender']= 'old' 
<groupby>
x_train = df_train[train_columns]
x_test = df_test[train_columns]
y_train = df_train["winPlacePerc"].astype('float' )
groupmean('Gender', 'Survived' )<feature_engineering>
encoded_train = pd.get_dummies(x_train.matchType, prefix=x_train.matchType.name ,prefix_sep="_")
 encoded_test = pd.get_dummies(x_test.matchType, prefix=x_test.matchType.name ,prefix_sep="_")
 encoded_train.head()
temp['Agroup']=0
temp.loc[temp['Newage']<1.0,'Agroup']= 1
temp.loc[(temp['Newage']>=1.0)&(temp['Newage']<=3.0),'Agroup']= 2
temp.loc[(temp['Newage']>3.0)&(temp['Newage']<11.0),'Agroup']= 7
temp.loc[(temp['Newage']>=11.0)&(temp['Newage']<15.0),'Agroup']= 13
temp.loc[(temp['Newage']>=15.0)&(temp['Newage']<18.0),'Agroup']= 16
temp.loc[(temp['Newage']>=18.0)&(temp['Newage']<= 20.0),'Agroup']= 18
temp.loc[(temp['Newage']> 20.0)&(temp['Newage']<=22.0),'Agroup']= 21
temp.loc[(temp['Newage']>22.0)&(temp['Newage']<=26.0),'Agroup']= 24
temp.loc[(temp['Newage']>26.0)&(temp['Newage']<=30.0),'Agroup']= 28
temp.loc[(temp['Newage']>30.0)&(temp['Newage']<=32.0),'Agroup']= 31
temp.loc[(temp['Newage']>32.0)&(temp['Newage']<=34.0),'Agroup']= 33
temp.loc[(temp['Newage']>34.0)&(temp['Newage']<=38.0),'Agroup']= 36
temp.loc[(temp['Newage']>38.0)&(temp['Newage']<=52.0),'Agroup']= 45
temp.loc[(temp['Newage']>52.0)&(temp['Newage']<=75.0),'Agroup']= 60
temp.loc[temp['Newage']>75.0,'Agroup']= 78<groupby>
x_train = x_train.merge(encoded_train, right_index=True, left_index=True)
 x_test = x_test.merge(encoded_test, right_index=True, left_index=True )
groupmean('Agroup', 'Survived' )<groupby>
del x_train['matchType']
del x_test['matchType']
groupmean('Agroup', 'Age' )<feature_engineering>
del df_train; del df_test
gc.collect()
temp['Alone']=0
temp.loc[(temp['SibSp']==0)&(temp['Parch']==0),'Alone']= 1<feature_engineering>
folds = KFold(n_splits=3,random_state=6)
 oof_preds = np.zeros(x_train.shape[0])
 sub_preds = np.zeros(x_test.shape[0])
 start = time.time()
valid_score = 0
importances = pd.DataFrame()
for n_fold,(trn_idx, val_idx)in enumerate(folds.split(x_train, y_train)) :
trn_x, trn_y = x_train.iloc[trn_idx], y_train[trn_idx]
val_x, val_y = x_train.iloc[val_idx], y_train[val_idx]
train_data = lgb.Dataset(data=trn_x, label=trn_y)
 valid_data = lgb.Dataset(data=val_x, label=val_y)
 params = {"objective" : "regression", "metric" : "mae", 'n_estimators':10000, 'early_stopping_rounds':100,
"num_leaves" : 30, "learning_rate" : 0.3, "bagging_fraction" : 0.9,
"bagging_seed" : 0}
lgb_model = lgb.train(params, train_data, valid_sets=[train_data, valid_data], verbose_eval=1000)
 oof_preds[val_idx] = lgb_model.predict(val_x, num_iteration=lgb_model.best_iteration)
 oof_preds[oof_preds>1] = 1
oof_preds[oof_preds<0] = 0
sub_pred = lgb_model.predict(x_test, num_iteration=lgb_model.best_iteration)/ folds.n_splits
sub_pred[sub_pred>1] = 1
sub_pred[sub_pred<0] = 0
sub_preds += sub_pred
print('Fold %2d RMSE : %.6f' %(n_fold + 1, mean_absolute_error(val_y, oof_preds[val_idx])))
 valid_score += mean_absolute_error(val_y, oof_preds[val_idx] )
temp['Family']=0
for i in temp:
temp['Family'] = temp['Parch'] + temp['SibSp'] +1<drop_column>
print('Done' )
bag('Parch','Survived','Survived per Parch','Parch Survived vs Not Survived' )<count_missing_values>
test_pred = pd.DataFrame({"Id":test_idx})
 test_pred["winPlacePerc"] = sub_preds
test_pred.columns = ["Id", "winPlacePerc"]
test_pred.to_csv("lgb_base_model.csv", index=False )
temp.Ticket.isnull().any()<feature_engineering>
print('Done' )