Python Sklearn Get List Of Available Hyper Parameters For Model
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 mo
Solution 1:
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)
EDIT:
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
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
.
Solution 2:
As of May 2021: (Building on sudo's answer)
# To get the model hyperparameters before you instantiate the classimport inspect
import sklearn
models = [sklearn.linear_model.LinearRegression]
for m in models:
hyperparams = inspect.signature(m.__init__)
print(hyperparams)
#>>> (self, *, fit_intercept=True, normalize=False, copy_X=True, n_jobs=None)
Using inspect.getargspec(m.__init__).args
, as suggested by sudo in the accepted answer, generated the following warning:
DeprecationWarning: inspect.getargspec() is deprecated since Python 3.0,
use inspect.signature() or inspect.getfullargspec()
Post a Comment for "Python Sklearn Get List Of Available Hyper Parameters For Model"