[scikit-learn] Delegating "get_params" and "set_params" to a wrapped estimator when parameter is not defined.

Andreas Mueller t3kcit at gmail.com
Fri Apr 13 12:50:04 EDT 2018

Please stay on the mailing list :)

I'm not sure if ValueError is the right error that would be raised (I 
think it's not).
And it's hard to say if this will break something in some edge cases. I 
probably rather explicitly encode the parameters of FancyEstimator 
instead of the try,
or get them using super.get_params. You also should rewrite get_params.

In principle something like that should work, but I wouldn't go as far 
as saying it's
"safe" and you should test it extensively.

On 04/13/2018 12:41 PM, Javier López wrote:
> Is something like this safe, or might I be breaking some important 
> functionality?
> ```
>     def set_params(self, **params):
>         try:
>             super(FancyEstimator, self).set_params(**params)
>         except ValueError:
>             self.wrapped_estimator_.set_params(**params)
> ```
> On Fri, Apr 13, 2018 at 5:05 PM Andreas Mueller <t3kcit at gmail.com 
> <mailto:t3kcit at gmail.com>> wrote:
>     You just need to implement get_params and set_params yourself to
>     delegate in this way, right?
>     On 04/13/2018 11:51 AM, Javier López wrote:
>>     I have a class
>>     `FancyEstimator(BaseEstimator, MetaEstimatorMixin): ...` that wraps
>>     around an arbitrary sklearn estimator to add some functionality I
>>     am interested about.
>>     This class contains an attribute `self.estimator` that contains
>>     the wrapped estimator.
>>     Delegation of the main methods, such as `fit`, `transform` works
>>     just fine, but I am
>>     having some issues with `get_params` and `set_params`.
>>     The main idea is, I would like to use my wrapped class as a
>>     drop-in replacement for
>>     the original estimator, but this raises some issues with some
>>     functions
>>     that try using the `get_params` and `set_params` straight in my
>>     class, as the original
>>     parameters now have prefixed names (for instance
>>     `estimator__verbose` instead of `verbose`)
>>     I would like to delegate calls of set_params and get_params in a
>>     smart way so that if a
>>     parameter is unknown for my wrapper class, then it automatically
>>     goes looking for it in
>>     the wrapped estimator.
>>      I am not concerned about my class parameter names as there are
>>     only a couple of very
>>     specific names on it, so it is safe to assume that any unknown
>>     parameter name should
>>     refer to the base estimator. Is there an easy way of doing that?
>>     Cheers,
>>     J
>>     _______________________________________________
>>     scikit-learn mailing list
>>     scikit-learn at python.org <mailto:scikit-learn at python.org>
>>     https://mail.python.org/mailman/listinfo/scikit-learn

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20180413/28e73e24/attachment.html>

More information about the scikit-learn mailing list