Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<load_from_csv>
warnings.filterwarnings('ignore')
 pd.options.display.float_format = '{:.3f}'.format
%matplotlib inline
color = sns.color_palette()
py.init_notebook_mode(connected=True)
 print(os.listdir(".. /input"))
df_test = pd.read_csv('.. /input/test.csv')
 df_test['label'] = 0
df_test['fn'] = df_test.index
df_test.head()<create_dataframe>
df_train = reduce_mem_usage(pd.read_csv('.. /input/train_V2.csv'))
df_test = reduce_mem_usage(pd.read_csv('.. /input/test_V2.csv'))
learn.data.add_test(PixelImageItemList.from_df(df_test,path='.',cols='fn'))<predict_on_test>
print('train : {}'.format(df_train.shape))
print('test : {}'.format(df_test.shape))
pred_test = learn.get_preds(ds_type=DatasetType.Test )<prepare_output>
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()
test_result = torch.argmax(pred_test[0],dim=1)
 result = test_result.numpy()<save_to_csv>
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()
final = pd.Series(result,name='Label')
 submission = pd.concat([pd.Series(range(1,28001),name='ImageId'),final],axis=1)
 submission.to_csv('fastai-res34-0.992.csv',index=False )<set_options>
df_train.dropna(axis=0, inplace=True)
 df_train.isnull().sum()
%reload_ext autoreload
%autoreload 2
%matplotlib inline
warnings.filterwarnings("ignore")
 %matplotlib inline 
<load_pretrained>
headshot = df_train[['kills', 'winPlacePerc', 'headshotKills']]
headshot['headshotrate'] = headshot['headshotKills'] / headshot['kills']
headshot.corr()
md_ef = EfficientNet.from_pretrained('efficientnet-b5', num_classes=1)
 !mkdir models
!cp '.. /input/kaggle-public/abcdef.pth' 'models'<feature_engineering>
df_train['headshotrate'] = df_train['headshotKills']/df_train['kills']
df_test['headshotrate'] = df_test['headshotKills']/df_test['kills']
del headshot
bs = 64
tfms = get_transforms(do_flip=True,flip_vert=True )<choose_model_class>
killStreak = df_train[['kills','winPlacePerc','killStreaks']]
killStreak['killStreakrate'] = killStreak['killStreaks']/killStreak['kills']
killStreak.corr()
learn.load('abcdef');
opt = OptimizedRounder()<predict_on_test>
df_train['killStreakrate'] = -(df_train['killStreaks'] / df_train['kills'])
 df_test['killStreakrate'] = -(df_test['killStreaks'] / df_test['kills'])
 del killStreak
preds0,y = learn.get_preds(DatasetType.Test )<compute_test_metric>
df_train['hacker_pt'] = 0
df_test['hacker_pt'] = 0
preds =(preds0 + preds1 + preds2 + preds3 + preds4)/5<save_to_csv>
pd.set_option('display.max_columns', 50 )
tst_pred = opt.predict(preds, coef=[0.5, 1.5, 2.5, 3.5])
 test_df.diagnosis = tst_pred.astype(int)
 test_df.to_csv('submission.csv',index=False)
 print('done' )<import_modules>
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()
import matplotlib.pyplot as plt
import sklearn
import seaborn as sb<load_from_csv>
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)
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 train.head()<define_variables>
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
combine = [train,test]<count_missing_values>
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
for c in train.columns:
print(c, str(100*train[c].isnull().sum() /len(train)))
<feature_engineering>
df_train['hacker_pt'][df_train['longestKill'] >= 1000] += 1
df_test['hacker_pt'][df_train['longestKill'] >= 1000] += 1
train['Age'] = train['Age'].fillna(train['Age'].mean() )<count_missing_values>
kills = df_train[['assists','winPlacePerc','kills']]
kills['kills_assists'] =(kills['kills'] + kills['assists'])
 kills.corr()
for c in train.columns:
print(c, str(100*train[c].isnull().sum() /len(train)) )<groupby>
df_train['kills_assists'] = df_train['kills'] + df_train['assists']
df_test['kills_assists'] = df_test['kills'] + df_test['assists']
del kills
dependencies_sex = train[['Sex', 'Survived']].groupby(['Sex'],as_index=False ).mean()
dependencies_Pclass = train[['Pclass', 'Survived']].groupby(['Pclass'],as_index=False ).mean()
print(dependencies_Pclass)
 print(dependencies_sex )<feature_engineering>
df_train = reduce_mem_usage(df_train)
 df_test = reduce_mem_usage(df_test)
 gc.collect()
for dats in combine:
dats['Title'] = dats.Name.str.extract('([A-Za-z]+)\.',expand=False )<feature_engineering>
del missing_data
del percent
del total
gc.collect()
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col',
	'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs' )<categorify>
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' )
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0 )<groupby>
df_train_mean = df_train.groupby(['matchId','groupId'] ).mean().reset_index()
df_test_mean = df_test.groupby(['matchId','groupId'] ).mean().reset_index()
title_dependencies=train[['Title','Survived','Sex']].groupby(['Title','Sex'],as_index=False ).mean()
title_dependencies<categorify>
df_train_match_mean = df_train.groupby(['matchId'] ).mean().reset_index()
df_test_match_mean = df_test.groupby(['matchId'] ).mean().reset_index()
train = train.drop(['Name', 'PassengerId', 'Cabin', 'Embarked','Ticket'], axis=1)
 test = test.drop(['Name', 'PassengerId', 'Cabin', 'Embarked','Ticket'], axis=1)
 combine=[train,test]
print(train.head())
 for dataset in combine:
dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0} ).astype(int)
 print(train.head())
 print(test.head())
<split>
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
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
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
X_train, X_test , Y_train, Y_test = train_test_split(train.drop(['Survived'],axis=1),train['Survived'],test_size=0.10,random_state=None )<import_modules>
df_train = reduce_mem_usage(df_train)
 df_test = reduce_mem_usage(df_test)
 gc.collect()
from sklearn.linear_model import LogisticRegression
from sklearn import metrics<train_model>
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" )
modelLR= LogisticRegression(solver='liblinear',C=0.21,random_state=1)
 modelLR.fit(X_train,Y_train)
<compute_train_metric>
x_train = df_train[train_columns]
x_test = df_test[train_columns]
y_train = df_train["winPlacePerc"].astype('float' )
Y_pred_log=modelLR.predict(X_test)
 acc_LR = metrics.accuracy_score(Y_test, Y_pred_log )<compute_test_metric>
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()
print(Y_pred_log)
 print("We see that Logistic regression gives an Accuracy of ",acc_LR*100,"% on the traing set.")
<import_modules>
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 )
from sklearn.tree import DecisionTreeClassifier<predict_on_test>
del x_train['matchType']
del x_test['matchType']
dtree = DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=1,
max_features=7, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0,
random_state=None, splitter='best')
 dtree.fit(X_train, Y_train)
 y_pred_tree = dtree.predict(X_test )<compute_test_metric>
del df_train
del df_test
gc.collect()
acc_DT = metrics.accuracy_score(y_pred_tree, Y_test)
 print(y_pred_tree)
 print("We see that Decision Tree gives an Accuracy of ",acc_DT*100,"% on the traing set." )<import_modules>
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] )
from sklearn.ensemble import RandomForestClassifier<predict_on_test>
print('Done' )
<compute_test_metric><EOS> 
test_pred = pd.DataFrame({"Id":test_idx})
 test_pred["winPlacePerc"] = sub_preds
test_pred.columns = ["Id", "winPlacePerc"]
test_pred.to_csv("lgb_model_181204.csv", index=False )