Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a dict to scikit learn estimator

I am trying to pass model parameters as a dict to a Scikit-learn estimator and am having no luck. It just seems to nest my dict into one of the parameters. For instance:

params = {
 'copy_X': True, 
 'fit_intercept': False, 
 'normalize': True
}

lr = LinearRegression(params)

Gives me:

LinearRegression(copy_X=True,
         fit_intercept={'copy_X': True, 'fit_intercept': False,'normalize': True},
     normalize=False)

Additionally, I created a function to iterate over the dict and can create a string like:

'copy_X=True, fit_intercept=True, normalize=False'

This was equally as unsuccessful. Anyone have any advice here? The only restriction I have is the data will be coming to me as a dict (well actually a json object being loaded with json.uploads).

Thanks.

like image 422
GMarsh Avatar asked Oct 13 '15 19:10

GMarsh


People also ask

What is estimator in scikit-learn?

Estimators objects Fitting data: the main API implemented by scikit-learn is that of the estimator . An estimator is any object that learns from data; it may be a classification, regression or clustering algorithm or a transformer that extracts/filters useful features from raw data.

What is fit () in Sklearn?

The fit() method takes the training data as arguments, which can be one array in the case of unsupervised learning, or two arrays in the case of supervised learning. Note that the model is fitted using X and y , but the object holds no reference to X and y .

How does fit () work in Python?

fit() is implemented by every estimator and it accepts an input for the sample data ( X ) and for supervised models it also accepts an argument for labels (i.e. target data y ). Optionally, it can also accept additional sample properties such as weights etc. fit methods are usually responsible for numerous operations.


2 Answers

The best solution to initialise your estimator with the right parameters would be to unpack your dictionary:

lr = LinearRegression(**params)

If for some reason you need to set some parameters afterwards, you could use:

lr.set_params(**params)

This has an advantage over using setattr in that it allows Scikit learn to perform some validation checks on the parameters.

like image 126
ldirer Avatar answered Oct 27 '22 05:10

ldirer


I got it. Used setattr like this.

for k,v in params.items():
   setattr(lr,k,v)
like image 6
GMarsh Avatar answered Oct 27 '22 04:10

GMarsh