Kaggle Titanic data set - Top 2% guide (Part 01)

##Part 01
####Introduction

There are many data set for classification tasks. I think the Titanic data set on Kaggle is a great data set for the machine learning beginners.
Titanic wreck is one of the most famous shipwrecks in history. Titanic sank after crashing into an iceberg. There was a **2,224** total number of people inside the ship. Unfortunately, **1502** people died due to a lack of lifeboats. People need to have some luck to escape from that kind of tragic situation. But if we analyze the data bit deeply, we can recognize some group of people was more likely to survive than others.

First of all, we need to import several Python libraries such as numpy, pandas, sklearn, seaborn, etc.

```titanic_demo.py
import numpy as np
import pandas as pd
import pandas_profiling
from pprint import pprint

import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, VotingClassifier
from sklearn.ensemble import AdaBoostClassifier, BaggingClassifier, ExtraTreesClassifier

from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, log_loss
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score, StratifiedKFold, learning_curve
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC, LinearSVC, NuSVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.linear_model import LogisticRegression,SGDClassifier
from sklearn.tree import DecisionTreeClassifier

```
I'm using **pandas_profiling** to get a quick insight into the data. If you guys don't have pandas_profiling, you may need to install it using pip

#### Explorer the data
I loaded my datasets to pandas data frames.

```titanic_demo.py
train_data = pd.read_csv('./data/titanic_train.csv')
test_data = pd.read_csv('./data/titanic_test.csv')
train_data.head()
```
![Screen Shot 2020-02-18 at 11.56.03.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/d1305f50-0f59-a7b7-ac68-2e9054c99e4a.png)

The next step is to inspect the dataset we have got. There are a few ways to get an initial look at the data. Here I use pandas_profiling. We can effortlessly get an overview of our variables, including missing values.

```titanic_demo.py
pandas_profiling.ProfileReport(train_data)
```
![Screen Shot 2020-02-18 at 11.58.01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/5bdd66fa-7f3d-3fed-d0fd-893c5c7304c6.png)

Here we have 12 variables, including our target which named Survived. So we have 11 features to use for the model. Some of those features can be very important and some of the features can be not much useful.
The following table will give us an explanation of our features.
![Screen Shot 2020-02-18 at 11.58.57.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/47a2a53d-1ea9-cb4d-4d84-8337699faccb.png)

We can see that there are 5 categorical features in our dataset. 
    1. Cabin
    2. Embarked
    3. Sex
    4. Pclass
    5. Survived

and also there are Numeric features,
    1. PassengerId
    2. SibSp
    3. Parch
    4. Age
    5. Fare
   
and text variables.
1. Ticket
2. Name

    
We can further divide categorical features into Nominal and Ordinal.

- Nominal: Unordered categories that are mutually exclusive.
    Ex: Sex (male, female)
- Ordinal: Ordered categories that are mutually exclusive.
    Ex: Pclass (1 = 1st, 2 = 2nd, 3 = 3rd)

This dataset is not much messy, but when you are dealing with NLP, messy data can be a tremendous headache. As we have a kind of idea about our data, next we should inspect the missing data.

##### Dealing with missing values
I created a simple function to get missing values os the features in our dataset.

```titanic_demo.py
def get_missing_values(df):
    values = df.isnull().sum().sort_values()
    data = pd.DataFrame(values, columns=['Total missing values'])
    data = data[data.values.sum(axis=1) > 0]
    
    return data
get_missing_values(train_data)
```
![Screen Shot 2020-02-18 at 12.09.09.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/9cdc1c45-890a-c6b9-87ee-9b46caaf54e4.png)

```titanic_demo.py
get_missing_values(test_data)
```
![Screen Shot 2020-02-18 at 12.09.45.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/bc05d766-7234-206f-4a03-c3b9b42222c8.png)
We can see that both our training and testing dataset have some amount of missing values. First, we need to think about a good strategy to fill those missing values. for example, we can take the mean and fill those values. Or we can think about a better way to fill those than the mean values.

From here, There are two ways to fill the elements.
1. Consider the data set separately. 
2. Combine data set and deal with once.
Here, I selected the second method.

```titanic_demo.py
combined_df = train_data.append(test_data, ignore_index=True, sort=True)
get_missing_values(combined_df)
```
![Screen Shot 2020-02-18 at 12.12.37.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/69b6442b-4f86-ad4e-1ea4-d22a6eedd4ab.png)
OK, now we can see all the missing values we have on our dataset.  Let's begin with the Embarked.

######Embarked Feature
```titanic_demo.py
def values(df, feature_name):
    return pd.DataFrame(df[feature_name].value_counts(dropna=False))
values(combined_df, 'Embarked')
```
![Screen Shot 2020-02-18 at 12.16.18.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/85d03a1a-0390-6afa-1bb2-5e2f3e89f018.png)
We can see that two missing values in the Embarked feature. **Since its just two values, I am going to fill it based on the majority values. If there are significant values, we should come with a better strategy than the above method.**
For example, There can be a relationship between Fare or passenger class.
Before filling those missing values, let's have look who is missing the Embarked feature.

```titanic_demo.py
combined_df[combined_df['Embarked'].isnull()]
```
![Screen Shot 2020-02-18 at 12.17.52.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/a421fae1-cf30-fb7b-b060-a936df2584d6.png)
Oh, it is very similar, they both survived, belong to ticket class 1, both are females, and ticket fare is the same. Since they share with the same ticket number, they should have some relationship. 

```titanic_demo.py
combined_df['Embarked'].fillna('S', inplace=True)
```
Great, we just finished dealing with one missing feature. Let's move to the next feature Fare.

###### Fare Feature
```titanic_demo.py
combined_df[combined_df['Fare'].isnull()]
```
![Screen Shot 2020-02-18 at 12.22.00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/4951205a-079e-1e88-bf63-39e11fb58684.png)
We have only one missing value. So, it can fill with the mean value of the fare. If we have more missing values, we can use the following approach. 

```titanic_demo.py
combined_df.groupby(['Embarked', 'Pclass', 'Sex'])['Fare'].mean()
```
![Screen Shot 2020-02-18 at 12.23.22.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/35550d29-3817-011e-1073-ae4a251f3dae.png)
Our missing Fare value person is a male and Embarked from S and belongs to the passenger ticket class 3. So mean are of this group is 13.14

```titanic_demo.py
combined_df['Fare'].fillna(13.14, inplace=True)
```

The article became too long. So,  I will continue this in next post.


## Next
- [Kaggle Titanic data set - Top 2% guide (Part 02)](https://qiita.com/qualitia_cdev/items/093ec0bba76f2a1ad2ab)


## Table of contents
1. [Kaggle Titanic data set - Top 2% guide (Part 01)](https://qiita.com/qualitia_cdev/items/c36f3cefe55501ae3122)
2. [Kaggle Titanic data set - Top 2% guide (Part 02)](https://qiita.com/qualitia_cdev/items/093ec0bba76f2a1ad2ab)
3. [Kaggle Titanic data set - Top 2% guide (Part 03)](https://qiita.com/qualitia_cdev/items/b5cf360c7c46b05b8343)
4. [Kaggle Titanic data set - Top 2% guide (Part 04)](https://qiita.com/qualitia_cdev/items/2b28383a34115fd8790b)
5. [Kaggle Titanic data set - Top 2% guide (Part 05)](https://qiita.com/qualitia_cdev/items/1d9fdf4ee3884af5c3f1)


*本記事は @qualitia_cdevの中の一人、@nuwanさんに作成していただきました。
*This article is written by @nuwan a member of @qualitia_cdev.