Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<count_missing_values>
warnings.filterwarnings('ignore')
 %matplotlib inline
py.init_notebook_mode(connected=True)
 warnings.filterwarnings("ignore")
 print(os.listdir(".. /input"))
debug = False
if debug == True:
df_train = pd.read_csv('.. /input/train_V2.csv', nrows=10000)
 df_test = pd.read_csv('.. /input/test_V2.csv')
 else:
df_train = pd.read_csv('.. /input/train_V2.csv')
 df_test = pd.read_csv('.. /input/test_V2.csv')
dfi.isnull().sum()<concatenate>
df_train[df_train['groupId']=='4d4b580de459be']
df1 = df.drop(['Age'], axis=1)
 df1 = pd.concat([df1, dfi['Age']], axis=1 )<data_type_conversions>
len(df_train[df_train['matchId']=='a10357fd1a4a91'])
df1['Embarked'].fillna("S", inplace = True )<drop_column>
df_train['roadKills'].value_counts()
df1 = df1.drop(['Cabin'], axis=1 )<categorify>
df_train['teamKills'].value_counts()
le = LabelEncoder()
T = df1['Sex']
encoded = le.fit_transform(np.ravel(T))<concatenate>
df_train['headshotKills'].value_counts()
df1 = pd.concat([df1, pd.DataFrame(encoded, columns = ['Gender'])], axis=1)
 df1 = df1.drop(['Sex'], axis=1 )<categorify>
df_train['vehicleDestroys'].value_counts()
le = LabelEncoder()
T = df1['Embarked']
encoded = le.fit_transform(np.ravel(T))<concatenate>
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(20 )
df1 = pd.concat([df1, pd.DataFrame(encoded, columns = ['Emb'])], axis=1)
 df1 = df1.drop(['Embarked'], axis=1 )<count_missing_values>
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(20 )
df1.isnull().sum()<save_to_csv>
df_train = df_train[df_train['Id']!='f70c74418bb064']
<prepare_x_and_y>
headshot = df_train[['kills','winPlacePerc','headshotKills']]
headshot['headshotrate'] = headshot['kills'] / headshot['headshotKills']
X = np.asarray(dfi.drop(['Survived'], axis = 1))
y = np.asarray(dfi[['Survived']] )<prepare_x_and_y>
del headshot
X1 = np.asarray(df1.drop(['Survived'], axis = 1))
y1 = np.asarray(df1[['Survived']] )<normalization>
df_train['headshotrate'] = df_train['kills']/df_train['headshotKills']
df_test['headshotrate'] = df_test['kills']/df_test['headshotKills']
X = preprocessing.StandardScaler().fit(X ).transform(X )<normalization>
killStreak = df_train[['kills','winPlacePerc','killStreaks']]
killStreak['killStreakrate'] = killStreak['killStreaks']/killStreak['kills']
killStreak.corr()
X1 = preprocessing.StandardScaler().fit(X1 ).transform(X1 )<import_modules>
del healthitems
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn import metrics<compute_train_metric>
kills = df_train[['assists','winPlacePerc','kills']]
kills['kills_assists'] =(kills['kills'] + kills['assists'])
 kills.corr()
GNB = GaussianNB()
scores_GNB = cross_val_score(GNB, X, y.ravel() , cv=100, scoring = "accuracy" )<compute_train_metric>
del df_train,df_test;
gc.collect()
LR = LogisticRegression(solver = 'lbfgs', max_iter=1000)
 scores_LR = cross_val_score(LR, X, y.ravel() , cv=100, scoring = "accuracy" )<compute_train_metric>
def feature_engineering(is_train=True,debug=True):
test_idx = None
if is_train:
print("processing train.csv")
 if debug == True:
df = pd.read_csv('.. /input/train_V2.csv', nrows=10000)
 else:
df = pd.read_csv('.. /input/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 df = pd.read_csv('.. /input/test_V2.csv')
 test_idx = df.Id
print("remove some columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 y = None
if is_train:
print("get target")
 y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 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("get group max feature")
 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("get group min feature")
 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("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 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
x_train, y_train, train_columns, _ = feature_engineering(True,False)
 x_test, _, _ , test_idx = feature_engineering(False,True)
RF = RandomForestClassifier(n_estimators=100, random_state=0)
 scores_RF = cross_val_score(RF, X1, y1.ravel() , cv=100, scoring = "accuracy" )<compute_train_metric>
x_train['headshotrate'] = x_train['kills']/x_train['headshotKills']
x_test['headshotrate'] = x_test['kills']/x_test['headshotKills']
x_train['killStreakrate'] = x_train['killStreaks']/x_train['kills']
x_test['killStreakrate'] = x_test['killStreaks']/x_test['kills']
x_train['healthitems'] = x_train['heals'] + x_train['boosts']
x_test['healthitems'] = x_test['heals'] + x_test['boosts']
del x_train['heals'];del x_test['heals']
train_columns.append('headshotrate')
 train_columns.append('killStreakrate')
 train_columns.append('healthitems')
 train_columns.remove('heals' )
VM = SVC(C = 0.01, kernel = 'rbf', gamma = 'scale', class_weight = 'balanced')
 scores_SVC = cross_val_score(VM, X1, y1.ravel() , cv=100, scoring = "accuracy" )<compute_train_metric>
x_train = reduce_mem_usage(x_train)
 x_test = reduce_mem_usage(x_test )
NGH = KNeighborsClassifier(n_neighbors = 2)
 scores_KNN = cross_val_score(NGH, X, y.ravel() , cv=100, scoring = "accuracy" )<compute_train_metric>
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
feature_importance_df = 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':15000, 'early_stopping_rounds':100,
"num_leaves" : 31, "learning_rate" : 0.05, "bagging_fraction" : 0.9,
"bagging_seed" : 0, "num_threads" : 4,"colsample_bytree" : 0.7
}
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)
 sub_pred[sub_pred>1] = 1
sub_pred[sub_pred<0] = 0
sub_preds += sub_pred/ folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = train_columns
fold_importance_df["importance"] = lgb_model.feature_importance()
fold_importance_df["fold"] = n_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 gc.collect()
end = time.time()
print("Take Time :",(end-start))
BST = XGBClassifier()
scores_XGB = cross_val_score(BST, X, y.ravel() , cv=10, scoring = "accuracy" )<create_dataframe>
df_test = pd.read_csv('.. /input/' + 'test_V2.csv')
 pred = sub_preds
print("fix winPlacePerc")
 for i in range(len(df_test)) :
winPlacePerc = pred[i]
maxPlace = int(df_test.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
pred[i] = winPlacePerc
if(i + 1)% 100000 == 0:
print(i, flush=True, end=" ")
 df_test['winPlacePerc'] = pred
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )