import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression

%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'])

"""
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()

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


del df_train['Cabin']
del df_train['Embarked']
del df_train['Ticket']
del df_train['Name']
del df_train['PassengerId']

# remvoe the helper columns
del df_train['Count']
del df_train['Percentage']

df_train.columns

df_train['Sex'] = df_train['Sex'].map({'female':1,'male':0}).astype(int)

df_train['Age'] = df_train['Age'].map({'(-2, 0]':-1,'(0, 5]':1,'(5, 10]':2,'(10, 20]':3,'(20, 35]':4,'(35, 60]':5,'(60, 100]':6}).astype(int)

df_train.head()

x_train = df_train.drop("Survived",axis=1)
y_train = df_train['Survived']

logreg = LogisticRegression()
logreg.fit(x_train,y_train)
logreg.score(x_train, y_train)

"""
Explanation: I am going to remove other columns which have NA, just for a quick dirty test.
End of explanation
"""


del df_test['Cabin']
del df_test['Embarked']
del df_test['Ticket']
del df_test['Name']
test_id = df_test['PassengerId']
del df_test['PassengerId']

# Sex
df_test['Sex'] = df_test['Sex'].map({'female':1,'male':0}).astype(int)

# Age
colage = SimplyAge(df_test['Age'])
df_test['Age'] = colage
df_test['Age'] = df_test['Age'].map({'(-2, 0]':-1,'(0, 5]':1,'(5, 10]':2,'(10, 20]':3,'(20, 35]':4,'(35, 60]':5,'(60, 100]':6}).astype(int)

df_test.head()

df_test.info()

df_test[df_test['Fare'].isnull()]

"""
Explanation: Now I am going to predict in the test data.
End of explanation
"""


df_test[df_test['Pclass']==3]['Fare'].mean()

df_test['Fare'] = df_test['Fare'].fillna(df_test[df_test['Pclass']==3]['Fare'].mean())

Y_pred = logreg.predict(df_test)

ypred = pd.DataFrame()
ypred['PassengerId'] = test_id
ypred['Survived'] = Y_pred
ypred

ypred.to_csv("1st_pred.csv",index=False)

"""
Explanation: Well, the NaN in Fare is a Pclass 3, so i am going fill it with the mean of Fare of Pclass 3.
End of explanation
"""


