%matplotlib inline

import matplotlib.pyplot as plt
import pandas as pd

plt.style.use('ggplot')

# Anaconda on Windows will get warning

"""
Explanation: Titanic Survival Prediction
Data Dictionary
Here we got the data dictionary. This useful info will help you to understand the csv.
|Variable   |Definition |Key|
|:----|:-----|:----|
|survival|  Survival|   0 = No, 1 = Yes|
|pclass|    Ticket class|   1 = 1st, 2 = 2nd, 3 = 3rd|
|sex|   Sex | |
|Age|   Age in years| | 
|sibsp| # of siblings / spouses aboard the Titanic| |
|parch| # of parents / children aboard the Titanic|  |
|ticket|    Ticket number|   |
|fare|  Passenger fare|  |
|cabin| Cabin number| | 
|embarked|  Port of Embarkation|    C = Cherbourg, Q = Queenstown, S = Southampton |
Variable Notes
pclass: A proxy for socio-economic status (SES)
1st = Upper
2nd = Middle
3rd = Lower
age: Age is fractional if less than 1. If the age is estimated, is it in the form of xx.5
sibsp: The dataset defines family relations in this way...
Sibling = brother, sister, stepbrother, stepsister
Spouse = husband, wife (mistresses and fiancés were ignored)
parch: The dataset defines family relations in this way...
Parent = mother, father
Child = daughter, son, stepdaughter, stepson
Some children travelled only with a nanny, therefore parch=0 for them.
Please check https://www.kaggle.com/c/titanic/data 
Let's waggle the data!!!
Import useful modules
End of explanation
"""


df=pd.read_csv('train.csv')

"""
Explanation: Loading data from CSV
End of explanation
"""


df.head()

"""
Explanation: Show first 5 records
End of explanation
"""


df.hist( figsize=(16, 10))

"""
Explanation: Histogram
Nice tool to help you understand the data
End of explanation
"""


df.describe()

"""
Explanation: Show statistic
End of explanation
"""


df.keys()

"""
Explanation: Show list of column names
End of explanation
"""


df2=df.drop(['Name','PassengerId','Cabin','Ticket'],1)

df2.head()

"""
Explanation: Drop some unnecessary columns
End of explanation
"""


code={'female':0,'male':1}
for k,i in df2.iterrows():
    df2.loc[k,"sexc"]=code[i['Sex']]

df2.head()

"""
Explanation: From column 'Sex', create another numeric column (sexc)
Randonforest cannot process text data. All data must be numeric type.
End of explanation
"""


df2["Embarked"].value_counts()

"""
Explanation: Count number of records of each category
End of explanation
"""



code={'C':0,'Q':1, 'S':2}
for k,i in df2.iterrows():
    if i['Embarked']==i['Embarked']:
        df2.loc[k,"embarkedc"]=code[i['Embarked']]

df2.head(10)

#remove the original column
df3=df2.drop(['Sex','Embarked'],1)

"""
Explanation: From column 'Embarked', create another numeric column (embarkedc)
End of explanation
"""


df4=df3.dropna(axis=0)
df4.head(10)

"""
Explanation: Drop every NaN row
End of explanation
"""


df5=df3[ df3['Age'].notnull() ]
df5.head()

"""
Explanation: Alternative approuch--drop every NaN row using Masking
End of explanation
"""


X=df4.as_matrix([df4.columns[1:]])

y=df4.as_matrix(["Survived"])

y.shape

y=y.reshape(-1)

y.shape

X.shape

"""
Explanation: Convert Dataframe to numpy array for traning
End of explanation
"""


from sklearn.ensemble import ExtraTreesClassifier
from sklearn.cross_validation import cross_val_score

forest = ExtraTreesClassifier(n_estimators=250,random_state=0)

"""
Explanation: Construct Classifier
End of explanation
"""


cross_val_score(forest, X, y)

"""
Explanation: Lazy k-fold cross-valivation
End of explanation
"""


forest.fit(X,y)

"""
Explanation: Training
End of explanation
"""


forest.predict([3,22.0,1,0,7.2500,1.0,2.0])

forest.predict([1,38.0,1,0,71.2833,0.0,0.0])

"""
Explanation: Prediction
End of explanation
"""


#Pclass=3 (lower), Age=25 (guessing), SibSp=0, Parch=0, Fare=0.0 (free trip), sexc=1 (male), embarkedc=1 (Queenstown, guessing)
forest.predict([3,25.0,0,0,0.0,1,1])

"""
Explanation: Dummy Jack
End of explanation
"""


forest.predict([1,23.0,0,1,50.0,0,1])

"""
Explanation: --The output of prediction is 0 that means sorry for Jack, he's not survive.
Dummy Rose
End of explanation
"""


%%HTML
<img src="https://upload.wikimedia.org/wikipedia/en/b/bb/Titanic_breaks_in_half.jpg">

"""
Explanation: --But Rose has to continue.
--- The End ---
End of explanation
"""


