diff --git a/docs/representations/spline_expansions.md b/docs/representations/spline_expansions.md index 7db8578..ced5a12 100644 --- a/docs/representations/spline_expansions.md +++ b/docs/representations/spline_expansions.md @@ -23,8 +23,9 @@ knot positions by `placement_strategy` (see Without explicit knots, `output_dim` is the number of output columns for a univariate spline **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`. For B/M/I splines with explicit `knot_locations`, a feature +`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). +For B/M/I splines with explicit `knot_locations`, a feature with `K` retained unique interior knots instead gets `K + degree + 1` non-bias columns; different feature ranges can therefore produce different widths. ``` diff --git a/docs/tutorials/custom_representation.md b/docs/tutorials/custom_representation.md index e791aac..d7b65b5 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 f7abc20..f6ca0cf 100644 --- a/pretab/compose/inspection.py +++ b/pretab/compose/inspection.py @@ -11,6 +11,7 @@ from typing import Any, cast import numpy as np +import pandas as pd from sklearn.base import clone from sklearn.pipeline import FeatureUnion, Pipeline @@ -184,6 +185,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 representation_leaf(transformer): """Return the last step of a fitted per-column block's representation. @@ -250,7 +264,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] @@ -298,7 +312,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] @@ -384,7 +398,13 @@ def _locate_block_representation(pipeline, columns): for index in reversed(range(len(steps))): step_name, transformer = steps[index] if hasattr(transformer, "get_representation_spec"): - spec = transformer.get_representation_spec(input_features=None if columns is None else 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. + input_features = None + if columns is not None and getattr(transformer, "n_features_in_", len(columns)) == len(columns): + input_features = list(columns) + spec = transformer.get_representation_spec(input_features=input_features) return index, (spec.family, spec.component_kind, spec.uses_target, spec.is_interaction) registered = TRANSFORMER_REGISTRY.get(step_name) if ( diff --git a/pretab/compose/serialize.py b/pretab/compose/serialize.py index 7418224..b39d003 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 .inspection import representation_leaf from .registry import TransformerSpec @@ -228,12 +228,17 @@ def _library_versions() -> dict: def _representation_summary(preprocessor) -> list: - """Best-effort declarative per-representation summary (family/columns/locations). - - One entry per block whose representation ends in a PreTab transformer. For a - missing-indicator / separate-state block that is the last step of its - representation branch (see :func:`~pretab.compose.inspection.representation_leaf`), - not the union itself. + """Declarative per-representation summary (family/columns/locations). + + One entry per block whose representation ends in a PreTab transformer, built from its + :class:`RepresentationSpec` with the block's column names as input features + (as the feature lineage does). For a missing-indicator / separate-state block, + this is the last step of its representation branch (see + :func:`~pretab.compose.inspection.representation_leaf`), not the union itself. + Blocks whose representation ends 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) @@ -246,11 +251,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 aa902c6..8fb37b3 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 @@ -93,17 +97,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 9c86645..34d1824 100644 --- a/pretab/core/validation.py +++ b/pretab/core/validation.py @@ -8,15 +8,35 @@ import sys import warnings -from typing import Literal +from typing import Literal, cast import numpy as np import pandas as pd -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, IncompatibleParamsError, PretabDataError +from ..exceptions import DataWarning, IncompatibleParamsError, PretabDataError, invalid_param_error -__all__ = ["is_polars_frame", "polars_to_pandas", "single_target", "validate_2d_allow_nan"] +__all__ = ["is_polars_frame", "polars_to_pandas", "resolve_input_features", "single_target", "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 single_target(y, estimator: str, *, fix: str | None = None) -> np.ndarray: @@ -112,9 +132,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. @@ -124,6 +146,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 83f6934..e3a9226 100644 --- a/pretab/encoding/categorical/ordinal.py +++ b/pretab/encoding/categorical/ordinal.py @@ -1,9 +1,10 @@ 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_is_fitted from ...core.representation import RepresentationSpecMixin +from ...core.validation import resolve_input_features from ...exceptions import PretabDataError @@ -24,6 +25,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 ----- @@ -58,6 +65,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: @@ -89,6 +97,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: @@ -108,19 +117,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 5c899ca..c09a07c 100644 --- a/pretab/encoding/numerical/ple.py +++ b/pretab/encoding/numerical/ple.py @@ -10,13 +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, validate_task from ...core.representation import RepresentationSpecMixin from ...core.supervised import warn_target_leakage -from ...core.validation import single_target +from ...core.validation import resolve_input_features, single_target from ...exceptions import ( IncompatibleParamsError, InvalidParamError, @@ -77,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 @@ -164,6 +168,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 @@ -237,6 +242,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 @@ -325,17 +331,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 a3857c7..d5540bf 100644 --- a/pretab/expansion/spline/base.py +++ b/pretab/expansion/spline/base.py @@ -139,6 +139,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 ff6d3e5..1ce7fde 100644 --- a/pretab/expansion/spline/cubic_regression.py +++ b/pretab/expansion/spline/cubic_regression.py @@ -100,6 +100,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 eed7767..6e83586 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, extrapolate_bspline_rows 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 @@ -103,6 +104,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). @@ -249,10 +255,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 ed7db65..333b3fc 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 593b05c..4462b00 100644 --- a/pretab/expansion/spline/natural_cubic.py +++ b/pretab/expansion/spline/natural_cubic.py @@ -103,6 +103,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 0d71548..6ba7b2c 100644 --- a/pretab/expansion/spline/p_spline.py +++ b/pretab/expansion/spline/p_spline.py @@ -97,6 +97,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 017d545..70b250d 100644 --- a/pretab/preprocessing/missing.py +++ b/pretab/preprocessing/missing.py @@ -2,7 +2,13 @@ import pandas as pd from scipy import sparse as sp from sklearn.base import BaseEstimator, OneToOneFeatureMixin, TransformerMixin -from sklearn.utils.validation import check_array, check_is_fitted, validate_data +from sklearn.utils.validation import ( + _check_feature_names, + _check_feature_names_in, + check_array, + check_is_fitted, + validate_data, +) class _CategoricalMissingNormalizer(OneToOneFeatureMixin, TransformerMixin, BaseEstimator): @@ -48,6 +54,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 -------- @@ -75,6 +85,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 @@ -93,6 +104,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) @@ -103,19 +115,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 c1933f3..a76ee31 100644 --- a/tests/compose/test_inspection.py +++ b/tests/compose/test_inspection.py @@ -60,6 +60,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_feature_info_reports_a_zero_width_block(): # scikit-learn's imputer default drops a column without observed values, so # the encoder after it receives no column at all. diff --git a/tests/core/test_cross_fitted.py b/tests/core/test_cross_fitted.py index 85b8087..2d750ab 100644 --- a/tests/core/test_cross_fitted.py +++ b/tests/core/test_cross_fitted.py @@ -885,7 +885,7 @@ def test_cross_fitting_a_discrete_feature_keeps_a_fixed_width(seed): assert out.shape == (200, 10) -@pytest.mark.parametrize("wrapped_kind", ["preprocessor", "column_transformer", "pipeline"]) +@pytest.mark.parametrize("wrapped_kind", ["ple", "preprocessor", "column_transformer", "pipeline"]) def test_dataframe_representation_spec_defaults_to_fitted_names(mixed_frame, wrapped_kind): from sklearn.compose import ColumnTransformer from sklearn.pipeline import Pipeline @@ -893,7 +893,10 @@ def test_dataframe_representation_spec_defaults_to_fitted_names(mixed_frame, wra from pretab import Preprocessor X, y = mixed_frame - if wrapped_kind == "preprocessor": + if wrapped_kind == "ple": + X = X[["num"]] + wrapped = PLETransformer(output_dim=4) + elif wrapped_kind == "preprocessor": wrapped = Preprocessor(output_dim=4, random_state=0) else: wrapped = ColumnTransformer([("ple", PLETransformer(output_dim=4), ["num"])]) @@ -902,13 +905,13 @@ def test_dataframe_representation_spec_defaults_to_fitted_names(mixed_frame, wra cross_fitted = CrossFittedTransformer(wrapped, n_folds=3, random_state=0).fit(X, y) spec = cross_fitted.get_representation_spec() - assert spec.input_features == ("num", "city") + assert spec.input_features == tuple(X.columns) assert spec.output_features == tuple(cross_fitted.get_feature_names_out()) assert spec.cross_fitted is True assert spec.n_folds == 3 assert spec.uses_target is True assert spec == type(spec).from_dict(spec.to_dict()) - assert cross_fitted.get_representation_spec(["num", "city"]) == spec + assert cross_fitted.get_representation_spec(list(X.columns)) == spec def test_dataframe_fold_rows_keep_index_dtypes_and_target_alignment(mixed_frame, monkeypatch): 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_column_labels.py b/tests/integration/test_column_labels.py index 6e0e88d..00a1ad7 100644 --- a/tests/integration/test_column_labels.py +++ b/tests/integration/test_column_labels.py @@ -6,6 +6,7 @@ scikit-learn's ColumnTransformer, silently swapping columns. """ +import warnings from typing import cast import numpy as np @@ -16,6 +17,7 @@ from sklearn.pipeline import make_pipeline from pretab import Preprocessor +from pretab.core.supervised import CrossFittedTransformer from pretab.exceptions import PretabDataError @@ -92,6 +94,48 @@ def test_preprocessor_chains_after_a_pandas_column_transformer(): assert np.asarray(pipe.transform(X)).shape == (50, 2) +@pytest.mark.parametrize("method", ["ple", "rbf", "bspline"]) +@pytest.mark.parametrize("numerical_imputation", [None, "median"]) +def test_named_representation_paths_align_columns_without_feature_name_warnings(method, numerical_imputation): + rng = np.random.default_rng(0) + X = pd.DataFrame( + { + "age": rng.normal(40, 10, 120), + "income": rng.normal(50, 10, 120), + "city": rng.choice(["a", "b", "c"], 120), + } + ) + y = X["age"].to_numpy() + rng.normal(size=len(X)) + if numerical_imputation is not None: + X.loc[::7, "age"] = np.nan + reordered = X[["city", "income", "age"]] + pre = Preprocessor( + numerical_method=method, + categorical_method="one-hot", + output_dim=5, + scaling="none", + numerical_imputation=numerical_imputation, + add_missing_indicator=True, + random_state=0, + ) + + with warnings.catch_warnings(): + warnings.simplefilter("error", UserWarning) + pre.fit(X, y) + expected = np.asarray(pre.transform(X, return_array=True)) + np.testing.assert_allclose(np.asarray(pre.transform(reordered, return_array=True)), expected) + numerical, categorical, _ = pre.get_feature_info(verbose=False) + dimensions = [info["dimension"] for info in [*numerical.values(), *categorical.values()]] + assert sum(dimensions) == expected.shape[1] + assert [record.output_feature for record in pre.get_feature_lineage()] == list(pre.get_feature_names_out()) + restored = Preprocessor.from_spec(pre.to_spec()) + np.testing.assert_allclose(np.asarray(restored.transform(reordered, return_array=True)), expected) + cross_fitted = CrossFittedTransformer(pre, n_folds=3, random_state=0) + assert cross_fitted.fit_transform(X, y).shape == expected.shape + assert cross_fitted.get_representation_spec().input_features == tuple(X.columns) + np.testing.assert_allclose(cross_fitted.transform(reordered), expected) + + def test_integer_labels_route_each_column_to_its_own_block(int_labelled): out = _blocks(_passthrough().fit_transform(int_labelled)) np.testing.assert_array_equal(out["num_1"].ravel(), int_labelled[1].to_numpy()) diff --git a/tests/integration/test_feature_lineage.py b/tests/integration/test_feature_lineage.py index 26432f4..307d24b 100644 --- a/tests/integration/test_feature_lineage.py +++ b/tests/integration/test_feature_lineage.py @@ -127,3 +127,19 @@ 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_keeps_the_missing_indicator_separate_from_the_representation(method, family): + """The indicator keeps its own lineage without becoming a representation input.""" + 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, "missing_state"} + 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 eb9e46b..9f07391 100644 --- a/tests/integration/test_serialization.py +++ b/tests/integration/test_serialization.py @@ -105,6 +105,40 @@ 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_names_the_representation_beside_the_missing_indicator(frame, target): + """The summary names the representation's input, not the separate indicator.""" + 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"] == ["a"] + 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"]