from __future__ import print_function

from sklearn import __version__ as sklearn_version
print('Sklearn version:', sklearn_version)

"""
Explanation: Sklearn basic example
Fit a simple classification model to the iris database
End of explanation
"""


from sklearn import datasets

iris = datasets.load_iris()
print(iris.DESCR)

# Print some data lines
print(iris.data[:10])
print(iris.target)

#Randomize and separate train & test
from sklearn.utils import shuffle
X, y = shuffle(iris.data, iris.target, random_state=0)

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=0)

print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)

"""
Explanation: Load data
End of explanation
"""


# Linear model 
from sklearn.linear_model import LogisticRegression

# Define classifier
clf_logistic = LogisticRegression()

# Fit classifier
clf_logistic.fit(X_train, y_train)

# Evaluate accuracy in test
from sklearn.metrics import accuracy_score

# Predict test data
y_test_pred = clf_logistic.predict(X_test)

# Evaluate accuracy
print('Accuracy test: ', accuracy_score(y_test, y_test_pred))


"""
Explanation: Linear model
End of explanation
"""


from sklearn import tree

# Define classifier
clf_tree = tree.DecisionTreeClassifier(max_depth=3)

# Fit
clf_tree.fit(X_train, y_train)

# Evaluate test accuracy
print('Tree accuracy test: ', accuracy_score(y_test, clf_tree.predict(X_test)))


"""
Explanation: Decision tree
- Build a second decision tree model to compare with the previous linear model
- Print Accuracy and ROC area
End of explanation
"""


# Configure model
from sklearn import svm
clf_svc = svm.LinearSVC()

# Fit over train
clf_svc.fit(X_train, y_train)

# Accuracy score over test
print(accuracy_score(y_test, clf_svc.predict(X_test)))

"""
Explanation: Test another clasifier

Based on the Sklearn algorithm cheat-sheet
Linear SVC: http://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html#sklearn.svm.LinearSVC
End of explanation
"""


# ROC area

# Print probabilities
y_test_proba = clf_logistic.predict_proba(X_test)
print(y_test_proba[:5])


#Recode y from multiclass labels to binary labels
from sklearn import preprocessing
lb = preprocessing.LabelBinarizer()
lb.fit(y_train)
print('Test classes: ',lb.classes_)
y_test_bin = lb.transform(y_test)
print(y_test_bin[:5])


# Roc curve
from sklearn.metrics import roc_auc_score
print('Average ROC area: ', roc_auc_score(y_test_bin, y_test_proba))



"""
Explanation: ROC area
End of explanation
"""


