Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<define_search_space>
import numpy as np
import pandas as pd
import tqdm
import matplotlib.pyplot as plt
import keras
from keras.layers.core import Dense
from keras.layers.normalization import BatchNormalization
from sklearn.preprocessing import MinMaxScaler, RobustScaler, MaxAbsScaler
svm_grid = {'C': [10,11,12,13,14,15,16,17,18,19,20], 'gamma': ['auto']}
svm_search = GridSearchCV(estimator = SVC() , param_grid = svm_grid, cv = cv, refit=True, n_jobs=1 )<train_on_grid>
train = pd.read_csv(".. /input/train_V2.csv" )
svm_search.fit(X_train, train['Survived'])
 svm_best = svm_search.best_estimator_
print("Cross-validation accuracy: {}, standard deviation: {}, with parameters {}" 
.format(svm_search.best_score_, svm_search.cv_results_['std_test_score'][svm_search.best_index_],
svm_search.best_params_))<save_to_csv>
pd.DataFrame(train.dtypes, columns=["Type"] )
y_train_hat = svm_best.predict(X_train)
 print('Train Accuracy: {}' 
.format(metrics.accuracy_score(y_train_hat, y_train)))
 predictions['Survived'] = svm_best.predict(X_test)
 predictions.to_csv('submission-svm.csv', index=False )<statistical_test>
print("Number of record:", len(train), "
Number of Unique Id:", len(pd.unique(train.Id)) )
def get_survived_svm_rule(row):
if row['Pclass'] in [1,2]:
if row['Title'] == 'Mr':
survived = 0
else:
survived = 1
else:
if row['Title'] == 'Mr' or row['FamSizeBin'] == 1 or \ 
(row['Title'] == 'Miss' and row['Embarked'] == 'S' and row['Age'] >= 18):
survived = 0
else:
survived = 1
return survived<set_options>
print("Number of match: ", len(pd.unique(train.matchId)) , "
Number of match(<9): ", sum(train.groupby("matchId" ).size() < 9))
warnings.filterwarnings("ignore" )<set_options>
temp = train.loc[train.matchId.isin(train.groupby("matchId" ).size() [train.groupby("matchId" ).size() < 9].index), :]
temp.loc[temp.matchId == "e263f4a227313a"]
warnings.filterwarnings("ignore" )<load_from_csv>
temp = pd.DataFrame(train.groupby("matchId" ).size() , columns=["player"])
 temp.reset_index(level=0, inplace=True )
gender_submission = pd.read_csv(".. /input/titanic/gender_submission.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 train = pd.read_csv(".. /input/titanic/train.csv")
 test["Survived"] = np.nan<concatenate>
train = train.merge(temp, left_on="matchId", right_on="matchId" )
dataset = pd.concat([train,test],axis=0 ).reset_index(drop=True)
 dataset = dataset.fillna(np.nan )<count_missing_values>
print("Type: ", pd.unique(train.matchType), "
Count: ", len(pd.unique(train.matchType)) )
dataset.isnull().sum(axis = 0 )<feature_engineering>
train["matchType_1"] = "-"
train.loc[(train.matchType == "solo-fpp")| 
(train.matchType == "solo")| 
(train.matchType == "normal-solo-fpp")| 
(train.matchType == "normal-solo"), "matchType_1"] = "solo"
train.loc[(train.matchType == "duo-fpp")| 
(train.matchType == "duo")| 
(train.matchType == "normal-duo-fpp")| 
(train.matchType == "normal-duo"), "matchType_1"] = "duo"
train.loc[(train.matchType == "squad-fpp")| 
(train.matchType == "squad")| 
(train.matchType == "normal-squad-fpp")| 
(train.matchType == "normal-squad"), "matchType_1"] = "squad"
train.loc[(train.matchType == "flarefpp")| 
(train.matchType == "flaretpp")| 
(train.matchType == "crashfpp")| 
(train.matchType == "crashtpp"), "matchType_1"] = "etc"
dataset["Family"] = dataset["SibSp"] + dataset["Parch"] + 1
train["Family"] = train["SibSp"] + train["Parch"] + 1
test["Family"] = test["SibSp"] + test["Parch"] + 1<drop_column>
train["matchType_2"] = "-"
train.loc[(train.matchType == "solo-fpp")| 
(train.matchType == "duo-fpp")| 
(train.matchType == "squad-fpp")| 
(train.matchType == "normal-solo-fpp")| 
(train.matchType == "normal-duo-fpp")| 
(train.matchType == "normal-squad-fpp")| 
(train.matchType == "crashfpp")| 
(train.matchType == "flarefpp"), "matchType_2"] = "fpp"
train.loc[(train.matchType == "solo")| 
(train.matchType == "duo")| 
(train.matchType == "squad")| 
(train.matchType == "normal-solo")| 
(train.matchType == "normal-duo")| 
(train.matchType == "normal-squad")| 
(train.matchType == "crashtpp")| 
(train.matchType == "flaretpp"), "matchType_2"] = "tpp"
dataset = dataset.drop(columns=["SibSp","Parch"])
 train = train.drop(columns=["SibSp","Parch"])
 test = test.drop(columns=["SibSp","Parch"] )<categorify>
train["solo"] = 0
train["duo"] = 0
train["squad"] = 0
train["etc"] = 0
train.loc[train.matchType_1 == "solo", "solo"] = 1
train.loc[train.matchType_1 == "duo", "duo"] = 1
train.loc[train.matchType_1 == "squad", "squad"] = 1
train.loc[train.matchType_1 == "etc", "etc"] = 1
dataset.Family = list(map(lambda x: 'Big' if x > 4 else('Single' if x == 1 else 'Medium'), dataset.Family))
train.Family = list(map(lambda x: 'Big' if x > 4 else('Single' if x == 1 else 'Medium'), train.Family))
test.Family = list(map(lambda x: 'Big' if x > 4 else('Single' if x == 1 else 'Medium'), test.Family))<categorify>
train["fpp"] = 0
train["tpp"] = 0
train.loc[train.matchType_2 == "fpp", "fpp"] = 1
train.loc[train.matchType_2 == "tpp", "tpp"] = 1
dataset.Sex = dataset.Sex.map({'male': 0, 'female': 1})
 train.Sex = train.Sex.map({'male': 0, 'female': 1})
 test.Sex = test.Sex.map({'male': 0, 'female': 1} )<data_type_conversions>
print(list(train.columns[train.dtypes != "O"]))
dataset["Fare"] = dataset["Fare"].fillna(dataset["Fare"].median())
 train["Fare"] = train["Fare"].fillna(dataset["Fare"].median())
 test["Fare"] = test["Fare"].fillna(dataset["Fare"].median() )<categorify>
feature = ["assists", "boosts", "damageDealt", "DBNOs", "headshotKills", "heals",
"killPlace", "killPoints", "kills", "killStreaks", "longestKill",
"matchDuration", "maxPlace", "rankPoints", "revives", "rideDistance",
"roadKills", "swimDistance", "teamKills", "vehicleDestroys", "walkDistance", "weaponsAcquired", "winPoints", "player"]
dataset.Fare = list(map(lambda x: 'Very Low' if x <= 10
else('Low' if(x > 10 and x < 26)
 else('Medium' if(x >= 26 and x <= 50)else 'High')) , dataset.Fare))
train.Fare = list(map(lambda x: 'Very Low' if x <= 10
else('Low' if(x > 10 and x < 26)
 else('Medium' if(x >= 26 and x <= 50)else 'High')) , train.Fare))
test.Fare = list(map(lambda x: 'Very Low' if x <= 10
else('Low' if(x > 10 and x < 26)
 else('Medium' if(x >= 26 and x <= 50)else 'High')) , test.Fare))<data_type_conversions>
feature_1 = ["matchId", "assists", "boosts", "damageDealt", "DBNOs", "headshotKills", "heals",
"killPlace", "killPoints", "kills", "killStreaks", "longestKill",
"revives", "rideDistance", "roadKills", "swimDistance", "teamKills",
"vehicleDestroys", "walkDistance", "weaponsAcquired", "winPoints"]
dataset.Embarked = dataset.Embarked.fillna('S')
 train.Embarked = train.Embarked.fillna('S')
 test.Embarked = test.Embarked.fillna('S' )<feature_engineering>
feature_2 = ["matchDuration", "maxPlace", "rankPoints", "player", "fpp", "tpp"]
title = []
for i in dataset.Name.str.split(', '):
title.append(i[1].split('.')[0])
 dataset["Title"] = title
title = []
for i in train.Name.str.split(', '):
title.append(i[1].split('.')[0])
 train["Title"] = title
title = []
for i in test.Name.str.split(', '):
title.append(i[1].split('.')[0])
 test["Title"] = title<drop_column>
for i in list(train.columns[train.dtypes != "O"]):
print(i, ":", sum(train[i].isna()))
dataset = dataset.drop(columns=["Name"])
 train = train.drop(columns=["Name"])
 test = test.drop(columns=["Name"] )<categorify>
cabin = []
for i in dataset.Cabin:
if type(i)!= float:
cabin.append(i[0])
 else:
cabin.append('Z')
 dataset.Cabin = cabin
cabin = []
for i in train.Cabin:
if type(i)!= float:
cabin.append(i[0])
 else:
cabin.append('Z')
 train.Cabin = cabin
cabin = []
for i in test.Cabin:
if type(i)!= float:
cabin.append(i[0])
 else:
cabin.append('Z')
 test.Cabin = cabin<categorify>
dataset.Cabin = dataset.Cabin.map({'B':'BCDE','C':'BCDE','D':'BCDE','E':'BCDE','A':'AFG','F':'AFG','G':'AFG','Z':'Z','T':'Z'})
 train.Cabin = train.Cabin.map({'B':'BCDE','C':'BCDE','D':'BCDE','E':'BCDE','A':'AFG','F':'AFG','G':'AFG','Z':'Z','T':'Z'})
 test.Cabin = test.Cabin.map({'B':'BCDE','C':'BCDE','D':'BCDE','E':'BCDE','A':'AFG','F':'AFG','G':'AFG','Z':'Z','T':'Z'} )<define_variables>
np.sum(train.winPlacePerc.isna() )
tickets = []
for i in dataset.Ticket:
tickets.append(i.split(' ')[-1][0])
 dataset.Ticket = tickets
tickets = []
for i in train.Ticket:
tickets.append(i.split(' ')[-1][0])
 train.Ticket = tickets
tickets = []
for i in test.Ticket:
tickets.append(i.split(' ')[-1][0])
 test.Ticket = tickets<feature_engineering>
train = train.loc[train.winPlacePerc.notna() , :]
dataset.Ticket = list(map(lambda x: 4 if(x == 'L' or int(x)>= 4)else int(x), dataset.Ticket))
train.Ticket = list(map(lambda x: 4 if(x == 'L' or int(x)>= 4)else int(x), train.Ticket))
test.Ticket = list(map(lambda x: 4 if(x == 'L' or int(x)>= 4)else int(x), test.Ticket))<create_dataframe>
medians = pd.DataFrame(dataset.groupby(['Pclass', 'Title'])['Age'].median())
 medians<feature_engineering>
ages = []
for i in dataset[dataset.Age.isnull() == True][["Pclass","Title"]].values:
ages.append(medians.ix[(i[0], i[1])].Age)
 dataset.Age[dataset.Age.isnull() == True] = ages<feature_engineering>
index = dataset[dataset.Age.isnull() == True].index
train_idx = index[index <= 890]
test_idx = index[index > 890]
train['Age'][train.index.isin(train_idx)] = dataset['Age'][dataset.index.isin(train_idx)].values
test['Age'][test.index.isin(test_idx - 891)] = dataset['Age'][dataset.index.isin(test_idx)].values<categorify>
ages = []
for i in dataset.Age:
if i < 18:
ages.append('less_18')
 elif i >= 18 and i < 50:
ages.append('18_50')
 else:
ages.append('greater_50')
 dataset.Age = ages
ages = []
for i in train.Age:
if i < 18:
ages.append('less_18')
 elif i >= 18 and i < 50:
ages.append('18_50')
 else:
ages.append('greater_50')
 train.Age = ages
ages = []
for i in test.Age:
if i < 18:
ages.append('less_18')
 elif i >= 18 and i < 50:
ages.append('18_50')
 else:
ages.append('greater_50')
 test.Age = ages<prepare_x_and_y>
train.set_index("Id", inplace=True)
 train.index.name = "Id"
x_train = train.loc[:, ~train.columns.isin(['PassengerId', 'Survived', 'Sex'])]
y_train = train.Survived
x_test = test.loc[:, ~test.columns.isin(['PassengerId', 'Survived', 'Sex'])]<categorify>
temp_1 = train.loc[:, feature_1]
temp_2 = train.loc[:, feature_2]
x_train = pd.get_dummies(x_train)
 x_train["Sex"] = train.Sex
x_test = pd.get_dummies(x_test)
 x_test["Sex"] = test.Sex<train_model>
temp_1.groupby("matchId" ).transform(minmax)
 for i in temp_2.columns[:4]:
temp_2[i] =(temp_2[i] - min(temp_2[i])) /(max(temp_2[i])- min(temp_2[i]))
rf = RandomForestClassifier()
rf.fit(x_train, y_train )<train_on_grid>
X = pd.merge(temp_1, temp_2, on="Id")
 X = pd.merge(X, train.loc[:, ["matchType_1", "winPlacePerc"]], on="Id" )
ABC = AdaBoostClassifier(DecisionTreeClassifier())
 ABC_param_grid = {"base_estimator__criterion" : ["gini", "entropy"],
"base_estimator__splitter" : ["best", "random"],
"algorithm" : ["SAMME","SAMME.R"],
"n_estimators" :[5,6,7,8,9,10,20],
"learning_rate": [0.001, 0.01, 0.1, 0.3]}
gsABC = GridSearchCV(ABC, param_grid = ABC_param_grid, cv = 10, scoring = "accuracy", n_jobs = 6, verbose = 1)
 gsABC.fit(x_train,y_train)
 ada_best = gsABC.best_estimator_
gsABC.best_score_<choose_model_class>
ExtC = ExtraTreesClassifier()
ex_param_grid = {"max_depth": [3, 4, 5],
"max_features": [3, 10, 15],
"min_samples_split": [2, 3, 4],
"min_samples_leaf": [1, 2],
"bootstrap": [False,True],
"n_estimators" :[100,200,300],
"criterion": ["gini","entropy"]}
gsExtC = GridSearchCV(ExtC, param_grid = ex_param_grid, cv = 10, scoring = "accuracy", n_jobs = 6, verbose = 1)
 gsExtC.fit(x_train,y_train)
 ext_best = gsExtC.best_estimator_
gsExtC.best_score_<define_search_space>
rf_test = {"max_depth": [24,26],
"max_features": [6,8,10],
"min_samples_split": [3,4],
"min_samples_leaf": [3,4],
"bootstrap": [True],
"n_estimators" :[50,80],
"criterion": ["gini","entropy"],
"max_leaf_nodes":[26,28],
"min_impurity_decrease":[0.0],
"min_weight_fraction_leaf":[0.0]}
tuning = GridSearchCV(estimator = RandomForestClassifier() , param_grid = rf_test, scoring = 'accuracy', n_jobs = 6, cv = 10)
 tuning.fit(x_train,np.ravel(y_train))
rf_best = tuning.best_estimator_
tuning.best_score_<train_on_grid>
GBM = GradientBoostingClassifier()
gb_param_grid = {'loss' : ["deviance"],
'n_estimators' : [450,460,500],
'learning_rate': [0.1,0.11],
'max_depth': [7,8],
'min_samples_leaf': [30,40],
'max_features': [0.1,0.4,0.6]}
gsGBC = GridSearchCV(GBM, param_grid = gb_param_grid, cv = 10, scoring = "accuracy", n_jobs = 6, verbose = 1)
 gsGBC.fit(x_train,y_train)
 gbm_best = gsGBC.best_estimator_
gsGBC.best_score_<train_on_grid>
SVMC = SVC(probability=True)
 svc_param_grid = {'kernel': ['rbf'],
'gamma': [0.027,0.029,0.03,0.031],
'C': [45,55,76,77,78,85,95,100],
'tol':[0.001,0.0008,0.0009,0.0011]}
gsSVMC = GridSearchCV(SVMC, param_grid = svc_param_grid, cv = 10, scoring = "accuracy", n_jobs = 6, verbose = 1)
 gsSVMC.fit(x_train,y_train)
 svm_best = gsSVMC.best_estimator_
gsSVMC.best_score_<train_on_grid>
print("Name: ", feature, "
Count: ", len(feature))
XGB = XGBClassifier()
xgb_param_grid = {'learning_rate': [0.1,0.04,0.01],
'max_depth': [5,6,7],
'n_estimators': [350,400,450,2000],
'gamma': [0,1,5,8],
'subsample': [0.8,0.95,1.0]}
gsXBC = GridSearchCV(XGB, param_grid = xgb_param_grid, cv = 10, scoring = "accuracy", n_jobs = 6, verbose = 1)
 gsXBC.fit(x_train,y_train)
 xgb_best = gsXBC.best_estimator_
gsXBC.best_score_<choose_model_class>
list_feat = ["assists", "boosts", "damageDealt", "DBNOs", "headshotKills", "heals",
"killPlace", "killPoints", "kills", "killStreaks", "longestKill",
"matchDuration", "maxPlace", "rankPoints", "revives", "rideDistance",
"roadKills", "swimDistance", "teamKills", "vehicleDestroys", "walkDistance",
"weaponsAcquired", "winPoints", "player", "fpp", "tpp"]
voting = VotingClassifier(estimators=[('rfc', rf_best), 
('extc', ext_best), 
('svc', svm_best), 
('gbc',gbm_best), 
('xgbc',xgb_best), 
('ada',ada_best)])
 v_param_grid = {'voting':['soft',
'hard']}
gsV = GridSearchCV(voting,
param_grid =
v_param_grid,
cv = 10,
scoring = "accuracy",
n_jobs = 6,
verbose = 1)
 gsV.fit(x_train,y_train)
 v_best = gsV.best_estimator_
gsV.best_score_<predict_on_test>
list_feat_1 = ["assists", "boosts", "damageDealt", "DBNOs", "headshotKills", "heals",
"killPlace", "killPoints", "kills", "killStreaks", "longestKill",
"matchDuration", "maxPlace", "rankPoints", "revives", "rideDistance",
"roadKills", "swimDistance", "teamKills", "vehicleDestroys", "walkDistance",
"weaponsAcquired", "winPoints", "player", "fpp", "tpp", "matchId"]
pred = v_best.predict(x_test)
 submission = pd.DataFrame(test.PassengerId)
 submission["Survived"] = pd.Series(pred )<save_to_csv>
train = X
submission.to_csv("submission.csv",index=False )<load_from_csv>
model_1 = keras.models.Sequential()
model_1.add(Dense(32, input_dim=len(list_feat), activation="elu", kernel_initializer="he_normal"))
model_1.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_1.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_1.add(keras.layers.Dropout(0.25))
model_1.add(Dense(256, activation="elu", kernel_initializer="he_normal"))
model_1.add(Dense(256, activation="elu", kernel_initializer="he_normal"))
model_1.add(keras.layers.Dropout(0.25))
model_1.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_1.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_1.add(Dense(32, activation="elu", kernel_initializer="he_normal"))
model_1.add(keras.layers.Dropout(0.25))
model_1.add(Dense(1, activation="sigmoid"))
model_1.compile(optimizer="RMSprop", loss='MAE', metrics=["MAE"] )
train_df = pd.read_csv('.. /input/train.csv', index_col='PassengerId')
 test_df = pd.read_csv('.. /input/test.csv', index_col='PassengerId')
 train_df.head()<count_missing_values>
x_train = train.loc[train.matchType_1 == "solo", list_feat]
y_train = train.loc[train.matchType_1 == "solo", ["winPlacePerc"]]
print('Train dataset:')
 print(train_df.isna().sum() [train_df.isna().any() ])
 print('
Test dataset:')
 print(test_df.isna().sum() [test_df.isna().any() ] )<count_values>
model_1.fit(x=x_train, y=y_train, epochs=50, batch_size=10000, validation_split=0.2, shuffle=True)
 model_1.fit(x=x_train, y=y_train, epochs=30, batch_size=2000, validation_split=0.2, shuffle=True )
cat_feat = ['Sex', 'Embarked']
for cf in cat_feat:
if cf != cat_feat[0]:
print()
print(train_df[cf].value_counts() / train_df[cf].count() )<drop_column>
train_df = train_df.drop(['Ticket', 'Cabin'], axis=1)
 test_df = test_df.drop(['Ticket', 'Cabin'], axis=1 )<feature_engineering>
model_1.save("model_1_solo.h5")
extract_title = lambda df: df.Name.str.extract(r'([A-Za-z]+)\.', expand=False)
 train_df['Title'] = extract_title(train_df)
 test_df['Title'] = extract_title(test_df)
 train_df.Title.value_counts()<categorify>
model_2 = keras.models.Sequential()
model_2.add(Dense(32, input_dim=len(list_feat), activation="elu", kernel_initializer="he_normal"))
model_2.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_2.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_2.add(keras.layers.Dropout(0.25))
model_2.add(Dense(256, activation="elu", kernel_initializer="he_normal"))
model_2.add(Dense(256, activation="elu", kernel_initializer="he_normal"))
model_2.add(keras.layers.Dropout(0.25))
model_2.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_2.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_2.add(Dense(32, activation="elu", kernel_initializer="he_normal"))
model_2.add(keras.layers.Dropout(0.25))
model_2.add(Dense(1, activation="sigmoid"))
model_2.compile(optimizer="RMSprop", loss='MAE', metrics=["MAE"] )
def replace_titles(df):
df['Title'] = df['Title'].replace(['Lady', 'Countess','Capt', 'Col',\
	'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 df['Title'] = df['Title'].replace('Mlle', 'Miss')
 df['Title'] = df['Title'].replace('Ms', 'Miss')
 df['Title'] = df['Title'].replace('Mme', 'Mrs')
 return df
train_df = replace_titles(train_df)
 test_df = replace_titles(test_df)
 train_df = train_df.drop('Name', axis=1)
 test_df = test_df.drop('Name', axis=1 )<feature_engineering>
x_train = train.loc[train.matchType_1 == "duo", list_feat]
y_train = train.loc[train.matchType_1 == "duo", ["winPlacePerc"]]
def add_family(df):
df['Family'] = df.SibSp + df.Parch
return df
train_df = add_family(train_df)
 test_df = add_family(test_df )<drop_column>
model_2.fit(x=x_train, y=y_train, epochs=50, batch_size=10000, validation_split=0.2, shuffle=True)
 model_2.fit(x=x_train, y=y_train, epochs=40, batch_size=2000, validation_split=0.2, shuffle=True )
family_feat = ['SibSp', 'Parch']
train_df = train_df.drop(family_feat, axis=1)
 test_df = test_df.drop(family_feat, axis=1 )<feature_engineering>
def fill_ages(df):
df.loc[(df.Age.isnull())&(df.Title == 'Master'), 'Age'] = df[df.Title == 'Master'].Age.median()
df.loc[(df.Age.isnull())&(df.Title != 'Master'), 'Age'] = df[df.Title != 'Master'].Age.median()
return df
def fill_embarked(df):
df['Embarked'] = df.Embarked.fillna('S')
 return df
train_df = fill_ages(fill_embarked(train_df))
test_df = fill_ages(fill_embarked(test_df))
test_df['Fare'] = test_df.Fare.fillna(test_df.Fare.median() )<feature_engineering>
model_2.save("model_2_duo.h5")
def add_bands(df):
df['AgeBand'] = pd.cut(df.Age, bins=5)
 df['FareBand'] = pd.qcut(df.Fare, q=4)
 return df
train_df = add_bands(train_df)
 test_df = add_bands(test_df)
 train_df = train_df.drop(['Age', 'Fare'], axis=1)
 test_df = test_df.drop(['Age', 'Fare'], axis=1 )<categorify>
model_3 = keras.models.Sequential()
model_3.add(Dense(32, input_dim=len(list_feat), activation="elu", kernel_initializer="he_normal"))
model_3.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_3.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_3.add(keras.layers.Dropout(0.25))
model_3.add(Dense(256, activation="elu", kernel_initializer="he_normal"))
model_3.add(Dense(256, activation="elu", kernel_initializer="he_normal"))
model_3.add(keras.layers.Dropout(0.35))
model_3.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_3.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_3.add(Dense(32, activation="elu", kernel_initializer="he_normal"))
model_3.add(keras.layers.Dropout(0.25))
model_3.add(Dense(1, activation="sigmoid"))
model_3.compile(optimizer="RMSprop", loss='MAE', metrics=["MAE"] )
def factorize(df):
df['Sex'] = df.Sex.factorize() [0]
df['AgeBand'] = df.AgeBand.factorize(sort=True)[0]
df['FareBand'] = df.FareBand.factorize(sort=True)[0]
return df
def one_hot_encode(df):
return pd.get_dummies(df, columns=['Embarked', 'Title'])
 train_df = one_hot_encode(factorize(train_df))
test_df = one_hot_encode(factorize(test_df))
train_df.head()<prepare_x_and_y>
x_train = train.loc[train.matchType_1 == "squad", list_feat]
y_train = train.loc[train.matchType_1 == "squad", ["winPlacePerc"]]
X = train_df.drop('Survived', axis=1)
 y = train_df.Survived<train_on_grid>
model_3.fit(x=x_train, y=y_train, epochs=60, batch_size=10000, validation_split=0.2, shuffle=True)
 model_3.fit(x=x_train, y=y_train, epochs=50, batch_size=3000, validation_split=0.2, shuffle=True )
param_grid = {
'n_estimators': [90, 95, 100],
'learning_rate': [0.009, 0.01],
'max_depth': [3],
'min_child_weight' :range(1, 3),
'gamma': [0, 0.001, 0.005]
}
gsearch = GridSearchCV(cv=5, estimator=XGBClassifier() , param_grid=param_grid, n_jobs=-1)
 gsearch.fit(X, y)
 gsearch.best_params_, gsearch.best_score_ 
<train_on_grid>
param_grid = {
'C': np.arange(0.99, 1.1, 0.01)
 }
model = SVC(gamma='auto')
 gsearch = GridSearchCV(cv=5, estimator=model, param_grid=param_grid, n_jobs=-1)
 gsearch.fit(X, y)
 print(f'Best params: {gsearch.best_params_}')
 print(f'Best score: {gsearch.best_score_}')
<train_on_grid>
model_3.save("model_3_squad.h5")
param_grid = {
'n_estimators': range(200, 300, 50),
'max_depth': range(2, 5),
'min_samples_split': [2, 3, 4],
'bootstrap': [True, False]
}
model = RandomForestClassifier()
gsearch = GridSearchCV(cv=5, estimator=model, param_grid=param_grid, n_jobs=-1)
 gsearch.fit(X, y)
 print(f'Best params: {gsearch.best_params_}')
 print(f'Best score: {gsearch.best_score_}' )<choose_model_class>
model_4 = keras.models.Sequential()
model_4.add(Dense(32, input_dim=len(list_feat), activation="elu", kernel_initializer="he_normal"))
model_4.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_4.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_4.add(keras.layers.Dropout(0.25))
model_4.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_4.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_4.add(Dense(32, activation="elu", kernel_initializer="he_normal"))
model_4.add(keras.layers.Dropout(0.25))
model_4.add(Dense(1, activation="sigmoid"))
model_4.compile(optimizer="RMSprop", loss='MAE', metrics=["MAE"] )
model = SVC(C = 1.26, gamma = 0.09)
 model.fit(X, y)
 predictions = model.predict(test_df )<save_to_csv>
x_train = train.loc[train.matchType_1 == "etc", list_feat]
y_train = train.loc[train.matchType_1 == "etc", ["winPlacePerc"]]
submit_df = pd.DataFrame({
'PassengerId': test_df.index.values,
'Survived': predictions
})
 submit_df.to_csv('submission.csv', index=False )<load_from_csv>
model_4.fit(x=x_train, y=y_train, epochs=70, batch_size=10000, validation_split=0.2, shuffle=True)
 model_4.fit(x=x_train, y=y_train, epochs=150, batch_size=1000, validation_split=0.2, shuffle=True )
datrn = pd.read_csv('.. /input/train.csv')
 datst = pd.read_csv('.. /input/test.csv' )<load_from_csv>
datsub = pd.read_csv('.. /input/gender_submission.csv' )<drop_column>
model_4.save("model_4_etc.h5")
outcome = datrn['Survived']
data = datrn.drop('Survived',axis=1 )<compute_test_metric>
del(train, x_train, y_train, X )
def accuracy_score(truth, pred):

 if len(truth)== len(pred):
return "Predictions have an accuracy of {:.2f}%.".format(( truth == pred ).mean() *100)
 else:
return "Number of predictions does not match number of outcomes!"<string_transform>
test = pd.read_csv(".. /input/test_V2.csv" )
warnings.filterwarnings("ignore", category = UserWarning, module = "matplotlib")
 get_ipython().run_line_magic('matplotlib', 'inline')
 def filter_data(data, condition):

 field, op, value = condition.split(" ")
 try:
value = float(value)
 except:
value = value.strip("'"")
 if op == ">":
matches = data[field] > value
elif op == "<":
matches = data[field] < value
elif op == ">=":
matches = data[field] >= value
elif op == "<=":
matches = data[field] <= value
elif op == "==":
matches = data[field] == value
elif op == "!=":
matches = data[field] != value
else:
raise Exception("Invalid comparison operator.Only >, <, >=, <=, ==, != allowed.")
 data = data[matches].reset_index(drop = True)
 return data
def survival_stats(data, outcomes, key, filters = []):

 if key not in data.columns.values :
print("'{}' is not a feature of the Titanic data.Did you spell something wrong?".format(key))
return False
if(key == 'Cabin' or key == 'PassengerId' or key == 'Ticket'):
print("'{}' has too many unique categories to display! Try a different feature.".format(key))
return False
all_data = pd.concat([data, outcomes.to_frame() ], axis = 1)
 for condition in filters:
all_data = filter_data(all_data, condition)
 all_data = all_data[[key, 'Survived']]
plt.figure(figsize=(8,6))
if(key == 'Age' or key == 'Fare'):
all_data = all_data[~np.isnan(all_data[key])]
min_value = all_data[key].min()
max_value = all_data[key].max()
value_range = max_value - min_value
if(key == 'Fare'):
bins = np.arange(0, all_data['Fare'].max() + 20, 20)
 if(key == 'Age'):
bins = np.arange(0, all_data['Age'].max() + 10, 10)
 nonsurv_vals = all_data[all_data['Survived'] == 0][key].reset_index(drop = True)
 surv_vals = all_data[all_data['Survived'] == 1][key].reset_index(drop = True)
 plt.hist(nonsurv_vals, bins = bins, alpha = 0.6,
color = 'red', label = 'Did not survive')
 plt.hist(surv_vals, bins = bins, alpha = 0.6,
color = 'green', label = 'Survived')
 plt.xlim(0, bins.max())
 plt.legend(framealpha = 0.8)
 else:
if(key == 'Pclass'):
values = np.arange(1,4)
 if(key == 'Parch' or key == 'SibSp'):
values = np.arange(0,np.max(data[key])+ 1)
 if(key == 'Embarked'):
values = ['C', 'Q', 'S']
if(key == 'Sex'):
values = ['male', 'female']
frame = pd.DataFrame(index = np.arange(len(values)) , columns=(key,'Survived','NSurvived'))
for i, value in enumerate(values):
frame.loc[i] = [value, \
len(all_data[(all_data['Survived'] == 1)&(all_data[key] == value)]), \
len(all_data[(all_data['Survived'] == 0)&(all_data[key] == value)])]
bar_width = 0.4
for i in np.arange(len(frame)) :
nonsurv_bar = plt.bar(i-bar_width, frame.loc[i]['NSurvived'], width = bar_width, color = 'r')
 surv_bar = plt.bar(i, frame.loc[i]['Survived'], width = bar_width, color = 'g')
 plt.xticks(np.arange(len(frame)) , values)
 plt.legend(( nonsurv_bar[0], surv_bar[0]),('Did not survive', 'Survived'), framealpha = 0.8)
 plt.xlabel(key)
 plt.ylabel('Number of Passengers')
 plt.title('Passenger Survival Statistics With '%s' Feature'%(key))
plt.show()
if sum(pd.isnull(all_data[key])) :
nan_outcomes = all_data[pd.isnull(all_data[key])]['Survived']
print("Passengers with missing '{}' values: {}({} survived, {} did not survive)".format(\
key, len(nan_outcomes), sum(nan_outcomes == 1), sum(nan_outcomes == 0)) )<compute_test_metric>
print("Check The NA value in test data")
 for i in list(test.columns[test.dtypes != "O"]):
print(i, ":", sum(test[i].isna()))
survival_stats(data, outcome, 'Sex' )<compute_test_metric>
len(pd.unique(test.matchId)) , sum(test.groupby("matchId" ).size() < 9 )
survival_stats(data, outcome, 'Age', ["Sex == 'female'"] )<compute_test_metric>
temp = pd.DataFrame(test.groupby("matchId" ).size() , columns=["player"])
 temp.reset_index(level=0, inplace=True)
 test = test.merge(temp, left_on="matchId", right_on="matchId" )
survival_stats(data, outcome, "Age",["Sex == 'male'","Embarked == 'C'"] )<compute_test_metric>
test["matchType_1"] = "-"
test.loc[(test.matchType == "solo-fpp")| 
(test.matchType == "solo")| 
(test.matchType == "normal-solo-fpp")| 
(test.matchType == "normal-solo"), "matchType_1"] = "solo"
test.loc[(test.matchType == "duo-fpp")| 
(test.matchType == "duo")| 
(test.matchType == "normal-duo-fpp")| 
(test.matchType == "normal-duo"), "matchType_1"] = "duo"
test.loc[(test.matchType == "squad-fpp")| 
(test.matchType == "squad")| 
(test.matchType == "normal-squad-fpp")| 
(test.matchType == "normal-squad"), "matchType_1"] = "squad"
test.loc[(test.matchType == "flarefpp")| 
(test.matchType == "flaretpp")| 
(test.matchType == "crashfpp")| 
(test.matchType == "crashtpp"), "matchType_1"] = "etc"
survival_stats(data, outcome, "Age",["Sex == 'male'","Embarked == 'S'"] )<predict_on_test>
test["matchType_2"] = "-"
test.loc[(test.matchType == "solo-fpp")| 
(test.matchType == "duo-fpp")| 
(test.matchType == "squad-fpp")| 
(test.matchType == "normal-solo-fpp")| 
(test.matchType == "normal-duo-fpp")| 
(test.matchType == "normal-squad-fpp")| 
(test.matchType == "crashfpp")| 
(test.matchType == "flarefpp"), "matchType_2"] = "fpp"
test.loc[(test.matchType == "solo")| 
(test.matchType == "duo")| 
(test.matchType == "squad")| 
(test.matchType == "normal-solo")| 
(test.matchType == "normal-duo")| 
(test.matchType == "normal-squad")| 
(test.matchType == "crashtpp")| 
(test.matchType == "flaretpp"), "matchType_2"] = "tpp"
def predict(data):

 predictions = []
for _, passenger in data.iterrows() :
if passenger['Sex'] == 'female':
if passenger['Embarked']== 'C' and passenger['Pclass'] <=3 :predictions.append(1)
 elif passenger['Embarked']== 'S' and passenger['Pclass'] <3:predictions.append(1)
 else:
if(passenger['SibSp'] <2)and(passenger['Parch']<2):
predictions.append(1)
 else:
predictions.append(0)
 elif passenger['Sex']=='male' and passenger['Age']<15:
if passenger['SibSp'] < 3 and passenger['Embarked']=='S':predictions.append(1)
 elif passenger['Embarked']=='C':predictions.append(1)
 else:predictions.append(0)
 else:predictions.append(0)
 return pd.Series(predictions )<predict_on_test>
test["solo"] = 0
test["duo"] = 0
test["squad"] = 0
test["etc"] = 0
test.loc[test.matchType_1 == "solo", "solo"] = 1
test.loc[test.matchType_1 == "duo", "duo"] = 1
test.loc[test.matchType_1 == "squad", "squad"] = 1
test.loc[test.matchType_1 == "etc", "etc"] = 1
pred = predict(data )<compute_test_metric>
test["fpp"] = 0
test["tpp"] = 0
test.loc[test.matchType_2 == "fpp", "fpp"] = 1
test.loc[test.matchType_2 == "tpp", "tpp"] = 1
print(accuracy_score(outcome,pred))<predict_on_test>
predtst = predict(datst )<prepare_x_and_y>
Past = datst.iloc[:,0]<concatenate>
dst = []
i=0
while i<len(predtst):
dst.append(( Past[i],predtst[i]))
i+=1<create_dataframe>
d = pd.DataFrame(dst,columns=['PassengerId','Survived'] )<save_to_csv>
d.to_csv('gender_submission.csv',index=False )<set_options>
test.set_index("Id", inplace=True)
 test.index.name = "Id"
%matplotlib inline
py.init_notebook_mode(connected=True)
 warnings.filterwarnings('ignore')
 GradientBoostingClassifier, ExtraTreesClassifier)
 print(os.listdir(".. /input")) 
<load_from_csv>
temp_1 = test.loc[:, feature_1]
temp_2 = test.loc[:, feature_2]
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
temp_1.groupby("matchId" ).transform(minmax)
 for i in temp_2.columns[:4]:
temp_2[i] =(temp_2[i] - min(temp_2[i])) /(max(temp_2[i])- min(temp_2[i]))
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<data_type_conversions>
X = pd.merge(temp_1, temp_2, on="Id")
 X = pd.merge(X, test.loc[:, ["matchType_1", "winPlacePerc"]], on="Id" )
train.columns[train.isnull().any() ].tolist()<groupby>
print(train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean() )<groupby>
print(train[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean() )<groupby>
test = X
test.reset_index()
print(train[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).count() )<define_variables>
result_1 = model_1.predict(test.loc[test.matchType_1 == "solo", list_feat])
 result_2 = model_2.predict(test.loc[test.matchType_1 == "duo", list_feat])
 result_3 = model_3.predict(test.loc[test.matchType_1 == "squad", list_feat])
 result_4 = model_4.predict(test.loc[test.matchType_1 == "etc", list_feat] )
full_data = [train, test]<groupby>
temp = pd.DataFrame(test.loc[test.matchType_1 == "solo", "Id"] ).append(pd.DataFrame(test.loc[test.matchType_1 == "duo", "Id"])).append(pd.DataFrame(test.loc[test.matchType_1 == "squad", "Id"])).append(pd.DataFrame(test.loc[test.matchType_1 == "etc", "Id"]))
_ = pd.DataFrame(result_1, columns = ["winPlacePerc"] ).append(pd.DataFrame(result_2, columns = ["winPlacePerc"])).append(pd.DataFrame(result_3, columns = ["winPlacePerc"])).append(pd.DataFrame(result_4, columns = ["winPlacePerc"]))
for dataset in full_data:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
print(train[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).mean() )<feature_engineering>
result = pd.concat([temp.reset_index(drop=True), _.reset_index(drop=True)], axis=1 )
for dataset in full_data:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1 
<groupby>
np.sum(result.winPlacePerc.isna() )
print(train[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean() )<groupby>
np.sum(result.winPlacePerc < 0 )
for dataset in full_data:
dataset['Embarked'] = dataset['Embarked'].fillna('S')
 print(train[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean() )<feature_engineering>
np.sum(result.winPlacePerc > 1 )
for dataset in full_data:
avg_age = round(dataset['Age'].mean() ,2)
 dataset['Age'][np.isnan(dataset['Age'])] = avg_age
train['CategoricalAge'] = pd.cut(train['Age'], 5)
 print(train[['CategoricalAge', 'Survived']].groupby(['CategoricalAge'], as_index=False ).mean() )<feature_engineering>
result.loc[result.winPlacePerc.isna() , "winPlacePerc"] = 0
result.loc[result.winPlacePerc < 0, "winPlacePerc"] = 0
result.loc[result.winPlacePerc > 1, "winPlacePerc"] = 1
result.to_csv('submission.csv', index=False )