2
votes

I am having trouble to access the coefficients of a support vector regression model (SVR) in scikit learn when the model is embedded in a pipeline and a grid search. Consider the following example:

from sklearn.datasets import load_iris
import numpy as np
from sklearn.grid_search import GridSearchCV
from sklearn.svm import SVR
from sklearn.feature_selection import SelectKBest
from sklearn.pipeline import Pipeline

iris = load_iris()
X_train = iris.data
y_train = iris.target

clf = SVR(kernel='linear')
select = SelectKBest(k=2)
steps = [('feature_selection', select), ('svr', clf)]
pipeline = Pipeline(steps)
grid = GridSearchCV(pipeline, param_grid={"svr__C":[10,10,100],"svr__gamma": np.logspace(-2, 2)})
grid.fit(X_train, y_train)

This seems to work fine but when I try to access the coefficient of the best fitting model

grid.best_estimator_.coef_

I get an error message: AttributeError: 'Pipeline' object has no attribute 'coef_'.

I also tried to access the individual steps of the pipeline:

pipeline.named_steps['svr']

but could not find the coefficients there.

1

1 Answers

2
votes

Just happened to come across the same problem and this post had the answer: grid.best_estimator_ contains an instance of the pipeline, which consists of steps. The last step should always be the estimator, so you should always find the coefficients at:

grid.best_estimator_.steps[-1][1].coef_