# Učitaj osnovne biblioteke...
import scipy as sp
import sklearn
import pandas as pd
%pylab inline

"""
Explanation: Sveučilište u Zagrebu<br>
Fakultet elektrotehnike i računarstva
Strojno učenje
<a href="http://www.fer.unizg.hr/predmet/su">http://www.fer.unizg.hr/predmet/su</a>
Ak. god. 2015./2016.
Bilježnica 1: Uvodni primjeri
(c) 2015 Jan Šnajder
<i>Verzija: 0.8 (2015-10-15)</i>
End of explanation
"""


titanic_df = pd.read_csv("../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: Sadržaj:


Klasifikacija: Preživjeli s Titanika


Regresija: Cijene nekretnina u Bostonu


Grupiranje: Rukom pisane znamenke


Klasifikacija: Preživjeli s Titanika
<img src="https://upload.wikimedia.org/wikipedia/commons/f/fd/RMS_Titanic_3.jpg" width="100%">
Podatci

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


from sklearn import datasets
boston = datasets.load_boston()
print boston.DESCR

boston_df = pd.DataFrame(boston.data, 
    columns=['CRIM','ZN','IDUS','CHAS','NOX','RM','AGE','DIS','RAD','TAX','PTRATIO','B','LSTAT'])
boston_df.insert(13, 'Price', boston.target)
boston_df

scatter(boston_df['RM'], boston_df['Price']);

"""
Explanation: Regresija: Cijene nekretnina u Bostonu
<img src="http://cdn1.bostonmagazine.com/wp-content/uploads/2014/03/shutterstock_138401321-1.jpg" width="100%">
Podatci
End of explanation
"""


boston_X = boston.data
boston_y = boston.target
shape(boston_X)

from sklearn.linear_model import Ridge
h = Ridge(alpha=1.0)
h.fit(boston_X, boston_y)

boston_y_predicted = h.predict(boston_X)

boston_df.insert(14, 'Price predicted', boston_y_predicted)
boston_df

"""
Explanation: Treniranje regresijskog modela
End of explanation
"""


digits = sklearn.datasets.load_digits()

print digits.DESCR

digits_X = digits.data
digits_y = digits.target

digits_X

shape(digits_X)

x = digits_X[0]; x

gray()
def show_digit(x) : 
    matshow(x.reshape(8,8))
    return

show_digit(x)

"""
Explanation: Grupiranje: Rukom pisane znamenke
<img src="https://mathematicaforprediction.files.wordpress.com/2013/08/digitimageswithzenbrush-testset.jpg" width="100%">
Podatci
End of explanation
"""


from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=10)
kmeans.fit(digits_X)

digits_y_predicted = map(lambda x : kmeans.predict(x)[0], digits_X)
print digits_y_predicted

from sklearn.decomposition import PCA
digits_X_reduced = PCA(n_components=2).fit_transform(digits_X)

shape(digits_X_reduced)

scatter(digits_X_reduced[:,0], digits_X_reduced[:,1], c=digits_y_predicted, cmap='prism');

kmeans.cluster_centers_

map(show_digit, kmeans.cluster_centers_);

"""
Explanation: Grupiranje algoritmom k-srednjih vrijednosti
End of explanation
"""


