Skip to content

fix(preprocessor): accept NumPy scalars for cat_cutoff - #154

Merged
mkumar73 merged 2 commits into
mainfrom
fix/153-numpy-cat-cutoff
Nov 30, 2026
Merged

mkumar73 merged 2 commits into
mainfrom
fix/153-numpy-cat-cutoff

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #153.

Preprocessor(cat_cutoff=...) rejected NumPy scalars, so a GridSearchCV grid such as {"preprocessor__cat_cutoff": np.arange(4, 12, 4)} failed on its first candidate. A boolean was silently accepted as the count 1.

Root cause

detect_column_types (pretab/compose/feature_detection.py) branched on isinstance(cat_cutoff, float) and isinstance(cat_cutoff, int). np.int64 and np.float32 are neither, and bool is an int. The check ran inside the per-column loop, so it was reached only when the frame had a column to classify.

Fix

cat_cutoff is validated once, before the column loop:

  • any numbers.Integral (Python or NumPy integer) is an absolute unique-count cutoff;
  • any other numbers.Real (Python or NumPy float) is a unique-ratio cutoff;
  • bool / np.bool_ and non-numbers raise the same InvalidParamError as before.

The value is converted to a Python int or float before the comparisons. Python ints and floats classify exactly as before.

Behaviour changes

  • NumPy integer and float cutoffs now work, like output_dim and the other integer parameters.
  • cat_cutoff=True / False now raises InvalidParamError instead of acting as the count 1 / 0.
  • An invalid cat_cutoff is reported even when the frame has no integer column.

Documentation

  • Preprocessor docstring, cat_cutoff: NumPy scalars are read the same way; a boolean is rejected.
  • detect_column_types docstring: the same, plus a Raises entry for InvalidParamError.
  • README ("Automatic feature-type detection"), homepage, docs/ and the tutorials use plain Python values and need no change.

Tests

  • tests/compose/test_feature_detection.py::test_numpy_scalar_cutoffs_follow_their_python_counterparts (np.int64, np.int32, np.uint8, np.float32, np.float16): fails on main, passes here.
  • tests/compose/test_feature_detection.py::test_non_numeric_cat_cutoff_raises_even_without_integer_columns: the True case fails on main and passes here; np.bool_(False) and None are guards that pass on both.
  • tests/integration/test_preprocessor.py::test_grid_search_accepts_numpy_cat_cutoff_values: a GridSearchCV over np.arange cutoffs, plus the expected classification at both cutoffs. Fails on main, passes here.

Reproduction

The script from #153 on this branch:

8 -> categorical: ['rooms']
np.int64(8) -> categorical: ['rooms']
0.08 -> categorical: ['rooms']
np.float32(0.08) -> categorical: ['rooms']
True -> InvalidParamError: Preprocessor.cat_cutoff = True is invalid.
GridSearchCV -> best {'preprocessor__cat_cutoff': np.int64(8)}

to_spec() / from_spec() round-trip a NumPy cutoff (it is restored as the equal Python number) with identical transform output.

Checks

  • just test: 4685 passed, 59 skipped, 7 xfailed; coverage 95.71% (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): 4502 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.compose.feature_detection (plus the facade docstring); no import direction changes.

Merge notes

detect_column_types checked cat_cutoff with isinstance(..., float) and
isinstance(..., int), so np.int64 (as produced by np.arange in a
GridSearchCV grid) and np.float32 raised InvalidParamError, while True
was silently read as the count 1. The check also ran only inside the
column loop.

cat_cutoff is now validated once, before the loop: any numbers.Integral
is a unique-count cutoff, any other numbers.Real a unique-ratio cutoff,
and bool / np.bool_ are rejected.

Fixes #153
@mkumar73
mkumar73 merged commit 5e6c932 into main Nov 30, 2026
19 checks passed
@mkumar73
mkumar73 deleted the fix/153-numpy-cat-cutoff branch November 30, 2026 06:46
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.

Preprocessor rejects NumPy scalars for cat_cutoff, so a GridSearchCV grid built with np.arange crashes

2 participants