Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<filter>
warnings.filterwarnings("ignore" )
feature_imp[feature_imp['Value']==0]<drop_column>
warnings.filterwarnings("ignore" )
del X_train, X_validation, y_train, y_validation, feature_imp
gc.collect()<load_from_csv>
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
test_x = pd.read_csv('.. /input/test_V2.csv', engine='c' )<concatenate>
dataset = pd.concat([train,test],axis=0 ).reset_index(drop=True)
 dataset = dataset.fillna(np.nan )
test_x = df_footprint_reduce(test_x, skip_obj=True)
 gc.collect()<feature_engineering>
dataset.isnull().sum(axis = 0 )
test_x = feature_engineering(test_x, False)
 gc.collect()<predict_on_test>
dataset["Family"] = dataset["SibSp"] + dataset["Parch"] + 1
train["Family"] = train["SibSp"] + train["Parch"] + 1
test["Family"] = test["SibSp"] + test["Parch"] + 1
pred_test = model.predict(test_x, num_iteration=model.best_iteration)
 del test_x
gc.collect()<load_from_csv>
dataset = dataset.drop(columns=["SibSp","Parch"])
 train = train.drop(columns=["SibSp","Parch"])
 test = test.drop(columns=["SibSp","Parch"] )
test_set = pd.read_csv('.. /input/test_V2.csv', engine='c' )<merge>
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))
submission = pd.read_csv(".. /input/sample_submission_V2.csv")
 submission['winPlacePerc'] = pred_test
submission.loc[submission.winPlacePerc < 0, "winPlacePerc"] = 0
submission.loc[submission.winPlacePerc > 1, "winPlacePerc"] = 1
submission = submission.merge(test_set[["Id", "matchId", "groupId", "maxPlace", "numGroups"]], on="Id", how="left")
 submission_group = submission.groupby(["matchId", "groupId"] ).first().reset_index()
submission_group["rank"] = submission_group.groupby(["matchId"])["winPlacePerc"].rank()
submission_group = submission_group.merge(
 submission_group.groupby("matchId")["rank"].max().to_frame("max_rank" ).reset_index() ,
on="matchId", how="left")
 submission_group["adjusted_perc"] =(submission_group["rank"] - 1)/(submission_group["numGroups"] - 1)
 submission = submission.merge(submission_group[["adjusted_perc", "matchId", "groupId"]], on=["matchId", "groupId"], how="left")
 submission["winPlacePerc"] = submission["adjusted_perc"]
submission.loc[submission.maxPlace == 0, "winPlacePerc"] = 0
submission.loc[submission.maxPlace == 1, "winPlacePerc"] = 1
subset = submission.loc[submission.maxPlace > 1]
gap = 1.0 /(subset.maxPlace.values - 1)
 new_perc = np.around(subset.winPlacePerc.values / gap)* gap
submission.loc[submission.maxPlace > 1, "winPlacePerc"] = new_perc
submission.loc[(submission.maxPlace > 1)&(submission.numGroups == 1), "winPlacePerc"] = 0
assert submission["winPlacePerc"].isnull().sum() == 0
submission[["Id", "winPlacePerc"]].to_csv("submission.csv", index=False )<import_modules>
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} )
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<load_from_csv>
dataset["Fare"] = dataset["Fare"].fillna(dataset["Fare"].median())
 train["Fare"] = train["Fare"].fillna(dataset["Fare"].median())
 test["Fare"] = test["Fare"].fillna(dataset["Fare"].median() )
train = pd.read_csv(".. /input/train_V2.csv" )<create_dataframe>
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))
pd.DataFrame(train.dtypes, columns=["Type"] )<count_unique_values>
dataset.Embarked = dataset.Embarked.fillna('S')
 train.Embarked = train.Embarked.fillna('S')
 test.Embarked = test.Embarked.fillna('S' )
print("Number of record:", len(train), "
Number of Unique Id:", len(pd.unique(train.Id)) )<count_unique_values>
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
print("Number of match: ", len(pd.unique(train.matchId)) , "
Number of match(<9): ", sum(train.groupby("matchId" ).size() < 9))<groupby>
dataset = dataset.drop(columns=["Name"])
 train = train.drop(columns=["Name"])
 test = test.drop(columns=["Name"] )
temp = train.loc[train.matchId.isin(train.groupby("matchId" ).size() [train.groupby("matchId" ).size() < 9].index), :]
temp.loc[temp.matchId == "e263f4a227313a"]<create_dataframe>
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
temp = pd.DataFrame(train.groupby("matchId" ).size() , columns=["player"])
 temp.reset_index(level=0, inplace=True )<merge>
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'} )
train = train.merge(temp, left_on="matchId", right_on="matchId" )<count_unique_values>
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
print("Type: ", pd.unique(train.matchType), "
Count: ", len(pd.unique(train.matchType)) )<feature_engineering>
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))
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"<feature_engineering>
medians = pd.DataFrame(dataset.groupby(['Pclass', 'Title'])['Age'].median())
 medians
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"<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
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<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
train["fpp"] = 0
train["tpp"] = 0
train.loc[train.matchType_2 == "fpp", "fpp"] = 1
train.loc[train.matchType_2 == "tpp", "tpp"] = 1<filter>
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
print(list(train.columns[train.dtypes != "O"]))<define_variables>
x_train = train.loc[:, ~train.columns.isin(['PassengerId', 'Survived', 'Sex'])]
y_train = train.Survived
x_test = test.loc[:, ~test.columns.isin(['PassengerId', 'Survived', 'Sex'])]
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"]<define_variables>
x_train = pd.get_dummies(x_train)
 x_train["Sex"] = train.Sex
x_test = pd.get_dummies(x_test)
 x_test["Sex"] = test.Sex
feature_1 = ["matchId", "assists", "boosts", "damageDealt", "DBNOs", "headshotKills", "heals",
"killPlace", "killPoints", "kills", "killStreaks", "longestKill",
"revives", "rideDistance", "roadKills", "swimDistance", "teamKills",
"vehicleDestroys", "walkDistance", "weaponsAcquired", "winPoints"]<define_variables>
rf = RandomForestClassifier()
rf.fit(x_train, y_train )
feature_2 = ["matchDuration", "maxPlace", "rankPoints", "player", "fpp", "tpp"]<count_missing_values>
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_
for i in list(train.columns[train.dtypes != "O"]):
print(i, ":", sum(train[i].isna()))<feature_engineering>
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_
<feature_engineering>
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_
<count_missing_values>
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_
np.sum(train.winPlacePerc.isna() )<filter>
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 = train.loc[train.winPlacePerc.notna() , :]<train_model>
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_
<categorify>
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_
<create_dataframe>
pred = v_best.predict(x_test)
 submission = pd.DataFrame(test.PassengerId)
 submission["Survived"] = pd.Series(pred )
<feature_engineering>
submission.to_csv("submission.csv",index=False )