Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
rnf_classifier= RandomForestClassifier(n_estimators=100, n_jobs=-1)
 rnf_classifier.fit(X_train,y_train )<predict_on_test>
train=pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/train_V2.csv')
 test=pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/test_V2.csv')
 sample_submission=pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/sample_submission_V2.csv')
y_pred=rnf_classifier.predict(X_valid)
 accuracy_score(y_pred, y_valid )<groupby>
train.isnull().sum()
df_test.dtypes
g_train =df_test.columns.to_series().groupby(df_test.dtypes ).groups
g_train<define_variables>
train.isnull().sum()
cat_names= [
'Name', 'Sex', 'Ticket', 'Cabin',
'Embarked', 'Name_wiki', 'Hometown',
'Boarded', 'Destination', 'Lifeboat',
'Body'
]
cont_names = [
'PassengerId', 'Pclass', 'SibSp', 'Parch',
'Age', 'Fare', 'WikiId', 'Age_wiki','Class'
]<create_dataframe>
train.dropna(axis=0,inplace=True )
test = TabularPandas(df_test, procs=[Categorify, FillMissing,Normalize],
cat_names = cat_names,
cont_names = cont_names,
)<prepare_x_and_y>
test.isnull().sum()
X_test= test.train.xs<groupby>
test.isnull().sum()
X_test.dtypes
g_train =X_test.columns.to_series().groupby(X_test.dtypes ).groups
g_train<drop_column>
plt.figure(figsize=(10,6))
sns.distplot(train["DBNOs"],hist=True)
 plt.show()
X_test= X_test.drop('Fare_na', axis=1 )<predict_on_test>
train['boosts+heals'] = train['boosts']+train['heals']
train['matchDuration_min'] = train['matchDuration']/60
train['teamwork'] = train['assists'] + train['revives']
train['revives-teamKills'] = train['revives'] - train['teamKills']
train['total_distance'] = train['swimDistance'] + train['rideDistance'] + train['walkDistance']
train['headshotKills/kills'] = train['headshotKills'] / train['kills']
train['headshotKills/kills'].fillna(0, inplace=True)
 train['headshotKills/kills'].replace(np.inf, 0, inplace=True)
 train['killPlace/maxPlace'] = train['killPlace'] / train['maxPlace']
train['killPlace/maxPlace'].fillna(0, inplace=True)
 train['killPlace/maxPlace'].replace(np.inf, 0, inplace=True)
 train['walkDistance/heals'] = train['walkDistance'] / train['heals']
train['walkDistance/heals'].fillna(0, inplace=True)
 train['walkDistance/heals'].replace(np.inf, 0, inplace=True)
 train['walkDistance/kills'] = train['walkDistance'] / train['kills']
train['walkDistance/kills'].fillna(0, inplace=True)
 train['walkDistance/kills'].replace(np.inf, 0, inplace=True)
 train['killStreaks/kills'] = train['killStreaks'] / train['kills']
train['walkDistance/kills'].fillna(0, inplace=True)
 train['walkDistance/kills'].replace(np.inf, 0, inplace=True)
 train['total_distance/weaponsAcquired'] = train['total_distance'] / train['weaponsAcquired']
train['total_distance/weaponsAcquired'].fillna(0, inplace=True)
 train['total_distance/weaponsAcquired'].replace(np.inf, 0, inplace=True)
 train['heals/walkDistance'] = train['heals'] /train['walkDistance']
train['heals/walkDistance'].fillna(0, inplace=True)
 train['heals/walkDistance'].replace(np.inf, 0, inplace=True)
 train['kills/walkDistance'] = train['kills'] / train['walkDistance']
train['kills/walkDistance'].fillna(0, inplace=True)
 train['kills/walkDistance'].replace(np.inf, 0, inplace=True)
 train['killPlace/kills'] = train['killPlace'] / train['kills']
train['killPlace/kills'].fillna(0, inplace=True)
 train['killPlace/kills'].replace(np.inf, 0, inplace=True)
 train['walkDistance/matchDuration'] = train["walkDistance"] / train["matchDuration"]
train['walkDistance/matchDuration'].fillna(0, inplace=True)
 train['walkDistance/matchDuration'].replace(np.inf, 0, inplace=True)
y_pred=rnf_classifier.predict(X_test)
<data_type_conversions>
test['boosts+heals'] = test['boosts']+test['heals']
test['matchDuration_min'] = test['matchDuration']/60
test['teamwork'] = test['assists'] + test['revives']
test['revives-teamKills'] = test['revives'] - test['teamKills']
test['total_distance'] = test['swimDistance'] + test['rideDistance'] + test['walkDistance']
test['headshotKills/kills'] = test['headshotKills'] / test['kills']
test['headshotKills/kills'].fillna(0, inplace=True)
 test['headshotKills/kills'].replace(np.inf, 0, inplace=True)
 test['killPlace/maxPlace'] = test['killPlace'] / test['maxPlace']
test['killPlace/maxPlace'].fillna(0, inplace=True)
 test['killPlace/maxPlace'].replace(np.inf, 0, inplace=True)
 test['walkDistance/heals'] = test['walkDistance'] / test['heals']
test['walkDistance/heals'].fillna(0, inplace=True)
 test['walkDistance/heals'].replace(np.inf, 0, inplace=True)
 test['walkDistance/kills'] = test['walkDistance'] / test['kills']
test['walkDistance/kills'].fillna(0, inplace=True)
 test['walkDistance/kills'].replace(np.inf, 0, inplace=True)
 test['killStreaks/kills'] = test['killStreaks'] / test['kills']
test['walkDistance/kills'].fillna(0, inplace=True)
 test['walkDistance/kills'].replace(np.inf, 0, inplace=True)
 test['total_distance/weaponsAcquired'] = test['total_distance'] / test['weaponsAcquired']
test['total_distance/weaponsAcquired'].fillna(0, inplace=True)
 test['total_distance/weaponsAcquired'].replace(np.inf, 0, inplace=True)
 test['heals/walkDistance'] = test['heals'] /test['walkDistance']
test['heals/walkDistance'].fillna(0, inplace=True)
 test['heals/walkDistance'].replace(np.inf, 0, inplace=True)
 test['kills/walkDistance'] = test['kills'] / test['walkDistance']
test['kills/walkDistance'].fillna(0, inplace=True)
 test['kills/walkDistance'].replace(np.inf, 0, inplace=True)
 test['killPlace/kills'] = test['killPlace'] / test['kills']
test['killPlace/kills'].fillna(0, inplace=True)
 test['killPlace/kills'].replace(np.inf, 0, inplace=True)
 test['walkDistance/matchDuration'] = test["walkDistance"] / test["matchDuration"]
test['walkDistance/matchDuration'].fillna(0, inplace=True)
 test['walkDistance/matchDuration'].replace(np.inf, 0, inplace=True)
y_pred= y_pred.astype(int )<save_to_csv>
dropped_cols = ["Id", "matchId", "groupId", "matchType"]
train.drop(dropped_cols,axis=1,inplace=True)
 test.drop(dropped_cols,axis=1,inplace=True )
output= pd.DataFrame({'PassengerId':df_test.PassengerId, 'Survived': y_pred})
 output.to_csv('my_submission_titanic.csv', index=False)
 output.head()<set_options>
X = train.drop('winPlacePerc',axis=1)
 y = train['winPlacePerc']
%matplotlib inline 
<load_from_csv>
test_size=0.20
seed=42
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=seed )
class TrainDataset(Dataset):
def __init__(self, file_path, transform=None):
self.data = pd.read_csv(file_path)
 self.transform = transform
def __len__(self):
return len(self.data)
 def __getitem__(self, index):
images = self.data.iloc[index, 1:].values.astype(np.uint8 ).reshape(( 28, 28, 1))
labels = self.data.iloc[index, 0]
if self.transform is not None:
images = self.transform(images)
 return images, labels
transform = transforms.ToTensor()
train_data = TrainDataset('.. /input/digit-recognizer/train.csv', transform=transform)
 valid_size = 0.15
num_train = len(train_data)
 indices = list(range(num_train))
torch.manual_seed(0)
 np.random.shuffle(indices)
 split = int(np.floor(valid_size * num_train))
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
 valid_sampler = SubsetRandomSampler(valid_idx)
 batch_size = 20
train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, sampler=train_sampler)
 valid_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, sampler=valid_sampler)
 class TestDataset(Dataset):
def __init__(self, file_path, transform=None):
self.data = pd.read_csv(file_path)
 self.transform = transform
def __len__(self):
return len(self.data)
 def __getitem__(self, index):
images = self.data.iloc[index, :].values.astype(np.uint8 ).reshape(( 28, 28, 1))
if self.transform is not None:
images = self.transform(images)
 return images
test_data = TestDataset('.. /input/digit-recognizer/test.csv', transform=transform)
 test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, shuffle=False )<choose_model_class>
del train
gc.collect()
class Net(nn.Module):
def __init__(self):
super(Net, self ).__init__()
self.conv1 = nn.Conv2d(1, 64, kernel_size=3, stride=1, padding=1)
 self.conv2 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
 self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
 hidden_1 = 1024
hidden_2 = 512
self.fc1 = nn.Linear(128*7*7, hidden_1)
 self.fc2 = nn.Linear(hidden_1, hidden_2)
 self.fc3 = nn.Linear(hidden_2, 10)
 self.dropout = nn.Dropout(p=0.5)
 def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
 x = self.pool(F.relu(self.conv2(x)))
 x = x.view(-1, 128*7*7)
 x = self.dropout(x)
 x = F.relu(self.fc1(x))
x = self.dropout(x)
 x = F.relu(self.fc2(x))
x = self.dropout(x)
 x = self.fc3(x)
 return x<choose_model_class>
params2 = {
"objective" : "regression",
"metric" : "mae",
"num_leaves" : 150,
"learning_rate" : 0.03,
"bagging_fraction" : 0.9,
"bagging_seed" : 0,
"num_threads" : 4,
"colsample_bytree" : 0.5,
'min_data_in_leaf':1900,
'lambda_l2':9
}
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 model = Net()
criterion = nn.CrossEntropyLoss()
lr = 0.001
optimizer = optim.Adam(model.parameters() , lr = lr)
 model.to(device )<train_model>
reg2 = lgb.LGBMRegressor(**params2, n_estimators=2000 )
t0 = time.time()
n_epochs = 50
valid_loss_min = np.Inf
for epoch in range(n_epochs):
train_loss = 0.0
model.train()
for data, target in train_loader:
data, target = data.to(device),target.to(device)
 optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
train_loss += loss.item() *data.size(0)
 valid_loss = 0.0
model.eval()
with torch.no_grad() :
for data, target in valid_loader:
data, target = data.to(device),target.to(device)
 output = model(data)
 loss = criterion(output, target)
 valid_loss += loss.item() *data.size(0)
 train_loss = train_loss/len(train_loader.dataset)
 valid_loss = valid_loss/len(valid_loader.dataset)
 print("Epoch: {}/{}.. ".format(epoch+1, n_epochs),
"Training Loss: {:.6f}.. ".format(train_loss),
"Validation Loss: {:.6f}.. ".format(valid_loss))
if valid_loss <= valid_loss_min:
print('Validation loss decreased({:.6f} --> {:.6f} ).Saving model...'.format(
 valid_loss_min,
valid_loss))
torch.save(model.state_dict() , 'model.pt')
 valid_loss_min = valid_loss
print('Training and validation executed in {:.1f} minutes.'.format(( time.time() - t0)/60))<load_from_disk>
reg2.fit(X_train, y_train )
model.load_state_dict(torch.load('model.pt'))<predict_on_test>
pred2 = reg2.predict(X_test, num_iteration=reg2.best_iteration_ )
class_correct = list(0.for i in range(10))
class_total = list(0.for i in range(10))
model.eval()
with torch.no_grad() :
for data, target in valid_loader:
data, target = data.to(device),target.to(device)
 output = model(data)
 _, pred = torch.max(output, dim=1)
 correct = pred == target.view_as(pred)
 for i in range(len(target)) :
label = target.data[i]
class_correct[label] += correct[i].item()
class_total[label] += 1
for i in range(10):
if class_total[i] > 0:
print('Validation Accuracy of %5s: %.1f%%(%2d/%2d)' %(
 str(i), 100 * class_correct[i] / class_total[i],
np.sum(class_correct[i]), np.sum(class_total[i])))
 else:
print('Validation Accuracy of %5s: N/A(no training examples)' %(classes[i]))
print('
Validation Accuracy(Overall): %.1f%%(%2d/%2d)' %(
 100.* np.sum(class_correct)/ np.sum(class_total),
np.sum(class_correct), np.sum(class_total)) )<prepare_output>
mean_absolute_error(y_test, pred2 )
test_preds = torch.LongTensor()
model.eval()
with torch.no_grad() :
for data in test_loader:
data, target = data.to(device),target.to(device)
 output = model(data)
 _, pred = torch.max(output, dim=1)
 test_preds = torch.cat(( test_preds.cpu() , pred.cpu()), dim=0)
 submission = pd.DataFrame({"ImageId":list(range(1, len(test_preds)+1)) ,
"Label":test_preds.numpy() })
 submission<save_to_csv>
predictions = reg2.predict(test, num_iteration=reg2.best_iteration_ )
submission.to_csv("my_submission.csv", index=False, header=True )<load_from_csv>
sample_submission['winPlacePerc'] = predictions
<load_from_url><EOS> 
sample_submission.to_csv('submission.csv',index=False )