Skip to main content
 首页 » 编程设计

python之使用 GridsearchCV 提取管道中最佳模型的 MLPRegressor 属性(n_iter_)

2024年11月01日10java哥

我制作了一个带有管道的 GridsearchCV,我想提取管道组件 (MLPRegressor) 的一个属性 (n_iter_) 以获得最佳模型。

我正在使用 Python 3.0。

管道的创建

pipeline_steps = [('scaler', StandardScaler()), ('MLPR', MLPRegressor(solver='lbfgs', early_stopping=True, validation_fraction=0.1, max_iter=10000))] 
 
MLPR_parameters = {'MLPR__hidden_layer_sizes':[(50,), (100,), (50,50)], 'MLPR__alpha':[0.001, 10, 1000]} 
 
MLPR_pipeline = Pipeline(pipeline_steps) 
 
gridCV_MLPR = GridSearchCV(MLPR_pipeline, MLPR_parameters, cv=kfold) 
gridCV_MLPR.fit(X_train, y_train) 

当我想用 gridCV_GBR.best_params_ 提取最佳模型时,我只有 GridsearchCV 的结果:

{'MLPR__alpha': 0.001, 'MLPR__hidden_layer_sizes': (50,)} 

但我想知道 gridCV_MLPR 的最佳模型使用的 MLPRegressor 的迭代次数。

如何通过 GridsearhCV 管道使用为 MLPRegressor() 设计的 n_iter_ 属性?

请您参考如下方法:

谢谢你的帮助,

我找到了解决方案:

gridCV_MLPR.best_estimator_.named_steps['MLPR'].n_iter_ 

由于 gridCV_MLPR.best_estimator_ 是一个管道,我们需要使用 .named_steps['MLPR'] 选择 MLPRegressor 参数。

非常感谢您非常、非常快速的回答...