Skip to content

fix(transformers): return feature names as an object array - #168

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/167-feature-names-object-dtype
Open

ChrisW09 wants to merge 1 commit into
mainfrom
fix/167-feature-names-object-dtype

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #167.

Preprocessor.get_feature_names_out(), PLETransformer.get_feature_names_out() and OneHotFromOrdinalTransformer.get_feature_names_out() returned a fixed-width unicode array (<U15) instead of an object array. scikit-learn's check_transformer_get_feature_names_out failed on the Preprocessor, and a longer name assigned into a copy was silently truncated.

Root cause

The three methods returned np.array(list_of_str), which infers a fixed-width <U dtype. The other PreTab transformers, and scikit-learn, pass dtype=object.

Fix

The three methods return np.asarray(names, dtype=object):

  • Preprocessor.get_feature_names_out (pretab/preprocessor.py);
  • PLETransformer.get_feature_names_out (pretab/encoding/numerical/ple.py);
  • OneHotFromOrdinalTransformer.get_feature_names_out (pretab/encoding/categorical/one_hot.py).

The names and their order are unchanged.

Behaviour changes

  • The returned arrays have dtype=object. Element values, tolist(), equality checks and use as DataFrame columns are unchanged.
  • Code that relied on the <U dtype (for example names.dtype.kind == "U") would now see "O", the scikit-learn convention.

Documentation

  • The Returns sections of the three methods say the names are an object array.
  • docs/representations/categorical_encoding.md already shows dtype=object and is now accurate. No other docs page shows the dtype.

Tests

In tests/transformers/test_feature_names_in.py:

  • test_feature_names_out_is_an_object_array: every exported transformer, fitted on a DataFrame and on an ndarray. The PLE and OneHotFromOrdinal cases fail on main; all pass here.
  • test_preprocessor_feature_names_out_is_an_object_array: object dtype, an in-place rename that is not truncated, and scikit-learn's check_transformer_get_feature_names_out. Fails on main, passes here.

Reproduction

The script from #167 on this branch:

Preprocessor: array(['num_age', 'cat_color_blue', 'cat_color_green', 'cat_color_red'],
      dtype=object)
sklearn OneHotEncoder: object | BSplineTransformer: object | PLETransformer: object
after renamed[1] = 'color_is_blue_flag': color_is_blue_flag
check_transformer_get_feature_names_out: passed

Checks

  • just test: 4726 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): 4543 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).
  • No import changes.

Merge notes

Preprocessor, PLETransformer and OneHotFromOrdinalTransformer built their
output names with np.array(list_of_str), which gives a fixed-width <U
array. scikit-learn and the other PreTab transformers return an object
array: check_transformer_get_feature_names_out failed on the Preprocessor,
and a longer name assigned into a copy was silently truncated.

The three methods now return np.asarray(..., dtype=object).

Fixes #167

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, PLE and OneHotFromOrdinal get_feature_names_out return a fixed-width <U array instead of dtype=object

1 participant