在Python的sklearn中,可以通过以下步骤获取GridSearchCV中的所选要素:
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_iris
from sklearn.svm import SVC
iris = load_iris()
X = iris.data
y = iris.target
model = SVC()
param_grid = {'C': [0.1, 1, 10], 'kernel': ['linear', 'rbf']}
grid_search = GridSearchCV(estimator=model, param_grid=param_grid)
grid_search.fit(X, y)
best_params = grid_search.best_params_
best_estimator = grid_search.best_estimator_
best_score = grid_search.best_score_
在上述代码中,我们首先导入了所需的库和模块。然后,加载了一个示例数据集(这里使用的是鸢尾花数据集)。接下来,我们定义了一个支持向量机模型(SVC)和一个参数网格,其中包含了不同的C值和核函数类型。然后,我们创建了一个GridSearchCV对象,并将模型和参数网格传递给它。接着,我们使用拟合方法对数据集进行训练和调优。最后,我们通过访问GridSearchCV对象的属性来获取所选要素,包括最佳参数(best_params)、最佳估计器(best_estimator)和最佳得分(best_score)。
这样,我们就可以在Python的sklearn中获取GridSearchCV中的所选要素了。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云