Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TargetEncoder can't convert to string (AttributeError) #93

Open
momijiame opened this issue Jan 5, 2023 · 1 comment
Open

TargetEncoder can't convert to string (AttributeError) #93

momijiame opened this issue Jan 5, 2023 · 1 comment

Comments

@momijiame
Copy link
Contributor

Abstract

AttributeError occurs when TargetEncoder object passed to str() function.
This behavior has the potential to become an issue on debugging etc.

How to reproduce

The steps to reproduce are the following.

>>> from nyaggle.feature.category_encoder.target_encoder import TargetEncoder
>>> encoder = TargetEncoder()
>>> str(encoder)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/amedama/.virtualenvs/py310/lib/python3.10/site-packages/sklearn/base.py", line 279, in __repr__
    repr_ = pp.pformat(self)
  File "/usr/local/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10/lib/python3.10/pprint.py", line 157, in pformat
    self._format(object, sio, 0, 0, {}, 0)
  File "/usr/local/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10/lib/python3.10/pprint.py", line 174, in _format
    rep = self._repr(object, context, level)
  File "/usr/local/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10/lib/python3.10/pprint.py", line 454, in _repr
    repr, readable, recursive = self.format(object, context.copy(),
  File "/Users/amedama/.virtualenvs/py310/lib/python3.10/site-packages/sklearn/utils/_pprint.py", line 189, in format
    return _safe_repr(
  File "/Users/amedama/.virtualenvs/py310/lib/python3.10/site-packages/sklearn/utils/_pprint.py", line 440, in _safe_repr
    params = _changed_params(object)
  File "/Users/amedama/.virtualenvs/py310/lib/python3.10/site-packages/sklearn/utils/_pprint.py", line 93, in _changed_params
    params = estimator.get_params(deep=False)
  File "/Users/amedama/.virtualenvs/py310/lib/python3.10/site-packages/sklearn/base.py", line 211, in get_params
    value = getattr(self, key)
AttributeError: 'TargetEncoder' object has no attribute 'cols'

TargetEncoder takes 'cols' parameter but doesn't save to the attribute.
(That transfers to category_encoders.TargetEncoder internally)
But scikit-learn BaseEstimator#get_params() expects that all __init__() parameters will be saved.
So accessing 'cols' raises AttributeError.

Environment

$ python -V                                   
Python 3.10.9
$ pip list | egrep -i "(nyaggle|scikit-learn)"
nyaggle            0.1.5
scikit-learn       1.2.0
@nyanp
Copy link
Owner

nyanp commented Jan 19, 2023

@momijiame
Thanks for your report! TargetEncoder in nyaggle does not comply with the interface required by scikit-learn, so I think this should be fixed.

In addition, every keyword argument accepted by __init__ should correspond to an attribute on the instance. Scikit-learn relies on this to find the relevant attributes to set on an estimator when doing model selection.
https://scikit-learn.org/stable/developers/develop.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants