Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should we use BaseEstimator and Transformermixmin from sklearn.base in python to create/ custom class?

I am a beginner at learning sklearn from Python. I know we could create any class by ourselves with something like that:

class name():
   def __init__(self):
       pass
   def fit(self, x):
          return  something

But I cannot understand why when we try to custom the transformer or the class in sklearn we need to use BaseEstimator and Transformermixmin as below:

from sklearn.base import BaseEstimator, Transformermixmin

class name(BaseEstimator, Transformermixmin):
    def __init(self):
       pass
    def fit(self, x):
       return self

what is the difference between the empty class and the other one with BaseEstimator, Transformermixmin? How can BaseEstimator and Transformermixmin work and what does it used for? Can someone answer the above questions and give me more examples or clear explanations about them? Thank you so much!

like image 350
Bertie Avatar asked Oct 18 '25 12:10

Bertie


1 Answers

You do not need to inherit from either class, but they can both be helpful.

BaseEstimator implements get_params and set_params for you, assuming you adhere to the requirements for the __init__ method. These two methods allow your custom transformer to be cloned and hyperparameter-tuned using GridSearchCV or RandomizedSearchCV. It also provides a printing methods to print either text or html representation of your estimator. It also contains some common input validation methods, and more recently, feature name handling.

TransformerMixin implements fit_transform as fit followed by transform. It's convenient, although not nearly so much as BaseEstimator.

See also the box titled "BaseEstimator and mixins" in the sklearn developer guide.

like image 57
Ben Reiniger Avatar answered Oct 21 '25 00:10

Ben Reiniger