在 scikit-learn 中使用 GridSearchCV 会导致 CalibratedClassifierCV 出错。 GridSearchCV 对象没有属性 'best_params_

Using GridSearchCV gives an error with CalibratedClassifierCV in scikit-learn. GridSearchCV object has no attribute 'best_params_

我将 CalibratedClassifierCV 与 RandomForest 结合使用,并使用 GridSearch 来确定最佳参数。但是,当我使用 GridSearchCV 读回最佳参数时,它说 GridSearchCV 对象没有属性 'best_params_'

from sklearn.calibration import CalibratedClassifierCV
from classifiers import SVMClassification 
from sklearn.model_selection import  GridSearchCV
from imblearn.pipeline import Pipeline as imbpipeline
        
pipeline=imbpipeline([ ('oversample', Sampling(random_state=444)),('rf', rf())])
paramgrid=[ {'rf__max_depth': [8,10], 'rf__n_estimators':[3,5]}]           
grid_search_rf = GridSearchCV(pipeline, param_grid=paramgrid,cv=3)
rf_calibrated=CalibratedClassifierCV(grid_search_rf,cv=5, method="sigmoid")
rf_calibrated.fit(x_labelled,y_labelled)
print(rf_calibrated.base_estimator.best_params_)

输出

AttributeError: 'GridSearchCV' object has no attribute 'best_params_'

我假设您认为 CalibratedClassifierCV 将适合提供的估计器,然后以某种方式增强(校准)它的输出概率。

部分正确。

发生的事情是:

  • CalibratedClassifierCV 将克隆提供的估计器,然后将数据拟合到克隆中。所以你这样做

    rf_calibrated.base_estimator`
    

    只会 return 一个没有 best_params_ 属性的未拟合估计量。 best_params_需要适配后才能使用

从 CalibratedClassifierCV 检查 best_params_ 没有任何意义,因为它将数据分成 5 部分(正如您所做的那样 cv=5),并且每个折叠都在单独的克隆上进行训练,因此您可能有多个 best_params,具体取决于数据。