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 files\python39\lib\site-packages\sklearn\utils\_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 files\python39\lib\site-packages\sklearn\base.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?
CodePudding user response:
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
andset_params
functions. Theget_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.