Python scikit-learn(使用grid_search.GridSearchCV)

0 投票
1 回答
4072 浏览
提问于 2025-04-18 08:53

我正在使用网格搜索来调整机器学习模型的参数。

我输入了以下代码(这个代码是从sklearn的文档页面修改过来的:http://scikit-learn.org/stable/modules/generated/sklearn.grid_search.GridSearchCV.html

from sklearn import svm, grid_search, datasets, cross_validation

# getting data
iris = datasets.load_iris()

# grid of parameters
parameters = {'kernel':('linear', 'poly'), 'C':[1, 10]}

# predictive model (support vector machine)
svr = svm.SVC()

# cross validation procedure
mycv = cross_validation.StratifiedKFold(iris.target, n_folds = 2)

# grid search engine
clf = grid_search.GridSearchCV(svr, parameters, mycv)

# fitting engine
clf.fit(iris.data, iris.target)

但是,当我查看 clf.estimator 时,得到的结果是:

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
  kernel='rbf', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False)

我怎么会得到一个'rbf'核呢?我并没有在我的参数选项中指定它。

这是怎么回事呢?

谢谢!

附言:我使用的是'sklearn 0.15-git'版本。

补充说明:我注意到 clf.best_estimator_ 给出了正确的输出。那么 clf.estimator 是在做什么呢?

1 个回答

1

clf.estimator 其实就是你传给 GridSearchCV 对象的第一个参数的一个副本。那些没有进行网格搜索的参数都是由这个估计器来决定的。因为你没有特别设置 svr 这个 SVC 对象的任何参数,所以它使用了所有的默认值。因此,clf.estimator 只是 svr 的一个副本,所以当你打印 clf.estimator 的值时,它会显示一个带有默认参数的 SVC 对象。如果你写了,比如说:

svr = svm.SVC(C=4.3)

那么 clf.estimator 的值就会是:

SVC(C=4.3, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
  kernel='rbf', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False)

其实访问 clf.estimator 对用户来说并没有什么实际的意义,而且它本来也不是一个公开的属性(因为它的名字后面没有 "_")。

撰写回答