Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
test_data = test_data/255<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')
 target = train['winPlacePerc']
train.drop(['winPlacePerc'], axis=1, inplace=True)
 merge = pd.concat([train, test], axis=0 ).reset_index()
train_index = list(range(len(train)))
 test_index = list(range(len(train), len(merge)) )
predictions = model.predict(test_data )<prepare_output>
merge[merge["kills"] > 35]["kills"] = 35
merge[merge["DBNOs"] > 20]["DBNOs"] = 20
merge[merge["assists"] > 10]["assists"] = 10
merge[merge["walkDistance"] > 10000]["walkDistance"] = 10000
merge[merge["boosts"] > 15]["boosts"] = 15
merge[merge["heals"] > 30]["heals"] = 30
merge[merge["weaponsAcquired"] > 50]["weaponsAcquired"] = 50
merge[merge["damageDealt"] > 2000]["damageDealt"] = 2000
merge[merge["longestKill"] > 500]["longestKill"] = 500
merge[merge["killStreaks"] > 10]["killStreaks"] = 10
merge[merge["rideDistance"] > 15000]["rideDistance"] = 15000
merge[merge["headshotKills"] > 15]["headshotKills"] = 15
predictions = np.argmax(predictions, axis=1, out=None )<save_to_csv>
def rstr(df, pred=None):
obs = df.shape[0]
types = df.dtypes
counts = df.apply(lambda x: x.count())
 uniques = df.apply(lambda x: [x.unique() ])
 nulls = df.apply(lambda x: x.isnull().sum())
 distincts = df.apply(lambda x: x.unique().shape[0])
 missing_ration =(df.isnull().sum() /obs)*100
skewness = df.skew()
kurtosis = df.kurt()
print('Data shape: ', df.shape)
 if pred is None:
cols = ['types', 'counts', 'distincts', 'nulls', 'missing ration', 'uniques', 'skewness', 'kurtosis', 'corr']
str = pd.concat([types, counts, distincts, nulls, missing_ration, uniques, skewness, kurtosis], axis=1)
 else:
corr = df.corr() [pred]
str =pd.concat([types, counts, distincts, nulls, missing_ration, uniques, skewness, kurtosis, corr], axis=1, sort=False)
 corr_col = 'corr ' + pred
cols = ['types', 'counts', 'distincts', 'nulls', 'missing ration', 'uniques', 'skewness', 'kurtosis', corr_col]
str.columns = cols
dtypes = str.types.value_counts()
print('___________________________
Data types:
',str.types.value_counts())
 print('___________________________')
 return str
with open("resultCNNwithPrepros.csv", "wb")as f:
f.write(b'ImageId,Label
')
 np.savetxt(f, np.hstack([(np.array(range(28000)) +1 ).reshape(-1,1), predictions.astype(int ).reshape(-1,1)]), fmt='%i', delimiter="," )<set_options>
matchType = pd.get_dummies(merge.matchType )
%matplotlib inline 
<define_variables>
merge = pd.concat([merge,matchType], axis=1 )
base = Path('.. /input' )<load_from_csv>
merge["group_size"] = merge.groupby(["matchId"])['groupId'].transform('count')
 merge["group_per"] = merge["maxPlace"]/merge["group_size"]
data_df = pd.read_csv(base/'train.csv')
 data_df.head()<drop_column>
merge["distanceSum"] = 0.85*merge['walkDistance'] + 0.14*merge['rideDistance'] + 0.01*merge['swimDistance']
merge["distanceRank"] = merge.groupby(["matchId"])["distanceSum"].rank(pct=True)
 merge.drop("distanceSum", axis=1, inplace=True )
trn_df = data_df.drop(val_df.index)
 trn_df.shape<prepare_x_and_y>
merge["killPlace"] = merge["killPlace"]-merge["group_per"]
merge["killPlace"] = -1*merge["killPlace"]/merge["group_size"]
trn_x, trn_y = trn_df.loc[:, 'pixel0':'pixel783'], trn_df['label']
val_x, val_y = val_df.loc[:, 'pixel0':'pixel783'], val_df['label']<concatenate>
merge["DBNOsNassists"] = merge["DBNOs"]+merge["assists"]
merge["items"] = merge["boosts"]+merge["heals"]
merge["itemsRank"] = merge.groupby(["matchId"])["items"].rank(pct=True)
 merge["damageDealtRank"] = merge.groupby(["matchId"])["damageDealt"].rank(pct=True)
 merge["damageDealtPerkills"] = merge["damageDealt"]/(merge["kills"]+1)
 merge["killStreaksPerkills"] = merge["killStreaks"] /(merge["kills"]+1)
 merge["headshotPerKills"] = merge["headshotKills"]/(merge["kills"]+1)
 merge["weaponsAcqRank"] = merge.groupby(["matchId"])["weaponsAcquired"].rank(pct=True)
 merge["longestKillRank"] = merge.groupby(["matchId"])["longestKill"].rank(pct=True)
 merge["DBNOsNassistsRank"] = merge.groupby(["matchId"])["DBNOsNassists"].rank(pct=True)
 merge["revivesRank"] = merge.groupby(["matchId"])["revives"].rank(pct=True)
 merge.drop(["boosts", "heals", "assists", "damageDealt", "killStreaks", "headshotKills", "weaponsAcquired", "longestKill", "DBNOs", "revives"],
axis=1, inplace=True )
def reshape(dt_x, dt_y):
dt_x = np.array(dt_x, dtype = np.uint8 ).reshape(-1,28,28)
 dt_x = np.stack(( dt_x,)*3, axis=-1)
 dt_y = np.array(dt_y)
 return dt_x, dt_y<prepare_x_and_y>
merge["itemsRank"] = merge.groupby(["groupId"])["items"].transform(np.mean)
 merge["damageDealtRank"] = merge.groupby(["groupId"])["damageDealtRank"].transform(np.mean)
 merge["damageDealtPerkills"] = merge.groupby(["groupId"])["damageDealtPerkills"].transform(np.mean)
 merge["killStreaksPerkills"] = merge.groupby(["groupId"])["killStreaksPerkills"].transform(np.mean)
 merge["headshotPerKills"] = merge.groupby(["groupId"])["headshotPerKills"].transform(np.mean)
 merge["weaponsAcqRank"] = merge.groupby(["groupId"])["weaponsAcqRank"].transform(np.mean)
 merge["longestKillRank"] = merge.groupby(["groupId"])["longestKillRank"].transform(np.mean)
 merge["DBNOsNassistsRank"] = merge.groupby(["groupId"])["DBNOsNassistsRank"].transform(np.mean)
 merge["revivesRank"] = merge.groupby(["groupId"])["revivesRank"].transform(np.mean)
 merge["distanceRank"] = merge.groupby(["groupId"])["distanceRank"].transform(np.mean)
 merge["killStreaksPerkillsRank"] = merge.groupby(["matchId"])["killStreaksPerkills"].rank(pct=True)
 merge["headshotPerKillsRank"] = merge.groupby(["matchiId"])["headshotPerKills"].rank(pct=True )
trn_x, trn_y = reshape(trn_x, trn_y)
 val_x, val_y = reshape(val_x, val_y )<save_model>
train=Path('.. /working/data/train')
 save(train, trn_x, trn_y)
 valid = Path('.. /working/data/valid')
 save(valid, val_x, val_y )<categorify>
merge_id = merge['Id']
merge.drop(['Id','groupId','matchId','numGroups','matchType'], axis=1, inplace=True )
path = Path('.. /working/data/')
 data =(ImageList.from_folder(path)
.split_by_folder(train='train', valid='valid')
.label_from_folder() 
.transform(get_transforms(do_flip=False), size=28)
.databunch(bs=256 ).normalize(imagenet_stats))<choose_model_class>
train = merge.loc[train_index,:]
test = merge.loc[test_index,:].reset_index()
learn = cnn_learner(data, models.resnet34, loss_func=nn.CrossEntropyLoss() , metrics=accuracy )<train_model>
train.drop(['index'], axis=1, inplace=True)
 test.drop(['index', 'level_0'], axis=1, inplace=True )
learn.fit_one_cycle(3, 1e-2 )<save_model>
train = pd.concat([train, target],axis=1 )
learn.save('stage1' )<find_best_params>
target = train['winPlacePerc']
target = target.fillna(target.mean())
 train.drop('winPlacePerc', axis=1, inplace=True )
learn.unfreeze()
learn.lr_find()
learn.recorder.plot()<train_model>
del merge
gc.collect()
learn.fit_one_cycle(15, slice(5e-5))<save_model>
x_train, x_test, y_train, y_test = train_test_split(train, target, train_size = 0.9, test_size = 0.1, random_state = 0 )
learn.save('stage2' )<find_best_params>
test_ds = Pool(data=x_test, label=y_test)
 train_ds = Pool(data=x_train, label=y_train)
learn.unfreeze()
learn.lr_find()
learn.recorder.plot()<train_model>
lr = LinearRegression()
ridge = Ridge(alpha = 1.0)
 lgbreg = LGBMRegressor(objective='regression_l1',
boosting='gbdt', metric='mae',
max_leaves=250, learning_rate=0.01, bagging_fraction=0.7,
feature_fraction=0.7, device_type='gpu', n_estimators=5000, verbosity=100,early_stopping_round=100,verbose_eval=100 )
learn.fit_one_cycle(5, 5e-5 )<train_model>
lgb_model = lgbreg.fit(x_train, y_train, eval_metric='mae',eval_set=[(x_test, y_test)])
def create_tst(path:Path, test):
path.mkdir(parents=True, exist_ok=True)
 for i in range(len(test)) :
matplotlib.image.imsave(str(path/(str(i)+ '.jpeg')) , test[i] )<load_from_csv>
test_df = pd.read_csv(base/'test.csv')
 test_df = np.array(test_df, dtype=np.uint8 ).reshape(-1,28,28)
 test_df = np.stack(( test_df,)*3, axis=-1)
 test_df.shape<train_model>
tst_path = Path('.. /working/test')
 create_tst(tst_path, test_df )<predict_on_test>
preds = []
ImageId = []
for i in range(len(test_df)) :
img = open_image(tst_path/str(str(i)+'.jpeg'))
pred_cls, pred_idx, pred_img = learn.predict(img)
 preds.append(int(pred_idx))
ImageId.append(i+1 )<create_dataframe>
submission = pd.DataFrame({'ImageId':ImageId, 'Label':preds} )<save_to_csv>
test_v2 = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/test_V2.csv')
 Id = test_v2['Id']
test_v2.drop('Id', inplace=True, axis=1)
 test.head()
submission.to_csv('submission.csv',index=False )<load_pretrained>
del test_v2
gc.collect()
shutil.rmtree(tst_path)
 path_val = Path('.. /working/data/valid')
 shutil.rmtree(path_val)
 path_trn = Path('.. /working/data/train')
 shutil.rmtree(path_trn )<import_modules>
del x_train, y_train
gc.collect()
import numpy as np
import pandas as pd
import os
from typing import Tuple<set_options>
answer_final = lgb_model.predict(test)
 submission = pd.DataFrame({'Id':Id, 'winPlacePerc':answer_final})
 submission.tail()
<load_from_csv><EOS> 
idx_lower_bound = submission[submission['winPlacePerc'] < 0].index
submission.iloc[idx_lower_bound,1] = 0
idx_upper_bound = submission[submission['winPlacePerc'] > 1].index
submission.iloc[idx_upper_bound,1] = 1
submission[submission['winPlacePerc'] < 0].count()
submission[submission['winPlacePerc'] >1].count()
submission.to_csv("submission.csv", index=False )