I typically use Pipeline
to do it. You can create list of pipelines including SVR
model (and others if you want). Then, you can apply GridSearchCV
where putting pipeline
in as your argument.
Here, you can add params_grid
where searching space can be defined as pipelinename__paramname
(double underscore in between). For example, I have pipeline name svr
and I want to search on parameter C
, I can put the key in my parameter dictionary as svr__C
.
from sklearn.pipeline import Pipeline
from sklearn.model_selection import cross_val_score, GridSearchCV
from sklearn.svm import SVR
c_range = np.arange(1, 10, 1)
pipeline = Pipeline([('svr', SVR())])
params_grid = {'svr__C': c_range}
# grid search with 3-fold cross validation
gridsearch_model = GridSearchCV(pipeline, params_grid,
cv=3, scoring='neg_mean_squared_error')
Then, you can do the same procedure by fitting training data and find best score and parameters
gridsearch_model.fit(X_train, y_train)
print(gridsearch_model.best_params_, gridsearch_model.best_score_)
You can also use cross_val_score
to find the score:
cross_val_score(gridsearch_model, X_train, y_train,
cv=3, scoring='neg_mean_squared_error')
Hope this helps!