GridSearchCV.best_estimator_
时间: 2023-10-09 14:15:47 浏览: 38
GridSearchCV.best_estimator_是GridSearchCV类中的一个属性,它返回在网格搜索期间得出的最佳模型。在scikit-learn中,网格搜索是一种超参数优化技术,它通过遍历指定的参数组合来确定最佳的模型超参数配置。网格搜索使用交叉验证来评估每种参数组合的性能,并选择导致最佳性能的参数组合。在GridSearchCV完成后,可以使用best_estimator_属性来访问最佳模型对象,然后将该模型用于进一步的预测或分析任务。
相关问题
GridSearchCV没有return_estimator
是的,GridSearchCV没有`return_estimator`属性。它只返回最佳的参数组合和对应的评估指标。如果你想获取最佳的估算器(estimator),你可以使用`best_estimator_`属性来获取。例如:
```
from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV
iris = datasets.load_iris()
parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
svc = svm.SVC()
clf = GridSearchCV(svc, parameters)
clf.fit(iris.data, iris.target)
print(clf.best_params_)
print(clf.best_score_)
print(clf.best_estimator_)
```
在上面的例子中,`clf.best_estimator_`将返回使用最佳参数组合所拟合的最佳估算器对象。
把这段代码的PCA换成LDA:LR_grid = LogisticRegression(max_iter=1000, random_state=42) LR_grid_search = GridSearchCV(LR_grid, param_grid=param_grid, cv=cvx ,scoring=scoring,n_jobs=10,verbose=0) LR_grid_search.fit(pca_X_train, train_y) estimators = [ ('lr', LR_grid_search.best_estimator_), ('svc', svc_grid_search.best_estimator_), ] clf = StackingClassifier(estimators=estimators, final_estimator=LinearSVC(C=5, random_state=42),n_jobs=10,verbose=1) clf.fit(pca_X_train, train_y) estimators = [ ('lr', LR_grid_search.best_estimator_), ('svc', svc_grid_search.best_estimator_), ] param_grid = {'final_estimator':[LogisticRegression(C=0.00001),LogisticRegression(C=0.0001), LogisticRegression(C=0.001),LogisticRegression(C=0.01), LogisticRegression(C=0.1),LogisticRegression(C=1), LogisticRegression(C=10),LogisticRegression(C=100), LogisticRegression(C=1000)]} Stacking_grid =StackingClassifier(estimators=estimators,) Stacking_grid_search = GridSearchCV(Stacking_grid, param_grid=param_grid, cv=cvx, scoring=scoring,n_jobs=10,verbose=0) Stacking_grid_search.fit(pca_X_train, train_y) Stacking_grid_search.best_estimator_ train_pre_y = cross_val_predict(Stacking_grid_search.best_estimator_, pca_X_train,train_y, cv=cvx) train_res1=get_measures_gridloo(train_y,train_pre_y) test_pre_y = Stacking_grid_search.predict(pca_X_test) test_res1=get_measures_gridloo(test_y,test_pre_y) best_pca_train_aucs.append(train_res1.loc[:,"AUC"]) best_pca_test_aucs.append(test_res1.loc[:,"AUC"]) best_pca_train_scores.append(train_res1) best_pca_test_scores.append(test_res1) train_aucs.append(np.max(best_pca_train_aucs)) test_aucs.append(best_pca_test_aucs[np.argmax(best_pca_train_aucs)].item()) train_scores.append(best_pca_train_scores[np.argmax(best_pca_train_aucs)]) test_scores.append(best_pca_test_scores[np.argmax(best_pca_train_aucs)]) pca_comp.append(n_components[np.argmax(best_pca_train_aucs)]) print("n_components:") print(n_components[np.argmax(best_pca_train_aucs)])
如果要将代码中的PCA替换为LDA,可以按照以下步骤进行修改:
```python
LR_grid = LogisticRegression(max_iter=1000, random_state=42)
LR_grid_search = GridSearchCV(LR_grid, param_grid=param_grid, cv=cvx ,scoring=scoring,n_jobs=10,verbose=0)
LR_grid_search.fit(lda_X_train, train_y)
estimators = [
('lr', LR_grid_search.best_estimator_),
('svc', svc_grid_search.best_estimator_),
]
clf = StackingClassifier(estimators=estimators, final_estimator=LinearSVC(C=5, random_state=42),n_jobs=10,verbose=1)
clf.fit(lda_X_train, train_y)
estimators = [
('lr', LR_grid_search.best_estimator_),
('svc', svc_grid_search.best_estimator_),
]
param_grid = {'final_estimator': [LogisticRegression(C=0.00001),LogisticRegression(C=0.0001),
LogisticRegression(C=0.001),LogisticRegression(C=0.01),
LogisticRegression(C=0.1),LogisticRegression(C=1),
LogisticRegression(C=10),LogisticRegression(C=100),
LogisticRegression(C=1000)]}
Stacking_grid = StackingClassifier(estimators=estimators,)
Stacking_grid_search = GridSearchCV(Stacking_grid, param_grid=param_grid, cv=cvx,
scoring=scoring, n_jobs=10, verbose=0)
Stacking_grid_search.fit(lda_X_train, train_y)
Stacking_grid_search.best_estimator_
train_pre_y = cross_val_predict(Stacking_grid_search.best_estimator_, lda_X_train, train_y, cv=cvx)
train_res1 = get_measures_gridloo(train_y, train_pre_y)
test_pre_y = Stacking_grid_search.predict(lda_X_test)
test_res1 = get_measures_gridloo(test_y, test_pre_y)
best_lda_train_aucs.append(train_res1.loc[:,"AUC"])
best_lda_test_aucs.append(test_res1.loc[:,"AUC"])
best_lda_train_scores.append(train_res1)
best_lda_test_scores.append(test_res1)
train_aucs.append(np.max(best_lda_train_aucs))
test_aucs.append(best_lda_test_aucs[np.argmax(best_lda_train_aucs)].item())
train_scores.append(best_lda_train_scores[np.argmax(best_lda_train_aucs)])
test_scores.append(best_lda_test_scores[np.argmax(best_lda_train_aucs)])
lda_comp.append(n_components[np.argmax(best_lda_train_aucs)])
print("n_components:")
print(n_components[np.argmax(best_lda_train_aucs)])
```
在这个修改后的代码中,将`pca_X_train`和`pca_X_test`替换为`lda_X_train`和`lda_X_test`,并相应地修改变量和参数的名称。这样就可以使用LDA进行特征降维和模型训练了。