scikeras.utils.transformers

Classes

ClassifierLabelEncoder([loss, categories])

Default target transformer for KerasClassifier.

RegressorTargetEncoder()

Default target transformer for KerasRegressor.

TargetReshaper()

Convert 1D targets to 2D and back.

class scikeras.utils.transformers.ClassifierLabelEncoder(loss=None, categories='auto')[source]

Default target transformer for KerasClassifier.

Parameters:
lossUnion[None, str, Loss], default None

Keras Model’s loss function. Used to automatically one-hot encode the target if the loss function is categorical crossentropy.

categoriesUnion[str, List[np.ndarray]], default “auto”

All of the categories present in the target for the entire dataset. “auto” will infer the categories from the data passed to fit.

Attributes:
classes_Iterable

The classes seen during fit.

n_classes_int

The number of classes seen during fit.

n_outputs_int

Dimensions of y that the transformer was trained on.

n_outputs_expected_int

Number of outputs the Keras Model is expected to have.

Parameters:
fit(y)[source]

Fit the estimator to the target y.

For all targets, this transforms classes into ordinal numbers. If the loss function is categorical_crossentropy, the target will be one-hot encoded.

Parameters:
ynp.ndarray

The target data to be transformed.

Returns:
ClassifierLabelEncoder

A reference to the current instance of ClassifierLabelEncoder.

Parameters:

y (ndarray) –

Return type:

ClassifierLabelEncoder

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.

Returns:
X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_metadata()[source]

Returns a dictionary of meta-parameters generated when this transfromer was fitted.

Used by SciKeras to bind these parameters to the SciKeras estimator itself and make them available as inputs to the Keras model.

Returns:
Dict[str, Any]

Dictionary of meta-parameters generated when this transfromer was fitted.

Return type:

Dict[str, Any]

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.

inverse_transform(y, return_proba=False)[source]

Restore the data types, shape and classes of the input y to the output of the Keras Model.

Parameters:
ynp.ndarray

Raw probability predictions from the Keras Model.

return_probabool, default False

If True, return the prediction probabilites themselves. If False, return the class predictions.

Returns:
np.ndarray

Class predictions (of the same shape as the y to fit/transform), or class prediction probabilities.

Parameters:
Return type:

ndarray

set_inverse_transform_request(*, return_proba='$UNCHANGED$')[source]

Request metadata passed to the inverse_transform method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to inverse_transform if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to inverse_transform.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
return_probastr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for return_proba parameter in inverse_transform.

Returns:
selfobject

The updated object.

Parameters:
Return type:

ClassifierLabelEncoder

set_output(*, transform=None)[source]

Set output container.

See Introducing the set_output API for an example on how to use the API.

Parameters:
transform{“default”, “pandas”}, 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

New 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(y)[source]

Transform the target y to the format expected by the Keras Model.

If the loss function is categorical_crossentropy, the target will be one-hot encoded. For other types of target, this transforms classes into ordinal numbers.

Returns:
np.ndarray

Transformed target.

Parameters:

y (ndarray) –

Return type:

ndarray

class scikeras.utils.transformers.RegressorTargetEncoder[source]

Default target transformer for KerasRegressor.

Attributes:
n_outputs_int

Dimensions of y that the transformer was trained on.

n_outputs_expected_int

Number of outputs the Keras Model is expected to have.

fit(y)[source]

Fit the transformer to the target y.

For RegressorTargetEncoder, this just records the dimensions of y as the expected number of outputs and saves the dtype.

Returns:
RegressorTargetEncoder

A reference to the current instance of RegressorTargetEncoder.

Parameters:

y (ndarray) –

Return type:

RegressorTargetEncoder

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.

Returns:
X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_metadata()[source]

Returns a dictionary of meta-parameters generated when this transfromer was fitted.

Used by SciKeras to bind these parameters to the SciKeras estimator itself and make them available as inputs to the Keras model.

Returns:
Dict[str, Any]

Dictionary of meta-parameters generated when this transfromer was fitted.

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.

inverse_transform(y)[source]

Restore the data types and shape of the input y to the output of the Keras Model.

Parameters:
ynp.ndarray

Raw predictions from the Keras Model.

Returns:
np.ndarray

Keras Model predictions cast to the dtype and shape of the input targets.

Parameters:

y (ndarray) –

Return type:

ndarray

set_output(*, transform=None)[source]

Set output container.

See Introducing the set_output API for an example on how to use the API.

Parameters:
transform{“default”, “pandas”}, 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

New 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(y)[source]

Transform the target y to the format expected by the Keras Model.

For RegressorTargetEncoder, this simply checks that the shape passed to fit matches the shape passed to transform.

Returns:
np.ndarray

Untouched input y.

Parameters:

y (ndarray) –

Return type:

ndarray

class scikeras.utils.transformers.TargetReshaper[source]

Convert 1D targets to 2D and back.

For use in pipelines with transformers that only accept 2D inputs, like OneHotEncoder and OrdinalEncoder.

Attributes:
ndim_int

Dimensions of y that the transformer was trained on.

fit(y)[source]

Fit the transformer to a target y.

Returns:
TargetReshaper

A reference to the current instance of TargetReshaper.

Parameters:

y (ndarray) –

Return type:

TargetReshaper

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.

Returns:
X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

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.

inverse_transform(y)[source]

Revert the transformation of transform.

Parameters:
ynp.ndarray

Transformed numpy array.

Returns:
np.ndarray

If the transformer was fit to a 1D numpy array, and a 2D numpy array with a singleton second dimension is passed, it will be squeezed back to 1D. Otherwise, it will eb left untouched.

Parameters:

y (ndarray) –

Return type:

ndarray

set_output(*, transform=None)[source]

Set output container.

See Introducing the set_output API for an example on how to use the API.

Parameters:
transform{“default”, “pandas”}, 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

New 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(y)[source]

Makes 1D y 2D.

Parameters:
ynp.ndarray

Target y to be transformed.

Returns:
np.ndarray

A numpy array, of dimension at least 2.

Parameters:

y (ndarray) –

Return type:

ndarray