# Load the basic libraries...
import scipy as sp
import sklearn
import pandas as pd
%pylab inline

"""
Explanation: Example 1: Titanic survivors
End of explanation
"""


titanic_df = pd.read_csv("/home/jan/su-jupyter/data/titanic-train.csv")
titanic_df

titanic_df.drop(['PassengerId'], axis=1, inplace=True)

titanic_df.describe()

titanic_df1 = titanic_df[['Pclass', 'Sex', 'Age','Survived']]
titanic_df1

survivors = titanic_df1[titanic_df1['Survived']==1]
victims = titanic_df1[titanic_df1['Survived']==0]

scatter(titanic_df1['Age'], titanic_df1['Pclass'], 
        c=titanic_df1['Survived'], cmap='prism', marker='o', s=100, alpha=0.5);

"""
Explanation: <img src="https://upload.wikimedia.org/wikipedia/commons/f/fd/RMS_Titanic_3.jpg" width="100%">
Data

http://lib.stat.cmu.edu/S/Harrell/data/descriptions/titanic.html
https://www.kaggle.com/c/titanic/data

<PRE>
VARIABLE DESCRIPTIONS:
survival        Survival
                (0 = No; 1 = Yes)
pclass          Passenger Class
                (1 = 1st; 2 = 2nd; 3 = 3rd)
name            Name
sex             Sex
age             Age
sibsp           Number of Siblings/Spouses Aboard
parch           Number of Parents/Children Aboard
ticket          Ticket Number
fare            Passenger Fare
cabin           Cabin
embarked        Port of Embarkation
                (C = Cherbourg; Q = Queenstown; S = Southampton)

SPECIAL NOTES:
Pclass is a proxy for socio-economic status (SES)
 1st ~ Upper; 2nd ~ Middle; 3rd ~ Lower

Age is in Years; Fractional if Age less than One (1)
 If the Age is Estimated, it is in the form xx.5

With respect to the family relation variables (i.e. sibsp and parch)
some relations were ignored.  The following are the definitions used
for sibsp and parch.

Sibling:  Brother, Sister, Stepbrother, or Stepsister of Passenger Aboard Titanic
Spouse:   Husband or Wife of Passenger Aboard Titanic (Mistresses and Fiances Ignored)
Parent:   Mother or Father of Passenger Aboard Titanic
Child:    Son, Daughter, Stepson, or Stepdaughter of Passenger Aboard Titanic

Other family relatives excluded from this study include cousins,
nephews/nieces, aunts/uncles, and in-laws.  Some children travelled
only with a nanny, therefore parch=0 for them.  As well, some
travelled with very close friends or neighbors in a village, however,
the definitions do not support such relations.
</PRE>
End of explanation
"""


titanic_X = titanic_df[['Pclass', 'Sex', 'Age']].as_matrix()
titanic_y = titanic_df['Survived'].as_matrix()

shape(titanic_X), shape(titanic_y)

titanic_X

titanic_y

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()

titanic_X[:,1] = le.fit_transform(titanic_X[:,1])
print titanic_X

from sklearn.preprocessing import Imputer
imp = Imputer(missing_values='NaN', strategy='mean', axis=0)
titanic_X = imp.fit_transform(titanic_X)
print titanic_X

from sklearn import tree
clf = tree.DecisionTreeClassifier(max_depth=3)
clf = clf.fit(titanic_X, titanic_y)

titanic_y_predicted = clf.predict(titanic_X)

titanic_df.insert(1,'Survior pred', titanic_y_predicted)
titanic_df

from sklearn.metrics import accuracy_score
accuracy_score(titanic_y, titanic_y_predicted)

from sklearn.externals.six import StringIO  
import pyparsing
import pydot
from IPython.display import Image
dot_data = StringIO() 
tree.export_graphviz(clf, out_file=dot_data, feature_names=['Pclass', 'Sex', 'Age'])
graph = pydot.graph_from_dot_data(dot_data.getvalue()) 
img = Image(graph.create_png())

img.width=800; img

titanic_y_predicted_proba = clf.predict_proba(titanic_X)

titanic_df.insert(2,'Survior prob', titanic_y_predicted_proba[:,1])
titanic_df

# Pclass, Sex, Age
x_male_student = sp.array([3, 1, 21])
x_rich_countess = sp.array([1, 0, 65])
x_midleclass_mother = sp.array([2, 0, 40])
x_baby = sp.array([1, 0, 1])

clf.predict_proba(x_male_student)

clf.predict_proba(x_rich_countess)

clf.predict_proba(x_midleclass_mother)

clf.predict_proba(x_baby)

"""
Explanation: Training a classifier
End of explanation
"""


