Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<choose_model_class>
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
grid_hard = VotingClassifier(estimators = [('Random Forest', ran), 
('Logistic Regression', log), 
('XGBoost', xgb), 
('Gradient Boosting', gbc), 
('Extra Trees', ext), 
('AdaBoost', ada), 
('Gaussian Process', gpc), 
('SVC', svc), 
('K Nearest Neighbour', knn), 
('Bagging Classifier', bag)], voting = 'hard')
 grid_hard_cv = model_selection.cross_validate(grid_hard, X_train, y_train, cv = 10)
 grid_hard.fit(X_train, y_train)
 print("Hard voting on train set score mean: {:.2f}".format(grid_hard_cv['train_score'].mean() *100))
print("Hard voting on test set score mean: {:.2f}".format(grid_hard_cv['test_score'].mean() *100))<choose_model_class>
develop_mode = False
if develop_mode:
df_train = reduce_mem_usage(pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv', nrows=5000))
df_test = reduce_mem_usage(pd.read_csv('.. /input/pubg-finish-placement-prediction/test_V2.csv'))
else:
df_train = reduce_mem_usage(pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv'))
df_test = reduce_mem_usage(pd.read_csv('.. /input/pubg-finish-placement-prediction/test_V2.csv'))
grid_soft = VotingClassifier(estimators = [('Random Forest', ran), 
('Logistic Regression', log), 
('XGBoost', xgb), 
('Gradient Boosting', gbc), 
('Extra Trees', ext), 
('AdaBoost', ada), 
('Gaussian Process', gpc), 
('SVC', svc), 
('K Nearest Neighbour', knn), 
('Bagging Classifier', bag)], voting = 'soft')
 grid_soft_cv = model_selection.cross_validate(grid_soft, X_train, y_train, cv = 10)
 grid_soft.fit(X_train, y_train)
 print("Soft voting on train set score mean: {:.2f}".format(grid_soft_cv['train_score'].mean() *100))
print("Soft voting on test set score mean: {:.2f}".format(grid_soft_cv['test_score'].mean() *100))<save_to_csv>
print('The sizes of the datasets are:')
 print('Training Dataset: ', df_train.shape)
 print('Testing Dataset: ', df_test.shape )
predictions = grid_soft.predict(X_test)
 submission = pd.concat([pd.DataFrame(passId), pd.DataFrame(predictions)], axis = 'columns')
 submission.columns = ["PassengerId", "Survived"]
submission.to_csv('titanic_submission.csv', header = True, index = False )<set_options>
warnings.filterwarnings('ignore' )
warnings.filterwarnings('ignore')
<load_from_csv>
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/pubg-finish-placement-prediction/train_V2.csv', nrows=10000)
 else:
df = pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 df = pd.read_csv('.. /input/pubg-finish-placement-prediction/test_V2.csv')
 test_idx = df.Id
df = reduce_mem_usage(df)
 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
train = pd.read_csv(".. /input/train.csv")
 train['label'] = 'train'
test = pd.read_csv(".. /input/test.csv")
 test['label'] = 'test'
test_passengerId = test.PassengerId
df = train.append(test)
 df.sample(2 )<count_missing_values>
X_train, y_train, train_columns, _ = feature_engineering(True,False)
 X_test, _, _ , test_idx = feature_engineering(False,True )
df.isnull().sum()<correct_missing_values>
X_train =reduce_mem_usage(X_train)
 X_test = reduce_mem_usage(X_test )
df['Embarked'].fillna('S', inplace = True )<filter>
LR_model = LinearRegression(n_jobs=4, normalize=True)
 LR_model.fit(X_train,y_train )
df[df.Fare.isnull() ]<define_search_space>
LR_model.score(X_train,y_train )
print(df[df.Pclass == 1].Fare.quantile([0.25, 0.50, 0.75]))
print(df[df.Pclass == 2].Fare.quantile([0.25, 0.50, 0.75]))
print(df[df.Pclass == 3].Fare.quantile([0.25, 0.50, 0.75]))<correct_missing_values>
y_pred_train = LR_model.predict(X_train)
 y_pred_test = LR_model.predict(X_test )
df['Fare'].fillna(df[df.Pclass == 3].Fare.median() , inplace = True )<count_missing_values>
y_pred_test[y_pred_test>1] = 1
y_pred_test[y_pred_test<0] = 0
print("Age column has", df.Age.isnull().sum() , "missing values out of", len(df), ".Missing value percentage =", df.Age.isnull().sum() /len(df)*100 )<create_dataframe>
df_test['winPlacePerc'] = y_pred_test
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )