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 (numpy.ndarray) –

Return type

scikeras.utils.transformers.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_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

numpy.ndarray

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 (numpy.ndarray) –

Return type

numpy.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 (numpy.ndarray) –

Return type

scikeras.utils.transformers.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_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 (numpy.ndarray) –

Return type

numpy.ndarray

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 (numpy.ndarray) –

Return type

numpy.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 (numpy.ndarray) –

Return type

scikeras.utils.transformers.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_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 (numpy.ndarray) –

Return type

numpy.ndarray

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.

static 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 (numpy.ndarray) –

Return type

numpy.ndarray