Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 model, when I try to use

model.get_params() 

I get this

TypeError: get_params() missing 1 required positional argument: 'self'
like image 330
thebeancounter Avatar asked Feb 07 '19 11:02

thebeancounter


People also ask

How do you find the parameters of a model Sklearn?

This should do it: estimator. get_params() where estimator is the name of your model. 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.

What are the hyperparameters of a model?

A model hyperparameter is a configuration that is external to the model and whose value cannot be estimated from data. They are often used in processes to help estimate model parameters. They are often specified by the practitioner. They can often be set using heuristics.


1 Answers

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.

like image 143
sudo Avatar answered Oct 11 '22 12:10

sudo