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

如何查看Scikit-learn中RandomForestRegressor等模型的参数

查看Scikit-learn回归模型的参数方法

我正在对比多种集成模型,包括:

from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import Lasso
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.ensemble import GradientBoostingRegressor
from xgboost import XGBRegressor

使用XGBRegressor时,训练后直接打印模型就能看到完整参数:

xgb_model = XGBRegressor()
xgb_model.fit(X_train, y_train)
xgb_model

输出:

XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
             colsample_bynode=1, colsample_bytree=1, enable_categorical=False,
             gamma=0, gpu_id=-1, importance_type=None,
             interaction_constraints='', learning_rate=0.300000012,
             max_delta_step=0, max_depth=6, min_child_weight=1, missing=nan,
             monotone_constraints='()', n_estimators=100, n_jobs=52,
             num_parallel_tree=1, predictor='auto', random_state=0, reg_alpha=0,
             reg_lambda=1, scale_pos_weight=1, subsample=1, tree_method='exact',
             validate_parameters=1, verbosity=None)

但Scikit-learn的部分回归器(比如RandomForestRegressor、AdaBoostRegressor、GradientBoostingRegressor)训练后打印模型只显示空括号:

RF_model = RandomForestRegressor()
RF_model.fit(X_train, y_train)
RF_model

输出:

RandomForestRegressor()

解决方法

1. 使用get_params()方法

这是Scikit-learn模型通用的参数查询方式,会返回包含所有参数(包括默认值)的字典:

# 查看RandomForest的所有参数
print(RF_model.get_params())

输出示例(部分):

{'bootstrap': True, 'ccp_alpha': 0.0, 'criterion': 'squared_error', 'max_depth': None, 'max_features': 'sqrt', ...}

如果只想查看单个参数,比如max_depth:

print(RF_model.get_params()['max_depth'])

2. 直接访问模型实例属性

部分核心参数会作为模型的实例属性存在,可以直接调用:

# 查看RandomForest的决策树数量
print(RF_model.n_estimators)
# 查看GradientBoosting的学习率
print(gbdt_model.learning_rate)

注:这种方式仅适用于部分常用参数,不如get_params()覆盖全面。

3. 理解Scikit-learn默认打印逻辑

Scikit-learn模型默认只在打印时显示非默认参数,如果初始化时没有修改参数,就只会显示空括号。如果想让打印时显示所有参数,也可以在初始化模型时显式指定所有参数,但这种方式繁琐,不如直接用get_params()高效。


内容的提问来源于stack exchange,提问作者Elaine Yang

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 05:35:23