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!
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 clone
d 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.
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