Import libraries.
In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
Load training data and test data.
In [2]:
df_train = pd.read_csv('./train.csv')
df_test = pd.read_csv('./test.csv')
Well, I don't really have any idea how to handle these data. So let's just take a look at them. Let's start from the trainning data.
In [3]:
df_train.head()
Out[3]:
In [4]:
df_train.describe()
Out[4]:
In [5]:
df_train.info()
In [6]:
df_train.isnull().sum()
Out[6]:
Hmm... There are some data missing. Age could be an important feature. Cabin seems like a useless feature and I am going to discard it. Well, my 1st question, how do you decide which feature to be used and which not?
After i read other people's analysis, they show me this:
In [7]:
df_train.describe(include=['O'])
Out[7]:
Hmm... Seems some people share one cabin. Is it the case that people in one cabin help each other and increase the survive chance? But the cabin has too less data. Also, the ticket number is shared by upto 7 people, which means they are a group? And they will more likely help each other and increase the survive chance?
Among 891 row, 577 are Male and 314 Female.
Now, do the same thing to the test data.
In [8]:
df_test.head()
Out[8]:
In [9]:
df_test.describe()
Out[9]:
In [10]:
df_test.describe(include=['O'])
Out[10]:
In [11]:
df_test.info()
In [12]:
sns.countplot(x='Survived', data=df_train)
plt.show()
In [32]:
df_train['Percentage'] = 1 # this is a helper colume
df_train[['Percentage','Survived']].groupby('Survived').count().apply(lambda x: (100 * x)/x.sum())
Out[32]:
In [14]:
df_train[['Pclass','Survived']].groupby('Pclass').mean()
Out[14]:
In [34]:
df_train['Count'] = 1 # this is a helper colume
df_train[['Pclass','Survived','Count']].groupby(['Pclass','Survived']).count()
Out[34]:
In [16]:
df_train[['Sex','Survived']].groupby('Sex').mean()
Out[16]:
In [35]:
df_train[['Sex','Survived','Count']].groupby(['Sex','Survived']).count()
Out[35]:
In [36]:
df_train[['Pclass','Sex','Survived','Count']].groupby(['Pclass','Sex','Survived']).count()
Out[36]:
In [19]:
df_train[['Pclass','Sex','Survived']].groupby(['Pclass','Sex']).mean()
Out[19]:
The female survive rate in Pclass 1 and 2 are similar, but Pclass 3 is way lower. Well, the story is the gate from Pclass 3 to the deck was locked at the very beginning. That's sad...
The male survive rate in Pclass 2 and 3 are similar, but Pclass 1 is way higher.
In [20]:
sns.boxplot(x='Survived', y='Age', hue='Sex',data=df_train, palette="coolwarm")
plt.show()
In [21]:
sns.barplot(x='Pclass', y='Survived', hue='Sex',data=df_train,estimator=np.sum)
plt.show()
In [ ]: