Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<compute_test_metric>
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
X_train, X_test = preproc(X, X_test )<train_on_grid>
develop_mode = False
if develop_mode:
df_train = reduce_mem_usage(pd.read_csv('.. /input/train_V2.csv', nrows=5000))
df_test = reduce_mem_usage(pd.read_csv('.. /input/test_V2.csv'))
else:
df_train = reduce_mem_usage(pd.read_csv('.. /input/train_V2.csv'))
df_test = reduce_mem_usage(pd.read_csv('.. /input/test_V2.csv'))
clf = RandomForestClassifier()
param_grid = {"max_depth": [7, 5, 3],
"max_features": [3, 5, 7],
"min_samples_split": [2, 3, 5],
"bootstrap": [True, False],
"criterion": ["gini", "entropy"],
"n_estimators": [150, 200, 250, 300]}
grid_search = GridSearchCV(clf, param_grid=param_grid, cv=5, iid=False)
 start = time()
grid_search.fit(X_train, y)
 print("GridSearchCV took %.2f seconds for %d candidate parameter settings."
%(time() - start, len(grid_search.cv_results_['params'])) )<compute_test_metric>
print('The sizes of the datasets are:')
 print('Training Dataset: ', df_train.shape)
 print('Testing Dataset: ', df_test.shape )
def report(results, n_top=3):
for i in range(1, n_top + 1):
candidates = np.flatnonzero(results['rank_test_score'] == i)
 for candidate in candidates:
print("Model with rank: {0}".format(i))
print("Mean validation score: {0:.3f}(std: {1:.3f})".format(
 results['mean_test_score'][candidate],
results['std_test_score'][candidate]))
print("Parameters: {0}".format(results['params'][candidate]))
print("" )<find_best_params>
group_tmp = df_train[df_train['matchId']=='df014fbee741c6']['groupId'].value_counts().sort_values(ascending=False )
best = np.argmin(grid_search.cv_results_['rank_test_score'])
 par = grid_search.cv_results_['params'][best]<predict_on_test>
warnings.filterwarnings('ignore' )
eval_clf = RandomForestClassifier(**par)
 eval_clf.fit(X_train, y)
 pred = eval_clf.predict(X_test )<save_to_csv>
def BuildFeature(is_train=True):

 y = None
test_idx = None
if is_train:
print("Reading train.csv")
 df = pd.read_csv('.. /input/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("Reading test.csv")
 df = pd.read_csv('.. /input/test_V2.csv')
 test_idx = df.Id
df = reduce_mem_usage(df)
 print("Delete Unuseful Columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 if is_train:
print("Read Labels")
 y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("Read Group mean features")
 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("Read Group max features")
 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("Read Group min features")
 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("Read Group size features")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("Read Match mean features")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("Read Match size features")
 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 = df_out
feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y, feature_names, test_idx
data_to_submit = pd.DataFrame({
'PassengerId':id_for_subm,
'Survived':pred
})
 data_to_submit.to_csv('csv_to_submit.csv', index = False )<set_options>
X_train, y_train, train_columns, _ = BuildFeature(is_train=True)
 X_test, _, _ , test_idx = BuildFeature(is_train=False )
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
X_train =reduce_mem_usage(X_train)
 X_test = reduce_mem_usage(X_test )
from fastai import *
from fastai.vision import *
import numpy as np
import pandas as pd
import re<load_from_csv>
LR_model = LinearRegression(n_jobs=4, normalize=True)
 LR_model.fit(X_train,y_train )
df_train = pd.read_csv('.. /input/train.csv')
 df_train['fn'] = df_train.index
df_train.head()<normalization>
LR_model.score(X_train,y_train )
data =(src.transform(tfms=(rand_pad(padding=5,size=28,mode='zeros'),[])) 
.databunch(num_workers=2,bs=128)
.normalize(imagenet_stats))<define_variables>
y_pred_train = LR_model.predict(X_train)
 y_pred_test = LR_model.predict(X_test )
data.show_batch(rows=3,figsize=(10,7))<choose_model_class>
y_pred_test[y_pred_test>1] = 1
y_pred_test[y_pred_test<0] = 0
learn = cnn_learner(data,models.resnet50,metrics=accuracy,model_dir='/kaggle/model' )<find_best_params>
df_test['winPlacePerc'] = y_pred_test
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission_lr.csv', index=False )
learn.lr_find(end_lr=100 )<define_search_space>
GBR = GradientBoostingRegressor(loss='ls',learning_rate=0.1,
n_estimators=100,max_depth=3)
 GBR.fit(X_train,y_train )
lr = 1e-2<train_model>
GBR.score(X_train,y_train )
learn.fit_one_cycle(5,slice(lr))<find_best_params>
y_pred_train = GBR.predict(X_train)
 y_pred_test = GBR.predict(X_test )
<train_model><EOS> 
df_test['winPlacePerc'] = y_pred_test
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission_gbr.csv', index=False )