From 0279b1d35f5d8d6218cf5b612d33e75652161dbd Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:40:59 +0200 Subject: [PATCH] fix(transformers): record and check DataFrame feature names The standalone transformers validated input with check_array or np.asarray only, so fitting on a DataFrame recorded no feature_names_in_: a reordered or renamed frame was processed by position (silently wrong features), output names were x0_..., and PLE / binning / tensor-product get_feature_names_out accepted an input_features of the wrong length (too few names, or an IndexError). This covered the feature maps, PLE, numeric binning, periodic encoding, the splines, Fourier features and the kernel approximations, and the other exported transformers: ContinuousOrdinalTransformer, OneHotFromOrdinalTransformer, ToFloatTransformer, NoTransformer, MissingStateIndicator and LanguageEmbeddingTransformer. The shared validator, the PLE and binning validation and the others now record feature_names_in_ at fit and check the column names at transform via scikit-learn's _check_feature_names (renamed or reordered columns raise, missing names warn). get_feature_names_out() resolves its prefixes from feature_names_in_ (else x0, x1, ...) and validates input_features. The binning feature-count error now uses scikit-learn's message. ToFloatTransformer and NoTransformer read the width of sparse input (the one-hot encoder's output) from its shape instead of recording a single feature. With strict input_features, get_representation_spec() could no longer build x0, x1, ... itself and pass them to get_feature_names_out: it would raise for every transformer fitted on a DataFrame, and the Preprocessor's spec summary would swallow the error, so to_spec() and reproducibility_report() would list no representation fitted directly on its column (no imputer or scaler). The spec now resolves its input names like get_feature_names_out (explicit names validated, else feature_names_in_, else x0, x1, ...). The summary passes each block's column names, so its entries read age_rbf0 instead of x0_rbf0, and no longer hides spec errors. The summary and the feature lineage let a step fitted on more inputs than its block has columns (behind the imputer's built-in missing indicator) name its own inputs, so the lineage of such a block no longer raises. get_feature_info() probes a step with a frame carrying its fitted names, so it no longer warns that X has no valid feature names. Docstrings (get_feature_names_out, feature_names_in_ attributes, the base transformer), the spline expansion guide and the custom representation tutorial describe the DataFrame behaviour. Fixes #85 --- docs/representations/spline_expansions.md | 4 +- docs/tutorials/custom_representation.md | 4 +- pretab/compose/inspection.py | 24 ++- pretab/compose/serialize.py | 24 ++- pretab/core/base.py | 41 ++-- pretab/core/representation.py | 14 +- pretab/core/validation.py | 37 +++- pretab/embedding/language.py | 29 ++- pretab/encoding/categorical/one_hot.py | 31 ++- pretab/encoding/categorical/ordinal.py | 30 ++- pretab/encoding/numerical/binning.py | 31 ++- pretab/encoding/numerical/ple.py | 25 ++- pretab/expansion/functional/fourier.py | 4 + pretab/expansion/spline/base.py | 5 + pretab/expansion/spline/cubic_regression.py | 5 + .../spline/multivariate/tensor_product.py | 32 ++- .../spline/multivariate/thin_plate.py | 4 + pretab/expansion/spline/natural_cubic.py | 5 + pretab/expansion/spline/p_spline.py | 5 + pretab/kernel_approximation/nystroem.py | 4 + pretab/kernel_approximation/random_fourier.py | 4 + pretab/preprocessing/floats.py | 64 ++++-- pretab/preprocessing/missing.py | 27 ++- tests/compose/test_inspection.py | 19 ++ tests/core/test_representation_spec.py | 29 +++ tests/extension/test_extension_protocol.py | 9 + tests/integration/test_feature_lineage.py | 19 ++ tests/integration/test_serialization.py | 37 ++++ tests/transformers/test_feature_names_in.py | 194 ++++++++++++++++++ 29 files changed, 663 insertions(+), 97 deletions(-) create mode 100644 tests/transformers/test_feature_names_in.py diff --git a/docs/representations/spline_expansions.md b/docs/representations/spline_expansions.md index d6b28d7..9d813a9 100644 --- a/docs/representations/spline_expansions.md +++ b/docs/representations/spline_expansions.md @@ -23,8 +23,8 @@ knot positions by `placement_strategy` (see For every univariate spline in this section, `output_dim` is the number of output columns **per input feature**, not the total. A `(n_samples, 3)` input produces `(n_samples, 3 * output_dim)` output (plus one extra column per feature if -`include_bias=True`). Feature names are suffixed per input, for example `x0_bs0, x0_bs1, ...` -for a B-spline on column `x0`. +`include_bias=True`). Feature names are suffixed per input, for example `age_bs0, age_bs1, ...` +for a B-spline fitted on a DataFrame column `age` (`x0_bs0, x0_bs1, ...` for array input). ``` ## B-spline diff --git a/docs/tutorials/custom_representation.md b/docs/tutorials/custom_representation.md index 978f8c4..b7da366 100644 --- a/docs/tutorials/custom_representation.md +++ b/docs/tutorials/custom_representation.md @@ -14,7 +14,9 @@ A complete, installable version of this example lives in the repository under `BaseRepresentation` gives you the shared scikit-learn contract: NaN-aware validation, estimator tags, `get_feature_names_out`, and a typed `RepresentationSpec`. You implement `fit`, -`transform`, and one sizing hook, and declare a small amount of metadata. +`transform`, and one sizing hook, and declare a small amount of metadata. Fitted on a DataFrame, +`self._validate` records the column names as `feature_names_in_` and checks them at `transform`, +and the output names use them (`length_f0` rather than `x0_f0`). ```python import numpy as np diff --git a/pretab/compose/inspection.py b/pretab/compose/inspection.py index 7f3b9b3..ef9d2d6 100644 --- a/pretab/compose/inspection.py +++ b/pretab/compose/inspection.py @@ -8,6 +8,7 @@ """ import numpy as np +import pandas as pd from sklearn.pipeline import FeatureUnion from ..core.logging import get_logger @@ -86,6 +87,19 @@ def _separate_state_branches(transformer): return branches["representation"], branches["missing"] +def _probe_input(step, value): + """Return a one-row probe input for measuring a fitted step's output width. + + A step fitted directly on the ColumnTransformer's DataFrame column records + ``feature_names_in_`` and, like scikit-learn, warns on input without feature + names, so its probe is a DataFrame carrying the fitted names. + """ + names = getattr(step, "feature_names_in_", None) + if names is None: + return np.full((1, 1), value) + return pd.DataFrame(np.full((1, len(names)), value), columns=names) + + def build_feature_info(column_transformer, *, embeddings, embedding_dimensions): """Collect per-feature metadata (preprocessing, dimension, categories). @@ -139,7 +153,7 @@ def build_feature_info(column_transformer, *, embeddings, embedding_dimensions): ): last_step = representation_pipeline.steps[-1][1] if hasattr(last_step, "transform"): - dummy_input = np.zeros((1, 1)) + 1e-05 + dummy_input = _probe_input(last_step, 1e-05) try: transformed_feature = last_step.transform(dummy_input) dimension = transformed_feature.shape[1] @@ -184,7 +198,7 @@ def build_feature_info(column_transformer, *, embeddings, embedding_dimensions): else: last_step = representation_pipeline.steps[-1][1] if hasattr(last_step, "transform"): - dummy_input = np.zeros((1, 1)) + dummy_input = _probe_input(last_step, 0.0) try: transformed_feature = last_step.transform(dummy_input) dimension = transformed_feature.shape[1] @@ -263,7 +277,11 @@ def _resolve_block_representation(pipeline, columns): steps = pipeline.steps if hasattr(pipeline, "steps") else [("_", pipeline)] for step_name, transformer in reversed(steps): if hasattr(transformer, "get_representation_spec"): - spec = transformer.get_representation_spec(input_features=list(columns)) + # A step fitted on more inputs than the block has columns (behind the + # imputer's built-in missing indicator) names its own inputs; its + # get_feature_names_out rejects the block's shorter column list. + fits_block = getattr(transformer, "n_features_in_", len(columns)) == len(columns) + spec = transformer.get_representation_spec(input_features=list(columns) if fits_block else None) return spec.family, spec.component_kind, spec.uses_target, spec.is_interaction if step_name in _STEP_FAMILY: family, component = _STEP_FAMILY[step_name] diff --git a/pretab/compose/serialize.py b/pretab/compose/serialize.py index c4b213f..1fe02ad 100644 --- a/pretab/compose/serialize.py +++ b/pretab/compose/serialize.py @@ -27,7 +27,7 @@ from ..core.parameters import UNSET from ..core.policy import RepresentationPolicy from ..core.representation import FeatureLineage, RepresentationSpec -from ..exceptions import PretabError, PretabSerializationError +from ..exceptions import PretabSerializationError from ..placement.base import PlacementResult from .registry import TransformerSpec @@ -227,7 +227,15 @@ def _library_versions() -> dict: def _representation_summary(preprocessor) -> list: - """Best-effort declarative per-representation summary (family/columns/locations).""" + """Declarative per-representation summary (family/columns/locations). + + One entry per block that ends in a PreTab transformer, built from its + :class:`RepresentationSpec` with the block's column names as input features + (as the feature lineage does). Blocks that end in a step without + ``get_representation_spec`` (scikit-learn scalers and encoders, embeddings) + have no entry. The steps are fitted, so a spec that fails to build is a bug: + its error propagates instead of silently dropping the entry. + """ summary: list = [] column_transformer = getattr(preprocessor, "column_transformer_", None) if column_transformer is None: @@ -239,11 +247,13 @@ def _representation_summary(preprocessor) -> list: spec_fn = getattr(leaf, "get_representation_spec", None) if spec_fn is None: continue - try: - entry = spec_fn().to_dict() - except (PretabError, ValueError, AttributeError, TypeError, KeyError): - continue - entry["columns"] = [str(col) for col in columns] + block_columns = [str(col) for col in columns] + # A leaf with more inputs than its block has columns (one fitted behind + # the imputer's built-in missing indicator, add_missing_indicator=True) + # names its own inputs. + fits_block = getattr(leaf, "n_features_in_", len(block_columns)) == len(block_columns) + entry = spec_fn(input_features=block_columns if fits_block else None).to_dict() + entry["columns"] = block_columns summary.append(entry) return summary diff --git a/pretab/core/base.py b/pretab/core/base.py index 284a0da..5dbd3c5 100644 --- a/pretab/core/base.py +++ b/pretab/core/base.py @@ -10,12 +10,11 @@ from sklearn.base import BaseEstimator, TransformerMixin from sklearn.utils.validation import check_is_fitted -from ..exceptions import invalid_param_error from .adaptive import AdaptiveResolutionMixin from .parameters import AliasResolverMixin from .policy import RepresentationPolicy, apply_constant_policy from .representation import RepresentationSpecMixin -from .validation import validate_2d_allow_nan +from .validation import resolve_input_features, validate_2d_allow_nan __all__ = ["BasePreTabTransformer"] @@ -37,6 +36,11 @@ class BasePreTabTransformer( means "inherit the shared policy"); :meth:`_resolved_policy` merges them. Transformers that expose their own ``policy`` constructor parameter let a caller override those defaults per-instance; see :meth:`_resolved_policy`. + + Like scikit-learn's transformers, a transformer fitted on a DataFrame with + string column names records ``feature_names_in_``, names its outputs after the + columns, and checks the columns at ``transform``: renamed or reordered columns + raise a ``ValueError``, and input without column names warns. """ _allow_nan: bool = True @@ -88,17 +92,30 @@ def _output_sizes(self) -> list[int]: raise NotImplementedError def get_feature_names_out(self, input_features=None): - """Return output feature names of the form ``{feature}_{suffix}{j}``.""" + """Return output feature names of the form ``{feature}_{suffix}{j}``. + + Parameters + ---------- + input_features : array-like of str or None, default=None + Input feature names. ``None`` uses ``feature_names_in_`` when the + transformer was fitted on a DataFrame, else ``x0, x1, ...``. Explicit + names need one entry per input feature and, after a DataFrame fit, + must equal ``feature_names_in_``. + + Returns + ------- + feature_names_out : ndarray of str + One name per output column. + + Raises + ------ + InvalidParamError + If ``input_features`` does not have one entry per input feature. + ValueError + If ``input_features`` differs from ``feature_names_in_``. + """ check_is_fitted(self, "n_features_in_") - if input_features is None: - input_features = [f"x{i}" for i in range(self.n_features_in_)] - elif len(input_features) != self.n_features_in_: - raise invalid_param_error( - type(self).__name__, - "get_feature_names_out.input_features", - len(input_features), - f"must have exactly {self.n_features_in_} entries (one per input feature)", - ) + input_features = resolve_input_features(self, input_features) suffix = self._feature_suffix() names = [] for feature, n_cols in zip(input_features, self._output_sizes(), strict=False): diff --git a/pretab/core/representation.py b/pretab/core/representation.py index 0c2676b..bf1bf8e 100644 --- a/pretab/core/representation.py +++ b/pretab/core/representation.py @@ -15,6 +15,8 @@ import numpy as np from sklearn.utils.validation import check_is_fitted +from .validation import resolve_input_features + __all__ = [ "FeatureLineage", "RepresentationSpec", @@ -297,8 +299,11 @@ def get_representation_spec(self, input_features=None) -> RepresentationSpec: Parameters ---------- input_features : list of str or None - Names of the input features. When ``None``, names of the form - ``x0, x1, ...`` are generated. + Names of the input features, resolved as in ``get_feature_names_out``: + explicit names need one entry per input feature and, after a fit on + named (DataFrame) columns, must equal ``feature_names_in_``. When + ``None``, ``feature_names_in_`` is used if it was recorded at fit, + else names of the form ``x0, x1, ...`` are generated. Returns ------- @@ -306,10 +311,7 @@ def get_representation_spec(self, input_features=None) -> RepresentationSpec: The representation metadata describing the produced columns. """ check_is_fitted(self, "n_features_in_") - if input_features is None: - inputs = tuple(f"x{i}" for i in range(self.n_features_in_)) - else: - inputs = tuple(str(feature) for feature in input_features) + inputs = tuple(resolve_input_features(self, input_features)) output_features = tuple(str(name) for name in self.get_feature_names_out(list(inputs))) location_kind, locations = self._representation_locations() periodic, period = self._representation_periodic() diff --git a/pretab/core/validation.py b/pretab/core/validation.py index fbe7ea7..8a271e7 100644 --- a/pretab/core/validation.py +++ b/pretab/core/validation.py @@ -7,14 +7,34 @@ """ import warnings -from typing import Literal +from typing import Literal, cast import numpy as np -from sklearn.utils.validation import check_array +from sklearn.utils.validation import _check_feature_names, _check_feature_names_in, check_array -from ..exceptions import DataWarning, PretabDataError +from ..exceptions import DataWarning, PretabDataError, invalid_param_error -__all__ = ["validate_2d_allow_nan"] +__all__ = ["resolve_input_features", "validate_2d_allow_nan"] + + +def resolve_input_features(estimator, input_features) -> list: + """Return the input feature names that a transformer's output names are built from. + + Follows scikit-learn's ``get_feature_names_out`` contract: explicit + ``input_features`` need one entry per input feature and, when the estimator was + fitted on named (DataFrame) columns, must equal ``feature_names_in_``; ``None`` + falls back to ``feature_names_in_``, else to ``x0, x1, ...``. + """ + n_features_in_ = getattr(estimator, "n_features_in_", None) + if input_features is not None and n_features_in_ is not None and len(input_features) != n_features_in_: + raise invalid_param_error( + type(estimator).__name__, + "get_feature_names_out.input_features", + len(input_features), + f"must have exactly {n_features_in_} entries (one per input feature)", + ) + names = cast(np.ndarray, _check_feature_names_in(estimator, input_features)) + return [str(name) for name in names] def validate_2d_allow_nan(X, *, allow_nan: bool = True, reset: bool, estimator): @@ -28,9 +48,11 @@ def validate_2d_allow_nan(X, *, allow_nan: bool = True, reset: bool, estimator): When True, missing values are preserved so a later imputer can handle them; when False, NaN/inf values raise as usual. reset : bool - When True (during ``fit``) record ``estimator.n_features_in_``; when - False (during ``transform``) verify the feature count matches the value - seen at ``fit`` and raise otherwise. + When True (during ``fit``) record ``estimator.n_features_in_`` and, for a + DataFrame, ``estimator.feature_names_in_``; when False (during + ``transform``) verify the feature count matches the value seen at ``fit`` + and raise otherwise, and check the column names as scikit-learn does + (renamed or reordered columns raise). estimator : object The calling transformer; used for the ``n_features_in_`` side effect and the transform-time feature-count check. @@ -40,6 +62,7 @@ def validate_2d_allow_nan(X, *, allow_nan: bool = True, reset: bool, estimator): X : ndarray of shape (n_samples, n_features) The validated float array. """ + _check_feature_names(estimator, X, reset=reset) input_shape = getattr(X, "shape", None) original_dim = input_shape[1] if input_shape is not None and len(input_shape) == 2 else None ensure_all_finite: Literal["allow-nan"] | bool = "allow-nan" if allow_nan else True diff --git a/pretab/embedding/language.py b/pretab/embedding/language.py index e8cded9..71c0479 100644 --- a/pretab/embedding/language.py +++ b/pretab/embedding/language.py @@ -1,6 +1,8 @@ +from typing import cast + import numpy as np from sklearn.base import BaseEstimator, TransformerMixin -from sklearn.utils.validation import check_is_fitted +from sklearn.utils.validation import _check_feature_names, _check_feature_names_in, check_is_fitted from ..exceptions import OptionalDependencyError, PretabConfigError, PretabDataError @@ -27,6 +29,10 @@ class LanguageEmbeddingTransformer(TransformerMixin, BaseEstimator): ``fit`` from ``model`` or by loading ``model_name``. n_features_in_ : int Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. embedding_dim_ : int Dimensionality of the embeddings produced by ``model_``. @@ -76,6 +82,7 @@ def fit(self, X, y=None): self : object Fitted transformer. """ + _check_feature_names(self, X, reset=True) X = np.asarray(X) self.n_features_in_ = X.shape[1] if X.ndim > 1 else 1 self.model_ = self._resolve_model() @@ -115,6 +122,7 @@ def transform(self, X): """ if getattr(self, "model_", None) is None: raise PretabConfigError("Model is not initialized. Call `fit` before `transform`.") + _check_feature_names(self, X, reset=False) # Normalise to a 2D array of strings so each column is encoded on its own # and the row count is preserved (a flat encode would return @@ -143,16 +151,23 @@ def get_feature_names_out(self, input_features=None): Parameters ---------- - input_features : array-like of str or None - Input feature names. When ``None``, names of the form ``x0, x1, ...`` - are generated. + input_features : array-like of str or None, default=None + Input feature names. ``None`` uses ``feature_names_in_`` when the + transformer was fitted on a DataFrame, else ``x0, x1, ...``. Explicit + names need one entry per input feature and, after a DataFrame fit, + must equal ``feature_names_in_``. Returns ------- feature_names_out : ndarray of str, shape (n_features_in_ * embedding_dim_,) + + Raises + ------ + ValueError + If ``input_features`` does not have one entry per input feature, or + differs from ``feature_names_in_``. """ check_is_fitted(self, ["n_features_in_", "embedding_dim_"]) - if input_features is None: - input_features = [f"x{i}" for i in range(self.n_features_in_)] - names = [f"{col}_emb{j}" for col in input_features for j in range(self.embedding_dim_)] + columns = cast(np.ndarray, _check_feature_names_in(self, input_features)) + names = [f"{col}_emb{j}" for col in columns for j in range(self.embedding_dim_)] return np.asarray(names, dtype=object) diff --git a/pretab/encoding/categorical/one_hot.py b/pretab/encoding/categorical/one_hot.py index 3aa569c..caaa6de 100644 --- a/pretab/encoding/categorical/one_hot.py +++ b/pretab/encoding/categorical/one_hot.py @@ -2,9 +2,10 @@ import numpy as np from sklearn.base import BaseEstimator, TransformerMixin -from sklearn.utils.validation import check_is_fitted +from sklearn.utils.validation import _check_feature_names, check_is_fitted from ...core.representation import RepresentationSpecMixin +from ...core.validation import resolve_input_features from ...exceptions import PretabDataError _NOT_ORDINAL_MSG = ( @@ -33,6 +34,12 @@ class OneHotFromOrdinalTransformer(RepresentationSpecMixin, TransformerMixin, Ba max_bins_ : ndarray of shape (n_features,) Array containing the maximum bin index (plus one) for each feature, which determines the width of the one-hot block for that feature. + n_features_in_ : int + Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. Notes ----- @@ -77,6 +84,7 @@ def fit(self, X, y=None): self : object Fitted transformer. """ + _check_feature_names(self, X, reset=True) X = np.asarray(X) try: codes = X.astype(int) @@ -108,6 +116,7 @@ def transform(self, X): all-zero row, mirroring scikit-learn's ``handle_unknown="ignore"``. """ check_is_fitted(self, "max_bins_") + _check_feature_names(self, X, reset=False) X = np.asarray(X) try: X = X.astype(int) @@ -132,17 +141,29 @@ def get_feature_names_out(self, input_features=None): Parameters ---------- - input_features : list of str - The names of the input features that were ordinal-encoded. + input_features : array-like of str or None, default=None + Input feature names. ``None`` uses ``feature_names_in_`` when the + transformer was fitted on a DataFrame, else ``x0, x1, ...``. Explicit + names need one entry per input feature and, after a DataFrame fit, + must equal ``feature_names_in_``. Returns ------- feature_names : ndarray of shape (n_output_features,) The names of the one-hot encoded features. + + Raises + ------ + InvalidParamError + If ``input_features`` does not have one entry per input feature. + ValueError + If ``input_features`` differs from ``feature_names_in_``. """ + check_is_fitted(self, "max_bins_") + input_features = resolve_input_features(self, input_features) feature_names = [] - for i, max_bins in enumerate(self.max_bins_): - feature_names.extend([f"{input_features[i]}_bin_{j}" for j in range(int(max_bins))]) # type: ignore + for name, max_bins in zip(input_features, self.max_bins_, strict=True): + feature_names.extend([f"{name}_bin_{j}" for j in range(int(max_bins))]) return np.array(feature_names) def __sklearn_tags__(self): diff --git a/pretab/encoding/categorical/ordinal.py b/pretab/encoding/categorical/ordinal.py index 24621ef..75b5013 100644 --- a/pretab/encoding/categorical/ordinal.py +++ b/pretab/encoding/categorical/ordinal.py @@ -1,8 +1,9 @@ import numpy as np from sklearn.base import BaseEstimator, TransformerMixin -from sklearn.utils.validation import check_is_fitted +from sklearn.utils.validation import _check_feature_names, check_is_fitted from ...core.representation import RepresentationSpecMixin +from ...core.validation import resolve_input_features from ...exceptions import PretabDataError @@ -28,6 +29,12 @@ class ContinuousOrdinalTransformer(RepresentationSpecMixin, TransformerMixin, Ba ---------- mapping_ : list of dict One dictionary per feature mapping original categories to integers. + n_features_in_ : int + Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. Notes ----- @@ -62,6 +69,7 @@ def fit(self, X, y=None): self : object Fitted transformer. """ + _check_feature_names(self, X, reset=True) # Coerce to 2-D ndarray so DataFrame.T / DataFrame row-iteration work correctly X = np.asarray(X, dtype=object) if X.ndim == 1: @@ -93,6 +101,7 @@ def transform(self, X): The transformed data with integer values. """ check_is_fitted(self, "mapping_") + _check_feature_names(self, X, reset=False) # Coerce to 2-D ndarray so DataFrame row-iteration and empty-input shape both work X = np.asarray(X, dtype=object) if X.ndim == 1: @@ -112,19 +121,26 @@ def get_feature_names_out(self, input_features=None): Parameters ---------- - input_features : list of str or None - The names of the input features. When ``None``, names of the form - ``x0, x1, ...`` are generated. + input_features : array-like of str or None, default=None + Input feature names. ``None`` uses ``feature_names_in_`` when the + transformer was fitted on a DataFrame, else ``x0, x1, ...``. Explicit + names need one entry per input feature and, after a DataFrame fit, + must equal ``feature_names_in_``. Returns ------- feature_names : ndarray of shape (n_features,) The names of the output features after transformation. + + Raises + ------ + InvalidParamError + If ``input_features`` does not have one entry per input feature. + ValueError + If ``input_features`` differs from ``feature_names_in_``. """ check_is_fitted(self, "mapping_") - if input_features is None: - input_features = [f"x{i}" for i in range(self.n_features_in_)] - return np.asarray(input_features, dtype=object) + return np.asarray(resolve_input_features(self, input_features), dtype=object) def __sklearn_tags__(self): """Declare that missing/unknown categories are handled (mapped to 0).""" diff --git a/pretab/encoding/numerical/binning.py b/pretab/encoding/numerical/binning.py index 4b1fcbf..15838fe 100644 --- a/pretab/encoding/numerical/binning.py +++ b/pretab/encoding/numerical/binning.py @@ -2,10 +2,11 @@ import numpy as np from sklearn.base import BaseEstimator, TransformerMixin -from sklearn.utils.validation import check_is_fitted +from sklearn.utils.validation import _check_feature_names, check_is_fitted from ...core.parameters import UNSET, AliasResolverMixin from ...core.representation import RepresentationSpecMixin +from ...core.validation import resolve_input_features from ...exceptions import InsufficientSamplesError, InvalidParamError, PretabDataError _VALID_ENCODINGS = ("ordinal", "onehot", "soft") @@ -47,6 +48,10 @@ class NumericBinningTransformer(RepresentationSpecMixin, AliasResolverMixin, Tra ---------- n_features_in_ : int The number of input features seen during :meth:`fit`. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. bin_edges_ : list of ndarray The sorted, de-duplicated bin edges learned per feature. n_bins_ : list of int @@ -89,7 +94,8 @@ def __init__(self, output_dim=UNSET, encode="ordinal", placement_strategy="unifo self.placement_strategy = placement_strategy def _check_array(self, X, *, reset): - """Validate ``X`` is a 2D numeric array and (re)set the feature count.""" + """Validate ``X`` is a 2D numeric array and (re)set the feature count and names.""" + _check_feature_names(self, X, reset=reset) X = np.asarray(X) if X.ndim != 2: raise PretabDataError("Input must be a 2D array of shape (n_samples, n_features).") @@ -120,7 +126,8 @@ def _check_array(self, X, *, reset): self.n_features_in_ = X.shape[1] elif X.shape[1] != self.n_features_in_: raise PretabDataError( - f"Input has {X.shape[1]} features, but NumericBinningTransformer was fitted with {self.n_features_in_}." + f"X has {X.shape[1]} features, but NumericBinningTransformer is expecting " + f"{self.n_features_in_} features as input." ) return X @@ -239,19 +246,27 @@ def get_feature_names_out(self, input_features=None): Parameters ---------- - input_features : list of str or None - The names of the input features. When ``None``, names of the form - ``x0, x1, ...`` are generated. + input_features : array-like of str or None, default=None + Input feature names. ``None`` uses ``feature_names_in_`` when the + transformer was fitted on a DataFrame, else ``x0, x1, ...``. Explicit + names need one entry per input feature and, after a DataFrame fit, + must equal ``feature_names_in_``. Returns ------- feature_names : ndarray of shape (total_output_dim_,) One name per input feature for ``encode="ordinal"``; otherwise one ``"{feature}_bin{k}"`` name per bin. + + Raises + ------ + InvalidParamError + If ``input_features`` does not have one entry per input feature. + ValueError + If ``input_features`` differs from ``feature_names_in_``. """ check_is_fitted(self, "n_features_in_") - if input_features is None: - input_features = [f"x{i}" for i in range(self.n_features_in_)] + input_features = resolve_input_features(self, input_features) if self.encode == "ordinal": return np.asarray(input_features, dtype=object) check_is_fitted(self, "n_bins_") diff --git a/pretab/encoding/numerical/ple.py b/pretab/encoding/numerical/ple.py index 81a639c..2afed3a 100644 --- a/pretab/encoding/numerical/ple.py +++ b/pretab/encoding/numerical/ple.py @@ -10,12 +10,13 @@ import numpy as np from sklearn.base import BaseEstimator, TransformerMixin -from sklearn.utils.validation import check_array, check_is_fitted +from sklearn.utils.validation import _check_feature_names, check_array, check_is_fitted from ...core.adaptive import AdaptiveResolutionMixin from ...core.parameters import UNSET, AliasResolverMixin from ...core.representation import RepresentationSpecMixin from ...core.supervised import warn_target_leakage +from ...core.validation import resolve_input_features from ...exceptions import ( IncompatibleParamsError, InvalidParamError, @@ -76,6 +77,10 @@ class PLETransformer( to ``[0, 1]``. n_features_in_ : int Number of features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. n_bins_per_feature_ : list of int Number of output bins produced for each feature (``len(thresholds) + 1``). total_output_dim_ : int @@ -161,6 +166,7 @@ def fit(self, X, y=None): "PLETransformer is always target-aware and requires y at fit time; got y=None." ) + _check_feature_names(self, X, reset=True) X = check_array( X, dtype=np.float64, # type: ignore @@ -230,6 +236,7 @@ def transform(self, X): """ check_is_fitted(self, ["thresholds_", "n_features_in_"]) + _check_feature_names(self, X, reset=False) X = check_array( X, dtype=np.float64, # type: ignore @@ -318,17 +325,25 @@ def get_feature_names_out(self, input_features=None): Parameters ---------- input_features : array-like of str or None, default=None - Input feature names. If None, generic names ``x0``, ``x1`` ... are used. + Input feature names. ``None`` uses ``feature_names_in_`` when the + transformer was fitted on a DataFrame, else ``x0, x1, ...``. Explicit + names need one entry per input feature and, after a DataFrame fit, + must equal ``feature_names_in_``. Returns ------- feature_names_out : ndarray of str One name per output column, formatted ``{name}_ple{j}``. + + Raises + ------ + InvalidParamError + If ``input_features`` does not have one entry per input feature. + ValueError + If ``input_features`` differs from ``feature_names_in_``. """ check_is_fitted(self, ["thresholds_", "n_features_in_"]) - - if input_features is None: - input_features = [f"x{i}" for i in range(self.n_features_in_)] + input_features = resolve_input_features(self, input_features) feature_names_out = [] for name, n_bins in zip(input_features, self.n_bins_per_feature_, strict=False): diff --git a/pretab/expansion/functional/fourier.py b/pretab/expansion/functional/fourier.py index 00d1e5b..adf826e 100644 --- a/pretab/expansion/functional/fourier.py +++ b/pretab/expansion/functional/fourier.py @@ -43,6 +43,10 @@ class FourierFeatureTransformer(BasePreTabTransformer): Per-feature angular frequencies used to build the sine/cosine bank. n_features_in_ : int Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. total_output_dim_ : int Total number of output columns produced across all input features. diff --git a/pretab/expansion/spline/base.py b/pretab/expansion/spline/base.py index ab365bf..2188b7a 100644 --- a/pretab/expansion/spline/base.py +++ b/pretab/expansion/spline/base.py @@ -117,6 +117,11 @@ class BaseSplineTransformer(BasePreTabTransformer): n_features_in_ : int Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. + total_output_dim_ : int Total number of output columns across all features (fitted). diff --git a/pretab/expansion/spline/cubic_regression.py b/pretab/expansion/spline/cubic_regression.py index 392a99d..713d767 100644 --- a/pretab/expansion/spline/cubic_regression.py +++ b/pretab/expansion/spline/cubic_regression.py @@ -93,6 +93,11 @@ class CubicRegressionSplineTransformer(SplineBasisMixin, TransformerMixin, BaseE n_features_in_ : int Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. + total_output_dim_ : int Total number of output columns across all features (fitted); equals ``n_features * (output_dim (+1 if include_bias))``. diff --git a/pretab/expansion/spline/multivariate/tensor_product.py b/pretab/expansion/spline/multivariate/tensor_product.py index 2be256b..00119a4 100644 --- a/pretab/expansion/spline/multivariate/tensor_product.py +++ b/pretab/expansion/spline/multivariate/tensor_product.py @@ -5,6 +5,7 @@ from ....core.knots import bspline_basis from ....core.parameters import UNSET from ....core.policy import RepresentationPolicy, resolve_out_of_range +from ....core.validation import resolve_input_features from ....exceptions import InvalidParamError from ..mixins import SplineBasisMixin @@ -99,6 +100,11 @@ class TensorProductSplineTransformer(SplineBasisMixin, TransformerMixin, BaseEst n_features_in_ : int Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. + total_output_dim_ : int Total number of output columns (fitted); equals ``output_dim ** dim_`` (``(output_dim + 1) ** dim_`` with a bias). @@ -244,10 +250,30 @@ def transform(self, X): return design def get_feature_names_out(self, input_features=None): - """Return names for the interaction basis as ``tp_{feat0 i}_{feat1 j}...``.""" + """Return names for the interaction basis as ``tp_{feat0 i}_{feat1 j}...``. + + Parameters + ---------- + input_features : array-like of str or None, default=None + Input feature names. ``None`` uses ``feature_names_in_`` when the + transformer was fitted on a DataFrame, else ``x0, x1, ...``. Explicit + names need one entry per input feature and, after a DataFrame fit, + must equal ``feature_names_in_``. + + Returns + ------- + feature_names_out : ndarray of str + One name per tensor-product basis column. + + Raises + ------ + InvalidParamError + If ``input_features`` does not have one entry per input feature. + ValueError + If ``input_features`` differs from ``feature_names_in_``. + """ check_is_fitted(self, "marginal_sizes_") - if input_features is None: - input_features = [f"x{i}" for i in range(self.n_features_in_)] + input_features = resolve_input_features(self, input_features) sizes = self.marginal_sizes_ names = [] for multi_index in np.ndindex(*sizes): diff --git a/pretab/expansion/spline/multivariate/thin_plate.py b/pretab/expansion/spline/multivariate/thin_plate.py index a9aaa84..7c4d943 100644 --- a/pretab/expansion/spline/multivariate/thin_plate.py +++ b/pretab/expansion/spline/multivariate/thin_plate.py @@ -67,6 +67,10 @@ class ThinPlateSplineTransformer(SplineBasisMixin, TransformerMixin, BaseEstimat optional bias). n_features_in_ : int Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. total_output_dim_ : int Total number of output columns (fitted); equals ``n_components`` (``+1`` when ``include_bias``). diff --git a/pretab/expansion/spline/natural_cubic.py b/pretab/expansion/spline/natural_cubic.py index 21c3f45..93188f5 100644 --- a/pretab/expansion/spline/natural_cubic.py +++ b/pretab/expansion/spline/natural_cubic.py @@ -96,6 +96,11 @@ class NaturalCubicSplineTransformer(SplineBasisMixin, TransformerMixin, BaseEsti n_features_in_ : int Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. + total_output_dim_ : int Total number of output columns across all features (fitted); equals ``n_features * (output_dim (+1 if include_bias))``. diff --git a/pretab/expansion/spline/p_spline.py b/pretab/expansion/spline/p_spline.py index 8328484..8aabea7 100644 --- a/pretab/expansion/spline/p_spline.py +++ b/pretab/expansion/spline/p_spline.py @@ -94,6 +94,11 @@ class PSplineTransformer(SplineBasisMixin, TransformerMixin, BaseEstimator): n_features_in_ : int Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. + total_output_dim_ : int Total number of output columns across all features (fitted); equals ``n_features * (output_dim (+1 if include_bias))``. diff --git a/pretab/kernel_approximation/nystroem.py b/pretab/kernel_approximation/nystroem.py index 49fa24a..67f4bc9 100644 --- a/pretab/kernel_approximation/nystroem.py +++ b/pretab/kernel_approximation/nystroem.py @@ -41,6 +41,10 @@ class NystroemFeaturesTransformer(BasePreTabTransformer): The fitted underlying scikit-learn estimator. n_features_in_ : int Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. total_output_dim_ : int Total number of output columns (the effective number of landmarks). diff --git a/pretab/kernel_approximation/random_fourier.py b/pretab/kernel_approximation/random_fourier.py index ccb0b8e..f103828 100644 --- a/pretab/kernel_approximation/random_fourier.py +++ b/pretab/kernel_approximation/random_fourier.py @@ -31,6 +31,10 @@ class RandomFourierFeaturesTransformer(BasePreTabTransformer): The fitted underlying scikit-learn sampler. n_features_in_ : int Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. total_output_dim_ : int Total number of output columns (equals ``n_components``). diff --git a/pretab/preprocessing/floats.py b/pretab/preprocessing/floats.py index d6cdb0d..2179613 100644 --- a/pretab/preprocessing/floats.py +++ b/pretab/preprocessing/floats.py @@ -1,6 +1,6 @@ import numpy as np from sklearn.base import BaseEstimator, TransformerMixin -from sklearn.utils.validation import check_is_fitted +from sklearn.utils.validation import _check_feature_names, _check_feature_names_in, check_is_fitted class NoTransformer(TransformerMixin, BaseEstimator): @@ -13,6 +13,10 @@ class NoTransformer(TransformerMixin, BaseEstimator): ---------- n_features_in_ : int Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. Examples -------- @@ -38,8 +42,12 @@ def fit(self, X, y=None): self : object Fitted transformer. """ - X = np.asarray(X) - self.n_features_in_ = X.shape[1] if X.ndim > 1 else 1 + _check_feature_names(self, X, reset=True) + # np.shape reads ``.shape`` directly, so a sparse matrix (the one-hot + # encoder's output) reports its real width instead of the 0-d object + # array that np.asarray makes of it. + shape = np.shape(X) + self.n_features_in_ = shape[1] if len(shape) > 1 else 1 return self def transform(self, X): @@ -56,6 +64,7 @@ def transform(self, X): The same input data, unmodified. """ check_is_fitted(self, "n_features_in_") + _check_feature_names(self, X, reset=False) return X def get_feature_names_out(self, input_features=None): @@ -63,19 +72,25 @@ def get_feature_names_out(self, input_features=None): Parameters ---------- - input_features : list of str or None - The names of the input features. When ``None``, names of the form - ``x0, x1, ...`` are generated. + input_features : array-like of str or None, default=None + Input feature names. ``None`` uses ``feature_names_in_`` when the + transformer was fitted on a DataFrame, else ``x0, x1, ...``. Explicit + names need one entry per input feature and, after a DataFrame fit, + must equal ``feature_names_in_``. Returns ------- feature_names : ndarray of shape (n_features,) The output feature names. + + Raises + ------ + ValueError + If ``input_features`` does not have one entry per input feature, or + differs from ``feature_names_in_``. """ check_is_fitted(self, "n_features_in_") - if input_features is None: - input_features = [f"x{i}" for i in range(self.n_features_in_)] - return np.asarray(input_features, dtype=object) + return np.asarray(_check_feature_names_in(self, input_features), dtype=object) def __sklearn_tags__(self): """Declare that missing values pass through unchanged.""" @@ -91,6 +106,10 @@ class ToFloatTransformer(TransformerMixin, BaseEstimator): ---------- n_features_in_ : int Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. Examples -------- @@ -116,8 +135,12 @@ def fit(self, X, y=None): self : object Fitted transformer. """ - X = np.asarray(X) - self.n_features_in_ = X.shape[1] if X.ndim > 1 else 1 + _check_feature_names(self, X, reset=True) + # np.shape reads ``.shape`` directly, so a sparse matrix (the one-hot + # encoder's output) reports its real width instead of the 0-d object + # array that np.asarray makes of it. + shape = np.shape(X) + self.n_features_in_ = shape[1] if len(shape) > 1 else 1 return self def transform(self, X): @@ -134,6 +157,7 @@ def transform(self, X): The input data cast to ``float``. """ check_is_fitted(self, "n_features_in_") + _check_feature_names(self, X, reset=False) return X.astype(float) def get_feature_names_out(self, input_features=None): @@ -141,19 +165,25 @@ def get_feature_names_out(self, input_features=None): Parameters ---------- - input_features : list of str or None - The names of the input features. When ``None``, names of the form - ``x0, x1, ...`` are generated. + input_features : array-like of str or None, default=None + Input feature names. ``None`` uses ``feature_names_in_`` when the + transformer was fitted on a DataFrame, else ``x0, x1, ...``. Explicit + names need one entry per input feature and, after a DataFrame fit, + must equal ``feature_names_in_``. Returns ------- feature_names : ndarray of shape (n_features,) The output feature names. + + Raises + ------ + ValueError + If ``input_features`` does not have one entry per input feature, or + differs from ``feature_names_in_``. """ check_is_fitted(self, "n_features_in_") - if input_features is None: - input_features = [f"x{i}" for i in range(self.n_features_in_)] - return np.asarray(input_features, dtype=object) + return np.asarray(_check_feature_names_in(self, input_features), dtype=object) def __sklearn_tags__(self): """Declare that missing values pass through the float cast.""" diff --git a/pretab/preprocessing/missing.py b/pretab/preprocessing/missing.py index bc6d6d1..53a9e8f 100644 --- a/pretab/preprocessing/missing.py +++ b/pretab/preprocessing/missing.py @@ -1,7 +1,7 @@ import numpy as np import pandas as pd from sklearn.base import BaseEstimator, TransformerMixin -from sklearn.utils.validation import check_is_fitted +from sklearn.utils.validation import _check_feature_names, _check_feature_names_in, check_is_fitted class MissingStateIndicator(TransformerMixin, BaseEstimator): @@ -20,6 +20,10 @@ class MissingStateIndicator(TransformerMixin, BaseEstimator): ---------- n_features_in_ : int Number of input features seen during ``fit``. + feature_names_in_ : ndarray of str + Names of the input columns seen during ``fit``. Defined only when ``X`` + was a DataFrame with string column names; ``transform`` then requires + the same columns in the same order. Examples -------- @@ -47,6 +51,7 @@ def fit(self, X, y=None): self : object Fitted transformer. """ + _check_feature_names(self, X, reset=True) X = np.asarray(X) self.n_features_in_ = X.shape[1] if X.ndim > 1 else 1 return self @@ -65,6 +70,7 @@ def transform(self, X): The missingness indicator as ``float``. """ check_is_fitted(self, "n_features_in_") + _check_feature_names(self, X, reset=False) X = np.asarray(X) if X.ndim == 1: X = X.reshape(-1, 1) @@ -75,19 +81,26 @@ def get_feature_names_out(self, input_features=None): Parameters ---------- - input_features : list of str or None - The names of the input features. When ``None``, names of the form - ``x0, x1, ...`` are generated. + input_features : array-like of str or None, default=None + Input feature names. ``None`` uses ``feature_names_in_`` when the + transformer was fitted on a DataFrame, else ``x0, x1, ...``. Explicit + names need one entry per input feature and, after a DataFrame fit, + must equal ``feature_names_in_``. Returns ------- feature_names : ndarray of shape (n_features,) The output feature names. + + Raises + ------ + ValueError + If ``input_features`` does not have one entry per input feature, or + differs from ``feature_names_in_``. """ check_is_fitted(self, "n_features_in_") - if input_features is None: - input_features = [f"x{i}" for i in range(self.n_features_in_)] - return np.asarray([f"{name}__missing" for name in input_features], dtype=object) + names = np.asarray(_check_feature_names_in(self, input_features)) + return np.asarray([f"{name}__missing" for name in names], dtype=object) def __sklearn_tags__(self): """Declare that missing values are expected (they are the signal).""" diff --git a/tests/compose/test_inspection.py b/tests/compose/test_inspection.py index 1921e62..0d4f950 100644 --- a/tests/compose/test_inspection.py +++ b/tests/compose/test_inspection.py @@ -1,5 +1,7 @@ """Unit tests for :mod:`pretab.compose.inspection`.""" +import warnings + import numpy as np import pandas as pd import pytest @@ -56,6 +58,23 @@ def test_build_feature_info_does_not_infer_kind_from_cat_in_feature_name(make_co assert feature not in categorical +@pytest.mark.parametrize(("method", "dimension"), [("rbf", 3), ("standardization", 1)]) +def test_build_feature_info_probes_a_step_fitted_on_the_frame_without_warning( + make_config, sample_frame, method, dimension +): + """Without an imputer the representation is fitted on the DataFrame column and + records ``feature_names_in_``; probing it with a bare array warned that X does + not have valid feature names.""" + config = make_config(numerical_method=method, numerical_imputation=None, output_dim=3) + ct = build_column_transformer(config, ["age"], ["city"]).fit(sample_frame) + + with warnings.catch_warnings(): + warnings.simplefilter("error") + numerical, _, _ = build_feature_info(ct, embeddings=False, embedding_dimensions={}) + + assert numerical["age"]["dimension"] == dimension + + def test_build_transformer_summary_has_header_and_rows(): numerical = {"age": {"preprocessing": "imputer -> standardization", "dimension": 1, "categories": None}} categorical = {"city": {"preprocessing": "imputer -> continuous_ordinal", "dimension": 1, "categories": 3}} diff --git a/tests/core/test_representation_spec.py b/tests/core/test_representation_spec.py index 592a098..f444e7b 100644 --- a/tests/core/test_representation_spec.py +++ b/tests/core/test_representation_spec.py @@ -1,8 +1,11 @@ import warnings import numpy as np +import pandas as pd import pytest +from sklearn.base import clone +from pretab import transformers from pretab.core.representation import RepresentationSpec, RepresentationSpecMixin from pretab.transformers import ( BSplineTransformer, @@ -203,6 +206,32 @@ def test_every_transformer_has_representation_spec(): assert hasattr(transformer, "get_representation_spec") +def test_cases_cover_every_exported_representation(): + exported = {getattr(transformers, name) for name in transformers.__all__} + with_spec = {cls for cls in exported if hasattr(cls, "get_representation_spec")} + assert with_spec == {type(case[1]) for case in CASES} + + +@pytest.mark.parametrize( + ("transformer", "X", "y"), + [(case[1], case[2], case[3]) for case in CASES], + ids=CASE_IDS, +) +def test_spec_after_a_dataframe_fit_follows_get_feature_names_out(transformer, X, y): + """A DataFrame fit records ``feature_names_in_``; the default spec passed + ``x0, x1, ...`` to ``get_feature_names_out``, which then raised a ValueError. + The spec now resolves its input names exactly like ``get_feature_names_out``.""" + frame = pd.DataFrame({f"feat_{i}": X[:, i] for i in range(X.shape[1])}) + fitted = _fit(clone(transformer), frame, y) + spec = fitted.get_representation_spec() + + generic = [f"x{i}" for i in range(X.shape[1])] + inputs = [str(name) for name in getattr(fitted, "feature_names_in_", generic)] + assert spec.input_features == tuple(inputs) + assert spec.output_features == tuple(str(name) for name in fitted.get_feature_names_out(inputs)) + assert fitted.get_representation_spec(input_features=inputs) == spec + + def test_periodic_spec_reports_period(): x_month = RNG.uniform(0.0, 12.0, size=(80, 1)) transformer = _fit(PeriodicEncodingTransformer(period=12, harmonics=1), x_month) diff --git a/tests/extension/test_extension_protocol.py b/tests/extension/test_extension_protocol.py index de89ae3..2ca252c 100644 --- a/tests/extension/test_extension_protocol.py +++ b/tests/extension/test_extension_protocol.py @@ -1,6 +1,7 @@ """Tests for the public ``BaseRepresentation`` extension base (P10.1).""" import numpy as np +import pandas as pd import pytest from sklearn.utils.validation import check_is_fitted @@ -62,6 +63,14 @@ def test_representation_spec_reflects_declaration(): assert spec.output_dim == 1 +def test_representation_spec_after_a_dataframe_fit_names_the_columns(): + X = pd.DataFrame({"length": np.linspace(0.0, 1.0, 20), "width": np.linspace(1.0, 2.0, 20)}) + est = _Square().fit(X) + spec = est.get_representation_spec() + assert spec.input_features == ("length", "width") + assert spec.output_features == tuple(est.get_feature_names_out()) + + def test_invalid_scope_rejected(): with pytest.raises(ValueError, match="scope"): diff --git a/tests/integration/test_feature_lineage.py b/tests/integration/test_feature_lineage.py index 26432f4..26bc4e2 100644 --- a/tests/integration/test_feature_lineage.py +++ b/tests/integration/test_feature_lineage.py @@ -127,3 +127,22 @@ def test_lineage_source_features_are_single_input_per_block(mixed_frame): # column traces back to exactly one source feature. assert len(record.source_features) == 1 assert not record.is_interaction + + +@pytest.mark.parametrize(("method", "family"), [("ple", "piecewise_linear"), ("rbf", "rbf"), ("bspline", "bspline")]) +def test_lineage_of_a_representation_behind_the_imputer_indicator(method, family): + """``add_missing_indicator`` feeds the imputer's indicator column to the + representation, so its step has two inputs for a one-column block. The lineage + passed the single column name to ``get_feature_names_out``, which now validates + the number of input features.""" + rng = np.random.default_rng(0) + a = rng.normal(size=120) + a[::7] = np.nan + X = pd.DataFrame({"a": a, "b": rng.normal(size=120)}) + pre = _fit(X, rng.normal(size=120), numerical_method=method, add_missing_indicator=True) + + lineage = pre.get_feature_lineage() + + assert [record.output_feature for record in lineage] == list(pre.get_feature_names_out()) + assert {record.family for record in lineage} == {family} + assert {record.source_features for record in lineage} == {("a",), ("b",)} diff --git a/tests/integration/test_serialization.py b/tests/integration/test_serialization.py index 493000c..0b08bc2 100644 --- a/tests/integration/test_serialization.py +++ b/tests/integration/test_serialization.py @@ -105,6 +105,43 @@ def test_representation_summary_present(frame, target): assert "rbf" in families +@pytest.mark.parametrize("leaf_input", ["imputed", "frame"]) +def test_representation_summary_names_each_block_after_its_column(frame, target, leaf_input): + """Without an imputer or scaler each representation is fitted on its DataFrame + column and records ``feature_names_in_``, so a spec built from ``x0`` raised + and the summary silently came back empty. The entries are named after the + block's column instead of x0.""" + params = {"numerical_method": "rbf", "target_aware": False, "placement_strategy": "quantile"} + if leaf_input == "frame": + params.update(numerical_imputation=None, scaling=None) + p = Preprocessor(**params).fit(frame, target) + entries = {entry["columns"][0]: entry for entry in p.to_spec()["representations"]} + inputs = {column: entry["input_features"] for column, entry in entries.items()} + + assert p.reproducibility_report()["representations"] == {"a": "rbf", "b": "rbf", "c": "ordinal"} + assert inputs == {"a": ["a"], "b": ["b"], "c": ["c"]} + assert entries["a"]["output_features"] == [f"a_rbf{i}" for i in range(entries["a"]["output_dim"])] + + +def test_representation_summary_of_a_leaf_behind_the_imputer_indicator(frame, target): + """``add_missing_indicator`` puts the imputer's built-in indicator in front of + the representation, so a column with missing values has a leaf with two inputs + for a one-column block. Its summary entry names its own inputs instead of + failing on the single column name.""" + X = frame.copy() + X.loc[::7, "a"] = np.nan + p = Preprocessor( + numerical_method="rbf", target_aware=False, placement_strategy="quantile", add_missing_indicator=True + ).fit(X, target) + + entries = {entry["columns"][0]: entry for entry in p.to_spec()["representations"]} + + assert entries["a"]["input_features"] == ["x0", "x1"] + assert entries["b"]["input_features"] == ["b"] + assert p.reproducibility_report()["representations"] == {"a": "rbf", "b": "rbf", "c": "ordinal"} + assert isinstance(p.fingerprint_, str) + + def test_round_trip_preserves_dtype_and_output_format(frame, target): p = Preprocessor( numerical_method="bspline", diff --git a/tests/transformers/test_feature_names_in.py b/tests/transformers/test_feature_names_in.py new file mode 100644 index 0000000..78c8660 --- /dev/null +++ b/tests/transformers/test_feature_names_in.py @@ -0,0 +1,194 @@ +"""Standalone transformers follow scikit-learn's feature-name contract. + +Fitted on a DataFrame they record ``feature_names_in_``, name their outputs after +the columns, reject renamed or reordered columns at ``transform`` and validate +``get_feature_names_out(input_features)``. Before, column names were dropped +(outputs were named ``x0_...``) and a reordered frame was silently accepted. +""" + +import inspect +import warnings + +import numpy as np +import pandas as pd +import pytest +from scipy import sparse +from sklearn.utils.estimator_checks import ( + check_dataframe_column_names_consistency, + check_transformer_get_feature_names_out_pandas, +) + +from pretab import transformers +from pretab.transformers import ( + BSplineTransformer, + ContinuousOrdinalTransformer, + CubicRegressionSplineTransformer, + FourierFeatureTransformer, + ISplineTransformer, + LanguageEmbeddingTransformer, + MissingStateIndicator, + MSplineTransformer, + NaturalCubicSplineTransformer, + NoTransformer, + NumericBinningTransformer, + NystroemFeaturesTransformer, + OneHotFromOrdinalTransformer, + PeriodicEncodingTransformer, + PLETransformer, + PSplineTransformer, + RandomFourierFeaturesTransformer, + RBFExpansionTransformer, + ReLUExpansionTransformer, + SigmoidExpansionTransformer, + TanhExpansionTransformer, + TensorProductSplineTransformer, + ThinPlateSplineTransformer, + ToFloatTransformer, +) + +pytestmark = [ + pytest.mark.filterwarnings("ignore::pretab.exceptions.LeakageWarning"), + pytest.mark.filterwarnings("ignore:OneHotFromOrdinalTransformer is deprecated:DeprecationWarning"), +] + + +class _StubTextModel: + """Stand-in for a SentenceTransformer, so no optional dependency is needed.""" + + def encode(self, values, convert_to_numpy=True): + return np.array([[len(str(value)), 1.0] for value in values], dtype=float) + + +# Every transformer exported from ``pretab.transformers``. Each case names the +# kind of input frame the transformer accepts. +CASES = { + "rbf": (lambda: RBFExpansionTransformer(output_dim=3), "numeric"), + "relu": (lambda: ReLUExpansionTransformer(output_dim=3), "numeric"), + "sigmoid": (lambda: SigmoidExpansionTransformer(output_dim=3), "numeric"), + "tanh": (lambda: TanhExpansionTransformer(output_dim=3), "numeric"), + "ple": (lambda: PLETransformer(output_dim=3, random_state=0), "numeric"), + "binning": (lambda: NumericBinningTransformer(output_dim=3, encode="onehot"), "numeric"), + "periodic": (lambda: PeriodicEncodingTransformer(period=12), "periodic"), + "bspline": (lambda: BSplineTransformer(output_dim=5), "numeric"), + "mspline": (lambda: MSplineTransformer(output_dim=5), "numeric"), + "ispline": (lambda: ISplineTransformer(output_dim=5), "numeric"), + "naturalspline": (lambda: NaturalCubicSplineTransformer(), "numeric"), + "cubicspline": (lambda: CubicRegressionSplineTransformer(), "numeric"), + "pspline": (lambda: PSplineTransformer(), "numeric"), + "tensorspline": (lambda: TensorProductSplineTransformer(), "numeric"), + "thinplate": (lambda: ThinPlateSplineTransformer(), "numeric"), + "fourier": (lambda: FourierFeatureTransformer(), "numeric"), + "random_fourier": (lambda: RandomFourierFeaturesTransformer(random_state=0), "numeric"), + "nystroem": (lambda: NystroemFeaturesTransformer(random_state=0), "numeric"), + "ordinal": (lambda: ContinuousOrdinalTransformer(), "categorical"), + "onehot_from_ordinal": (lambda: OneHotFromOrdinalTransformer(), "codes"), + "language_embedding": (lambda: LanguageEmbeddingTransformer(model=_StubTextModel()), "categorical"), + "missing_state": (lambda: MissingStateIndicator(), "numeric"), + "no_transform": (lambda: NoTransformer(), "numeric"), + "to_float": (lambda: ToFloatTransformer(), "numeric"), +} +IDS = list(CASES) +# scikit-learn's checks feed standardized data, which is outside the periodic +# encoding's required input range [0, period]. +SKLEARN_CHECK_IDS = [case for case in IDS if case != "periodic"] + + +def _frame(kind): + rng = np.random.default_rng(0) + n = 100 + if kind == "numeric": + X = pd.DataFrame({"age": rng.normal(40, 10, n), "income": rng.uniform(1, 5, n)}) + elif kind == "periodic": + X = pd.DataFrame({"age": rng.uniform(0, 12, n), "income": rng.uniform(0, 12, n)}) + elif kind == "categorical": + X = pd.DataFrame({"age": rng.choice(["young", "old"], n), "income": rng.choice(["low", "mid", "high"], n)}) + else: + X = pd.DataFrame({"age": rng.integers(0, 3, n), "income": rng.integers(0, 4, n)}) + return X, rng.normal(size=n) + + +def _fitted(case): + make, kind = CASES[case] + X, y = _frame(kind) + return make().fit(X, y), X, y + + +def test_cases_cover_every_exported_transformer(): + exported = {name for name in transformers.__all__ if inspect.isclass(getattr(transformers, name))} + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + covered = {type(make()).__name__ for make, _ in CASES.values()} + assert covered == exported + + +@pytest.mark.parametrize("case", IDS) +def test_dataframe_fit_records_feature_names_and_uses_them(case): + transformer, _, _ = _fitted(case) + assert list(transformer.feature_names_in_) == ["age", "income"] + names = [str(name) for name in transformer.get_feature_names_out()] + assert all(("age" in name) or ("income" in name) for name in names) + assert not any("x0" in name or "x1" in name for name in names) + + +@pytest.mark.parametrize("case", IDS) +def test_reordered_columns_are_rejected(case): + transformer, X, _ = _fitted(case) + with pytest.raises(ValueError, match="feature names"): + transformer.transform(X[["income", "age"]]) + + +@pytest.mark.parametrize("case", IDS) +def test_renamed_columns_are_rejected(case): + transformer, X, _ = _fitted(case) + with pytest.raises(ValueError, match="feature names"): + transformer.transform(X.rename(columns={"income": "salary"})) + + +@pytest.mark.parametrize("case", IDS) +def test_array_after_a_dataframe_fit_warns_like_scikit_learn(case): + transformer, X, _ = _fitted(case) + with pytest.warns(UserWarning, match="does not have valid feature names"): + transformer.transform(X.to_numpy()) + + +@pytest.mark.parametrize("case", IDS) +def test_input_features_are_validated(case): + transformer, _, _ = _fitted(case) + with pytest.raises(ValueError): + transformer.get_feature_names_out(["age"]) + with pytest.raises(ValueError, match="not equal to feature_names_in_"): + transformer.get_feature_names_out(["a", "b"]) + np.testing.assert_array_equal( + transformer.get_feature_names_out(["age", "income"]), transformer.get_feature_names_out() + ) + + +@pytest.mark.parametrize("case", IDS) +def test_array_fit_keeps_generic_names_and_drops_stale_feature_names(case): + transformer, X, y = _fitted(case) + transformer.fit(X.to_numpy(), y) + assert not hasattr(transformer, "feature_names_in_") + assert "x0" in str(transformer.get_feature_names_out()[0]) + with pytest.raises(ValueError): + transformer.get_feature_names_out(["a"]) + + +@pytest.mark.parametrize("case", SKLEARN_CHECK_IDS) +def test_scikit_learn_feature_name_checks_pass(case): + make, _ = CASES[case] + estimator = make() + name = type(estimator).__name__ + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + check_dataframe_column_names_consistency(name, estimator) + check_transformer_get_feature_names_out_pandas(name, estimator) + + +@pytest.mark.parametrize("cls", [ToFloatTransformer, NoTransformer]) +def test_sparse_input_records_its_width(cls): + """The one-hot encoder hands ``ToFloatTransformer`` a sparse matrix, which + ``np.asarray`` turns into a 0-d object array, so one input feature was + recorded instead of three; the now validated output names then failed.""" + transformer = cls().fit(sparse.csr_matrix(np.eye(3))) + assert transformer.n_features_in_ == 3 + assert list(transformer.get_feature_names_out(["a", "b", "c"])) == ["a", "b", "c"]