Skip to content
Advertisement

do i need to use exactly same attribute names to properties in custom preprocessing class which inherit scikit learn BaseEstimator?

when writing custom classes inherit from BaseEstimator of the sklearn throwing AttributeError: object has no attribute . but that attribute is present and has values.

    class BaseNull(BaseEstimator, TransformerMixin):
        def __init__(self,
                     variables: Union[str, list[str]],
                     row_wise: bool = False,
                     na_kwds: Union[str, list, tuple] = None):
            self.na_kwds = na_kwds
            self.row = row_wise
            self.null_index = None
            self.null_columns = None
            self.row_count = None
            self.column_count = None


`null = BaseNull(temp_data.columns).fit(temp_data)`. it is working fine until 

print(null) execute or null. then it throws the above attribute error. traceback shows that this error happens in getattr() in sklearn base.

c:program filespython39libsite-packagessklearnutils_pprint.py in _changed_params(estimator)
     91     estimator with non-default values."""
     92 
---> 93     params = estimator.get_params(deep=False)
     94     init_func = getattr(estimator.__init__, "deprecated_original", estimator.__init__)
     95     init_params = inspect.signature(init_func).parameters

c:program filespython39libsite-packagessklearnbase.py in get_params(self, deep)
    209         out = dict()
    210         for key in self._get_param_names():
--> 211             value = getattr(self, key)
    212             if deep and hasattr(value, "get_params") and not isinstance(value, type):
    213                 deep_items = value.get_params().items()

I found that this is caused by attributes that assign to different property names ex: self.row = row_wise. what’s happening here? and can I use different property names to assign attribute values?

Advertisement

Answer

do i need to use exactly same attribute names to properties in custom preprocessing class which inherit scikit learn BaseEstimator?

Yes. See this part of the docs:

All scikit-learn estimators have get_params and set_params functions. The get_params function takes no arguments and returns a dict of the __init__ parameters of the estimator, together with their values.

(Source.)

In other words, if you have a parameter to __init__ called variables, then it expects self.variables to be a valid variable.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement