Sklearn: use pipeline in RandomizedSearchCV?

I would like to be able to use pipelines in the construction of RandomizedSearchCV in sklearn. However, now I believe that only estimates are supported. Here is an example of what I would like to do:

import numpy as np

from sklearn.grid_search import RandomizedSearchCV
from sklearn.datasets import load_digits
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler    
from sklearn.pipeline import Pipeline

# get some data
iris = load_digits()
X, y = iris.data, iris.target

# specify parameters and distributions to sample from
param_dist = {'C': [1, 10, 100, 1000], 
          'gamma': [0.001, 0.0001], 
          'kernel': ['rbf', 'linear'],}

# create pipeline with a scaler 
steps = [('scaler', StandardScaler()), ('rbf_svm', SVC())]
pipeline = Pipeline(steps)

# do search
search = RandomizedSearchCV(pipeline, 
param_distributions=param_dist, n_iter=50)
search.fit(X, y)

print search.grid_scores_

If you just ran this, you will get the following error:

ValueError: Invalid parameter kernel for estimator Pipeline

Is there a good way to do this in sklearn?

+4
source share
1 answer

RandomizedSearchCVAnd also GridSearchCV, do support conveyors (in fact, they do not depend on their implementation, and conveyors are designed so that they are equivalent to conventional classifiers).

, , . ( + ), , . , - , / /.

, , , , gamma ( ), gamma , rbf_svm ( ). , sklearn :

param_dist = {
          'rbf_svm__C': [1, 10, 100, 1000], 
          'rbf_svm__gamma': [0.001, 0.0001], 
          'rbf_svm__kernel': ['rbf', 'linear'],
}
+9

All Articles