Skip to content

fix(preprocessor): name constant columns by label in the policy check - #184

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/183-constant-policy-column-names
Open

ChrisW09 wants to merge 1 commit into
mainfrom
fix/183-constant-policy-column-names

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #183.

Preprocessor(policy={"constant": "error"}), or "warn", reported a constant numerical column by its position among the numerical columns only. With a categorical column before it, the message pointed at a different, non-constant column of X.

Root cause

Preprocessor.fit passed X[numerical_features].to_numpy() to apply_constant_policy (pretab/core/policy.py). That function reported positions in the array it received, and the array had lost the column labels.

Fix

  • apply_constant_policy takes an optional labels argument naming the columns of the checked array. With labels, the message lists the constant columns by label, for example Preprocessor received constant (zero-variance) column(s) ['flag'].
  • Preprocessor.fit passes labels=numerical_features, the original column labels, so integer labels are reported as given.
  • Standalone transformers (BasePreTabTransformer) check their whole input, where a position is correct, and keep the positional message.

Behaviour changes

  • The Preprocessor's constant-column error and warning name the columns by label instead of a misleading position. The exception and warning types are unchanged.
  • Standalone transformers are unchanged.

Documentation

  • apply_constant_policy docstring: the new labels argument.
  • docs/representations/choosing_a_method.md (edge cases): the policy's error or warning names the constant columns by label.

Tests

In tests/core/test_policy.py:

  • test_preprocessor_constant_policy_names_the_constant_column (string and integer labels, "error" and "warn"): fails on main, passes here.
  • test_standalone_constant_policy_still_reports_positions: guard that passes on both.

Reproduction

The script from #183 on this branch:

PretabDataError: Preprocessor received constant (zero-variance) column(s) ['flag'].
X.columns[1] = age
warn: ["Preprocessor received constant (zero-variance) column(s) ['flag']."]

Checks

  • just test: 4679 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): 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).
  • pretab.core.policy gains a keyword argument only; no import direction changes.

Merge notes

Preprocessor passed only its numerical columns, as an unlabelled array,
to apply_constant_policy, which reported positions in that array. With a
categorical column before them, policy={"constant": "error"} (or "warn")
pointed at a different, non-constant column of X.

apply_constant_policy takes optional labels for the checked columns and
lists the constant ones by label; the Preprocessor passes its numerical
feature labels. Standalone transformers check their whole input and
keep the positional message.

Fixes #183

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.

Preprocessor(policy={"constant": ...}) names the wrong column when X has non-numerical columns

1 participant