Title : Titanic - Machine Learning from Disaster¶

Description:¶

The objective of this code is to build a predictive model using XGBoostClassifier that answers the question: “what sorts of people were more likely to survive?” using passenger data (ie name,age,gender,socio-economic class,etc).

Load titanic dataset¶

In [1]:
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session

Read titanic train dataset and store it in train_data¶

In [2]:
train_data = pd.read_csv("./titanic/train.csv")
train_data.head()
Out[2]:
PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
0 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.2500 NaN S
1 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 0 PC 17599 71.2833 C85 C
2 3 1 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.9250 NaN S
3 4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 0 113803 53.1000 C123 S
4 5 0 3 Allen, Mr. William Henry male 35.0 0 0 373450 8.0500 NaN S

Read titanic test dataset and store it in test_data¶

In [3]:
test_data = pd.read_csv("./titanic/test.csv")
test_data.head()
Out[3]:
PassengerId Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
0 892 3 Kelly, Mr. James male 34.5 0 0 330911 7.8292 NaN Q
1 893 3 Wilkes, Mrs. James (Ellen Needs) female 47.0 1 0 363272 7.0000 NaN S
2 894 2 Myles, Mr. Thomas Francis male 62.0 0 0 240276 9.6875 NaN Q
3 895 3 Wirz, Mr. Albert male 27.0 0 0 315154 8.6625 NaN S
4 896 3 Hirvonen, Mrs. Alexander (Helga E Lindqvist) female 22.0 1 1 3101298 12.2875 NaN S

Steps to follow¶

step1 : Using "Pclass","Sex","SibSp","Age","Fare","Embarked" features to build a model
step2 : Replacing the string values with numerics of 'Sex' feature in train_data
step3 : Replacing the string values with numerics of 'Embarked' feature in train_data
step4 : Extracting the above columns from train_data store it in X
step5 : Extract the class('Survived') column from train_data and store it in y
step6 : repeat steps from 2 to 4 for test_data
step7 : Using XGBoostClassifier with finely tuned hyperparameters to build titanic disaster predictive model 
step8 : Fit the model using X and y
step9 : predict the test_y for X_test using the fitted model
In [4]:
import numpy as np
import xgboost as xgb

lsfeatures = ["Pclass","Sex","SibSp","Age","Fare","Embarked"]

train_data['Sex'].replace(['male','female'],[0,1], inplace = True)
train_data['Embarked'].replace(['S','C','Q'],[0,1,2], inplace = True)
X = train_data[lsfeatures]
Y = train_data['Survived']

test_data['Sex'].replace(['male','female'],[0,1], inplace = True)
test_data['Embarked'].replace(['S','C','Q'],[0,1,2], inplace = True)
X_test = test_data[lsfeatures]


model = xgb.XGBClassifier(n_estimators = 30, 
                      learning_rate=0.1, 
                      max_depth= 10,
                      colsample_bytree =0.9,
                      use_label_encoder=False, random_state =20)
model.fit(X, Y)
predictions = model.predict(X_test)

output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
output.to_csv('submission.csv', index=False)
print("Your submission was successfully saved!")
Your submission was successfully saved!