from IPython.display import Image
Image("http://blog.kaggle.com/wp-content/uploads/2015/04/iris_petal_sepal.png")

import pandas as pd
import numpy as np

from sklearn.datasets import load_iris

iris = load_iris()

"""
Explanation: Scikit Learn
http://scikit-learn.org/
A mature library for machine learning, built using tools like numpy and scipy. 

Classification
Regression
Clustering
Dimensionality reduction
Model Selection

An excellent class from PyCon in 2013, is available here:
https://github.com/jakevdp/sklearn_pycon2013
A fantastic slide to help identify what approach to use:
http://scikit-learn.org/stable/tutorial/machine_learning_map/index.html

Classification of flowers.
End of explanation
"""


X = pd.DataFrame(iris.data, columns=iris.feature_names)
X.head()

"""
Explanation: Form the feature matrix, which is the collection of measurements or observations that are associated with a particular state or label.
End of explanation
"""


Y = pd.DataFrame(np.vstack([iris.target, np.choose(iris.target, 
                                                   iris.target_names)]).T,
                 columns=["label id", "label name"])
y = Y["label id"]

Y.head()

"""
Explanation: Form the set of labels, the classification of the state based on the measurements.
End of explanation
"""


print("There are {} measurements and {} classes of flower.".format(X.size, Y["label name"].unique().size))

"""
Explanation: Q. How many measurements and how many classes of flower?
End of explanation
"""


from sklearn import cross_validation

X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.4, random_state=0)

"""
Explanation: Best practice suggests that we should always partition our data into train, test and validation sets. It is very common to do this using randomized partitioning.
http://scikit-learn.org/stable/modules/cross_validation.html
End of explanation
"""


from sklearn.svm import LinearSVC

clf = LinearSVC()

clf.fit(X_train, y_train)

"""
Explanation: Using a simple linear classifier, lets train the model and evaluate the performance on the test set.
End of explanation
"""


clf.score(X_test, y_test)

"""
Explanation: We can look at how the classifier performed on the test set using the classifiers "score" method. This reports the mean accuracy of the classifier.
End of explanation
"""


from sklearn.metrics import classification_report

print(classification_report(y_test, clf.predict(X_test), target_names=iris.target_names))

"""
Explanation: Measuring accuracy alone ignores other important features of a classifier:
http://scikit-learn.org/stable/modules/model_evaluation.html
End of explanation
"""


