python sklearn 获取模型的可用超参数列表 [英] python sklearn get list of available hyper parameters for model
问题描述
我正在将 python 与 sklearn 一起使用,并想获取模型的可用超参数列表,如何做到这一点?谢谢
I am using python with sklearn, and would like to get a list of available hyper parameters for a model, how can this be done? Thanks
这需要在我初始化模型之前发生,当我尝试使用
This needs to happen before I initialize the model, when I try to use
model.get_params()
我明白了
类型错误:get_params() 缺少 1 个必需的位置参数:'self'
TypeError: get_params() missing 1 required positional argument: 'self'
推荐答案
应该这样做:estimator.get_params()
其中 estimator
是您的模型的名称.
This should do it: estimator.get_params()
where estimator
is the name of your model.
要在模型上使用它,您可以执行以下操作:
To use it on a model you can do the following:
reg = RandomForestRegressor()
params = reg.get_params()
# do something...
reg.set_params(params)
reg.fit(X, y)
在实例化类之前获取模型超参数:
To get the model hyperparameters before you instantiate the class:
import inspect
import sklearn
models = [sklearn.ensemble.RandomForestRegressor, sklearn.linear_model.LinearRegression]
for m in models:
hyperparams = inspect.getargspec(m.__init__).args
print(hyperparams) # Do something with them here
模型超参数被传递到 sklearn
中的构造函数,因此我们可以使用 inspect
模型来查看哪些构造函数参数可用,从而查看超参数.您可能需要过滤掉一些不特定于模型的参数,例如 self
和 n_jobs
.
The model hyperparameters are passed in to the constructor in sklearn
so we can use the inspect
model to see what constructor parameters are available, and thus the hyperparameters. You may need to filter out some arguments that aren't specific to the model such as self
and n_jobs
.
这篇关于python sklearn 获取模型的可用超参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!