【发布时间】:2017-03-26 21:55:29
【问题描述】:
当模型嵌入管道和网格搜索时,我无法在 scikit learn 中访问支持向量回归模型 (SVR) 的系数。 考虑以下示例:
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)
这似乎工作正常,但是当我尝试访问最佳拟合模型的系数时
grid.best_estimator_.coef_
我收到一条错误消息:AttributeError: 'Pipeline' object has no attribute 'coef_'。
我还尝试访问管道的各个步骤:
pipeline.named_steps['svr']
但在那里找不到系数。
【问题讨论】:
标签: regression svm python scikit-learn