Skip to content

fix(encoding): number mixed-type categories in the int encoder - #178

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/177-mixed-type-categories
Open

ChrisW09 wants to merge 1 commit into
mainfrom
fix/177-mixed-type-categories

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #177.

The default "int" categorical encoding (ContinuousOrdinalTransformer) raised TypeError: '<' not supported between instances of 'str' and 'int' on an object column mixing int and str categories, so Preprocessor().fit failed on such data.

Root cause

ContinuousOrdinalTransformer.fit (pretab/encoding/categorical/ordinal.py) numbered the categories in the order of np.unique(non_missing), which sorts them, and Python cannot order an int and a str.

Fix

New _sorted_categories helper:

  • it returns np.unique's order when the categories can be compared, so every single-type column is numbered exactly as before;
  • otherwise it groups the distinct categories by type name (in sorted type-name order) and sorts each group naturally, falling back to repr within a group.

fit uses it in place of np.unique. Missing values are still excluded and code 0 stays reserved for missing or unseen values.

Behaviour changes

  • Mixed-type categorical columns are encoded instead of raising. Ints come before strings, because "int" < "str".
  • Single-type columns get the same codes as before, and existing specs load unchanged.
  • In scikit-learn 1.6, the minimum supported version, the default SimpleImputer(strategy="most_frequent") before the encoder can still fail on such a column when the most frequent values tie across types. That is an upstream limitation; later versions break the tie. The integration test therefore uses a column with a single most frequent category.

Documentation

  • ContinuousOrdinalTransformer docstring: the order for mixed types.
  • docs/representations/categorical_encoding.md (integer encoding): codes follow the sorted order, and how mixed-type columns are ordered.

Tests

In tests/encoding/categorical/test_continuous_ordinal_transformer.py:

  • test_continuous_ordinal_numbers_mixed_type_categories: mapping and transform with mixed types, missing and unseen values. Fails on main, passes here.
  • test_continuous_ordinal_keeps_the_sorted_order_of_one_type: guard that passes on both.
  • test_preprocessor_encodes_a_mixed_type_categorical_column: default-encoding fit plus a JSON spec round trip. Fails on main, passes here, including under the minimum dependencies.

Reproduction

The script from #177 on this branch:

ContinuousOrdinalTransformer    -> [{101: 1, 205: 2, 'A7': 3, 'B2': 4, None: 0}]
Preprocessor() (default 'int')  -> {'price': 7, 'code': 1}

Checks

  • just test: 4679 passed, 59 skipped, 7 xfailed; coverage 95.72% (gate 90%).
  • Minimum dependencies (CI job: Python 3.11, numpy 1.24.4, pandas 2.0.3, scipy 1.10.1, scikit-learn 1.6.0): 4496 passed, 173 skipped, 7 xfailed; scripts/quickstart.py passes.
  • just docs: strict build succeeds.
  • just check: all pre-commit and pre-push hooks pass (file hygiene, ruff format, ruff lint, prettier, pyright with 0 errors).
  • The change stays inside pretab.encoding.categorical.ordinal; no import direction changes.

Merge notes

ContinuousOrdinalTransformer sorted the distinct categories with
np.unique, which cannot order an int next to a str. A categorical object
column such as product codes (101, "A7", 205) therefore made fit, and the
default Preprocessor, raise a TypeError.

Categories of one type keep np.unique's order. When they cannot be
compared, they are ordered by type name and naturally within each type,
so the codes stay deterministic.

Fixes #177

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The default "int" categorical encoding crashes on a column that mixes int and str categories

1 participant