Randomized Searching


In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

from sklearn.datasets import load_iris
from sklearn.cross_validation import train_test_split


iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y)

In [ ]:
from sklearn.grid_search import RandomizedSearchCV

In [ ]:
from scipy.stats import expon

In [ ]:
plt.hist([expon.rvs(scale=0.001) for x in xrange(10000)], bins=100, normed=True);

In [ ]:
from sklearn.pipeline import make_pipeline
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler



param_distributions = {'C': expon(), 'gamma': expon()}
rs = RandomizedSearchCV(SVC(), param_distributions=param_distributions, n_iter=50)

In [ ]:
rs.fit(X_train, y_train)

In [ ]:
rs.best_params_

In [ ]:
rs.best_score_

In [ ]:
scores, Cs, gammas = zip(*[(score.mean_validation_score, score.parameters['C'], score.parameters['gamma']) for score in rs.grid_scores_])

In [ ]:
plt.scatter(Cs, gammas, s=50, c=scores, linewidths=0)
plt.xlabel("C")
plt.ylabel("gamma")

In [ ]:
plt.scatter(np.log(Cs), np.log(gammas), s=50, c=scores, linewidths=0)
plt.xlabel("C")
plt.ylabel("gamma")

In [ ]: