FeatureRenamer#

class scikitplot.feature_selection.FeatureRenamer(rule=None, *, start=0, on_collision='raise', on_missing='raise')[source]#

Rename columns with a plan frozen at fit time.

Parameters:
rulemapping, sequence of str, callable, str or None, default=None

Naming rule (see RenamePlan): {"old": "new"}, a list of new names, callable(name), or a template such as "f{index}". None keeps every name.

startint, default=0

First {index} of a template.

on_collision{“raise”, “suffix”}, default=”raise”

Handling of duplicate new names.

on_missing{“raise”, “ignore”}, default=”raise”

Handling of mapping keys that are not columns.

Attributes:
plan_RenamePlan

The frozen plan.

feature_names_in_ndarray of str

Set only when fitted on a DataFrame, as in scikit-learn.

n_features_in_int

Input count. A NumPy input is named x0, x1, ... for the rule.

Parameters:
  • rule (Any)

  • start (int)

  • on_collision (str)

  • on_missing (str)

Notes

User note. Output is always a DataFrame. A callable rule runs once in fit; transform only applies the stored plan, so test data can never be renamed differently. Define callables with def (not lambda) if the pipeline must be pickled.

Examples

>>> import pandas as pd
>>> from scikitplot.feature_selection import FeatureRenamer
>>> X = pd.DataFrame({"num__age": [1, 2], "cat__city_a": [0, 1]})
>>> FeatureRenamer(lambda n: n.split("__", 1)[1]).fit_transform(X).columns.tolist()
['age', 'city_a']
fit(X, y=None)[source]#

Build and freeze the rename plan from the columns of X.

Parameters:
Xpandas.DataFrame or array-like

Training data.

yignored

Present for API consistency.

Returns:
selfFeatureRenamer

The fitted renamer.

Parameters:
Return type:

FeatureRenamer

fit_transform(X, y=None, **fit_params)[source]#

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters:
Xarray-like of shape (n_samples, n_features)

Input samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).

**fit_paramsdict

Additional fit parameters. Pass only if the estimator accepts additional params in its fit method.

Returns:
X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_feature_lineage(input_features=None)[source]#

Return the one-to-one lineage of this step.

Parameters:
input_featuresarray-like of str or None, default=None

Checked as in get_feature_names_out.

Returns:
FeatureMapping

The identity mapping.

Parameters:

input_features (Any)

Return type:

FeatureMapping

get_feature_names_out(input_features=None)[source]#

Return the new names.

Parameters:
input_featuresarray-like of str or None, default=None

Must equal feature_names_in_ when given.

Returns:
ndarray of str

New names.

Parameters:

input_features (Any)

Return type:

ndarray

get_metadata_routing()[source]#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)[source]#

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

set_output(*, transform=None)[source]#

Set output container.

Refer to the user guide for more details and Introducing the set_output API for an example on how to use the API.

Parameters:
transform{“default”, “pandas”, “polars”}, default=None

Configure output of transform and fit_transform.

  • "default": Default output format of a transformer

  • "pandas": DataFrame output

  • "polars": Polars output

  • None: Transform configuration is unchanged

Added in version 1.4: "polars" option was added.

Returns:
selfestimator instance

Estimator instance.

set_params(**params)[source]#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

transform(X)[source]#

Apply the frozen plan.

Parameters:
Xpandas.DataFrame or array-like

Data with the fitted columns, in the fitted order.

Returns:
pandas.DataFrame

Renamed frame.

Raises:
SchemaMismatchError

If the columns differ from those seen in fit.

Parameters:

X (Any)

Return type:

DataFrame