import numpy as np, pandas as pd
import matplotlib.pyplot as plt, ipywidgets, mpld3
plt.style.use('fivethirtyeight')
%matplotlib inline
import sklearn.svm, sklearn.grid_search, sklearn.datasets.samples_generator
parameters = {'kernel':('poly', 'rbf'), 'C':[.01, .1, 1, 10, 100, 1000]}
clf = sklearn.grid_search.GridSearchCV(
sklearn.svm.SVC(probability=True),
parameters,
cv=5,
n_jobs=4)
X, y = sklearn.datasets.samples_generator.make_classification(n_samples=200, n_features=5, random_state=12345)
%time clf.fit(X, y)
clf.best_params_
Wall time: 4.03 s
{'C': 100, 'kernel': 'rbf'}
def show_search_results(C, kernel):
cv_scores = []
x_pos = []
x_i = 0
for i, gs_i in enumerate(clf.grid_scores_):
if gs_i.parameters['C'] not in C or gs_i.parameters['kernel'] not in kernel:
continue
reps = len(gs_i.cv_validation_scores)
pts, = plt.plot([x_i for _ in range(reps)], gs_i.cv_validation_scores, 'o', ms=25, alpha=.5)
cv_scores.append(gs_i.cv_validation_scores)
x_pos.append(x_i)
x_i += 1
labels = ['<h1>{} (rep {})</h1>'.format(gs_i.parameters, j) for j in range(reps)]
tt = mpld3.plugins.PointHTMLTooltip(pts, labels, hoffset=10)
mpld3.plugins.connect(plt.gcf(), tt)
plt.plot(np.array(cv_scores), color='grey', zorder=-5)
plt.axis([-.5, x_i-.5, -.1, 1.1])
return mpld3.display()
show_search_results(**clf.param_grid)
# NOTE: you need to run this in your own notebook to see the interactive widgets
ipywidgets.interact(show_search_results,
C=ipywidgets.SelectMultiple(options=clf.param_grid['C'],
selected_labels=clf.param_grid['C']),
kernel=ipywidgets.SelectMultiple(options=clf.param_grid['kernel'],
selected_labels=['rbf']),
)
<function __main__.show_search_results>