import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline

"""
Explanation: Import libraries.
End of explanation
"""


df_train = pd.read_csv('../input/train.csv')
df_test = pd.read_csv('../input/test.csv')

"""
Explanation: Load training data and test data.
End of explanation
"""


df_train.head()

df_train.describe()

df_train.info()

df_train.isnull().sum()

"""
Explanation: 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.
End of explanation
"""


df_train.describe(include=['O'])

"""
Explanation: 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:
End of explanation
"""


df_test.head()

df_test.describe()

df_test.describe(include=['O'])

df_test.info()

"""
Explanation: 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.
End of explanation
"""


sns.countplot(x='Survived', data=df_train)
plt.show()

df_train['Percentage'] = 1 # this is a helper colume
df_train[['Percentage','Survived']].groupby('Survived').count().apply(lambda x: (100 * x)/x.sum())

"""
Explanation: Relationship between Features and Survival
Let's find out each feature's contribution to the survival. Well, maybe it is not correct. The feature might not contribute to the survival, just co-relate to it. :-)
Let's checkout the total survive rate
End of explanation
"""


df_train[['Pclass','Survived']].groupby('Pclass').mean()

df_train['Count'] = 1 # this is a helper colume
df_train[['Pclass','Survived','Count']].groupby(['Pclass','Survived']).count()

"""
Explanation: Pclass vs. Survived
End of explanation
"""


df_train[['Sex','Survived']].groupby('Sex').mean()

df_train[['Sex','Survived','Count']].groupby(['Sex','Survived']).count()

"""
Explanation: Apparantly, the smaller Pclass, the higher survive rate. Richer is better, right?
Sex vs. Survived
End of explanation
"""


df_train[['Pclass','Sex','Survived','Count']].groupby(['Pclass','Sex','Survived']).count()

df_train[['Pclass','Sex','Survived']].groupby(['Pclass','Sex']).mean()

"""
Explanation: Female has way higher survive rate than male. Sex is another strong corelate feature to the survival.
Pclass and Sex vs. Survived
Now let's combine the Pclass and Sex to see what we got.
End of explanation
"""


sns.boxplot(x='Survived', y='Age', hue='Sex',data=df_train, palette="coolwarm")
plt.show()

def SimplyAge(colage):
    colage = colage.fillna(-1)
    bins = (-2,0,5,10,20,35,60,100)
    colage = pd.cut(colage,bins)
    return colage

colage = SimplyAge(df_train['Age'])

# for test
df_train['Age'] = colage
dfage = df_train 
#dfage = pd.DataFrame()
#dfage['Age'] = colage
#dfage['Survived'] = df_train['Survived']


df_train[['Age','Survived','Count']].groupby(['Age','Survived']).count()

df_train[['Age','Survived']].groupby('Age').mean()

"""
Explanation: 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.
Age vs. Survived
Let's take a look at the age. Many of the age information are missed. We will be need to fill it up.
End of explanation
"""


df_train[['Age','Survived','Sex','Count']].groupby(['Age','Sex','Survived']).count()

df_train[['Age','Sex','Survived']].groupby(['Age','Sex']).mean()

#sns.countplot(x='Age',hue='Survived',data=dfage)
sns.countplot(x='Age',data=dfage,color='Red')
sns.barplot(x='Age',y='Survived',data=dfage,estimator=np.sum,color='Blue')

sns.barplot(x='Age',y='Count',hue='Survived',data=dfage,estimator=np.sum)

sns.barplot(x='Pclass', y='Survived', hue='Sex',data=df_train,estimator=np.sum)
plt.show()

sns.countplot(x='Fare',data=df_train)
plt.show()

"""
Explanation: Well, the babys look have highest survive rate.
End of explanation
"""


