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.
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.
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 .
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.
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.
I got it. Used setattr like this.
for k,v in params.items():
setattr(lr,k,v)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With