感谢您的阅读!
我正在尝试使用 for 循环将不同的超参数发送到 RandomForestRegressor。 我使用下面的代码创建超参数(列表?数组?)来控制循环。当我尝试拟合模型时,我不断收到代码后面列出的错误。
我想做的事情可能吗?如果可能的话,我会怎么做?
谢谢!
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())
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)```
ERROR:
```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.```
将超参数字典传递给构造函数时,您应该“解压”它:
model_regressor = RandomForestRegressor(**hparams)
否则,根据文档,它会尝试将 n_estimators 设置为您作为第一个参数传递的任何内容。