You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在for循环中向RandomForestRegressor传入单变量超参数

循环传入超参训练RandomForestRegressor报错问题解决

问题描述

尝试用for循环遍历字典列表形式的超参数集合,给RandomForestRegressor传入不同超参训练模型,但拟合时持续报错,错误提示n_estimators参数应是[1, +∞)范围内的整数,却收到了整个超参数字典。

错误信息

Traceback (most recent call last):
File "c:\Projects\Python\DATA260\data_260_python\src\DATA_280A_Course\src\week6_project_work\jess_obesity_dataset_optimized_RFR.py", line 283, in <module>      
  main()
File "c:\Projects\Python\DATA260\data_260_python\src\DATA_280A_Course\src\week6_project_work\jess_obesity_dataset_optimized_RFR.py", line 210, in main
  model_regressor.fit(X_train, y_train)
File "C:\Users\Jess\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\base.py", line 1144, in wrapper
  estimator._validate_params()
File "C:\Users\Jess\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\base.py", line 637, in _validate_params
  validate_parameter_constraints(
File "C:\Users\Jess\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\utils\_param_validation.py", line 95, in validate_parameter_constraints   
  raise InvalidParameterError(
sklearn.utils._param_validation.InvalidParameterError: The 'n_estimators' parameter of RandomForestRegressor must be an int in the range [1, inf). Got {'n_estimators': 460, 'bootstrap': False, 'criterion': 'poisson', 'max_depth': 60, 'max_features': 2, 'min_samples_leaf': 1, 'min_samples_split': 2} instead.

错误原因

创建RandomForestRegressor实例时,直接将超参字典作为位置参数传入,而RandomForestRegressor的构造函数第一个位置参数就是n_estimators,导致整个字典被赋值给了n_estimators参数,触发参数类型校验错误。

解决方法

使用Python的**字典解包操作符,将超参字典中的键值对展开为关键字参数传入模型构造函数。修改模型初始化代码:

原代码:

model_regressor = RandomForestRegressor(hparams)

修改为:

model_regressor = RandomForestRegressor(**hparams)

**操作符会把字典中的每个键值对转换为key=value形式的关键字参数,这样每个超参数都会被正确赋值给模型对应的参数。

修改后的完整代码

hyperparams = [{
                'n_estimators':460,
                'bootstrap':False,
                'criterion':'poisson',
                'max_depth':60,
                'max_features':2,
                'min_samples_leaf':1,
                'min_samples_split':2
            },
            {
                'n_estimators':60,
                'bootstrap':False,
                'criterion':'friedman_mse',
                'max_depth':90,
                'max_features':3,
                'min_samples_leaf':1,
                'min_samples_split':2
            }]
for hparams in hyperparams:
    # 用**解包超参字典
    model_regressor = RandomForestRegressor(**hparams)
    print(model_regressor.get_params())

    total_r2_score_value = 0
    total_mean_squared_error_array = 0

    total_explained_variance_score_value = 0
    total_max_error_value = 0
    total_mean_absolute_error_value = 0
    total_mean_absolute_percent_value = 0
    total_median_absolute_error_value = 0
    total_mean_tweedie_deviance_value = 0
    total_mean_pinball_loss_value = 0
    total_d2_pinball_score_value = 0
    total_d2_absolute_error_score_value = 0
    
    total_tests = 10
    for index in range(1, total_tests+1):
        # model fitting
        model_regressor.fit(X_train, y_train)
        # 后续可添加评估逻辑,计算各项指标并累加

说明

该需求完全可行,通过字典解包的方式就能实现批量传入不同超参数训练模型的目的。

内容的提问来源于stack exchange,提问作者Jess Stuart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 06:11:07