10000 FIX missing_indices were calculated twice in OrdinalEncoder by xuefeng-xu · Pull Request #27017 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

FIX missing_indices were calculated twice in OrdinalEncoder #27017

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

Merged
merged 4 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whats_new/v1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ Changelog
:mod:`sklearn.preprocessing`
............................

- |Efficiency| :class:`preprocessing.OrdinalEncoder` avoids calculating
missing indices twice to improve efficiency.
:pr:`27017` by `Xuefeng Xu <xuefeng-xu>`.

- |Fix| :class:`preprocessing.OneHotEncoder` shows a more informative error message
when `sparse_output=True` and the output is configured to be pandas.
:pr:`26931` by `Thomas Fan`_.
Expand Down
10 changes: 3 additions & 7 deletions sklearn/preprocessing/_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -1508,15 +1508,11 @@ def fit(self, X, y=None):
if infrequent is not None:
cardinalities[feature_idx] -= len(infrequent)

# stores the missing indices per category
self._missing_indices = {}
# missing values are not considered part of the cardinality
# when considering unknown categories or encoded_missing_value
for cat_idx, categories_for_idx in enumerate(self.categories_):
for i, cat in enumerate(categories_for_idx):
for cat in categories_for_idx:
if is_scalar_nan(cat):
self._missing_indices[cat_idx] = i

# missing values are not considered part of the cardinality
# when considering unknown categories or encoded_missing_value
cardinalities[cat_idx] -= 1
continue

Expand Down
0