import numpy as np
import pandas as pd
from sklearn import svm, datasets
from sklearn.metrics import accuracy_score
from sklearn.model_selection import GridSearchCV, train_test_split

# load iris data
iris = datasets.load_iris()
X = iris.data
y = iris.target

X[:3]

y[:3]

"""
Explanation: Deep Learning - Part I
Theory
Two important figures from Chapter 5:


Practical
Training an SVM in scikit-learn and choosing its hyperparameters using cross-validation. We are using a polynomial kernel and are tuning the polynomial degree of the kernel:
$
\kappa(\mathbf{u}, \mathbf{v}) = (\mathbf{u}^T \mathbf{v} + c)^d
$
We are using the Iris flower data set first introduced by Ronald Fisher https://en.wikipedia.org/wiki/Iris_flower_data_set which contains:

50 samples
4 features (Sepal length, Sepal width, Petal length, Petal width)
3 classes
End of explanation
"""


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

"""
Explanation: Randomly select 20% of the samples as test set.
End of explanation
"""


parameters = {'degree':list(range(1, 21))}
svc = svm.SVC(kernel='poly')
clf = GridSearchCV(svc, parameters, scoring='accuracy')
clf.fit(X_train, y_train)

"""
Explanation: Using cross-validation, try out $d=1,2,\ldots,20$.
Use accuracy to determine the train/test error.
End of explanation
"""


pd.DataFrame(clf.cv_results_)

"""
Explanation: The cross-validation results can be loaded into a pandas DataFrame. We see that the model starts overfitting for polynomial degrees $>3$.
End of explanation
"""


e = clf.estimator.fit(X_train, y_train)
e

y_pred = e.predict(X_test)
accuracy_score(y_test, y_pred)

"""
Explanation: Finally, train the model with lowest mean test error in cross-validation on all training data and determine the error on the test set.
End of explanation
"""


