scikeras.wrappers.KerasClassifier

class scikeras.wrappers.KerasClassifier(model=None, *, build_fn=None, warm_start=False, random_state=None, optimizer='rmsprop', loss=None, metrics=None, batch_size=None, validation_batch_size=None, verbose=1, callbacks=None, validation_split=0.0, shuffle=True, run_eagerly=False, epochs=1, class_weight=None, **kwargs)[source]

Implementation of the scikit-learn classifier API for Keras.

Below are a list of SciKeras specific parameters. For details on other parameters, please see the see the keras.Model documentation.

Parameters:
modelUnion[None, Callable[…, keras.Model], keras.Model], default None

Used to build the Keras Model. When called, must return a compiled instance of a Keras Model to be used by fit, predict, etc. If None, you must implement _keras_build_fn.

optimizerUnion[str, keras.optimizers.Optimizer, Type[keras.optimizers.Optimizer]], default “rmsprop”

This can be a string for Keras’ built in optimizers, an instance of keras.optimizers.Optimizer or a class inheriting from keras.optimizers.Optimizer. Only strings and classes support parameter routing.

lossUnion[Union[str, keras.losses.Loss, Type[keras.losses.Loss], Callable], None], default None

The loss function to use for training. This can be a string for Keras’ built in losses, an instance of keras.losses.Loss or a class inheriting from keras.losses.Loss . Only strings and classes support parameter routing.

random_stateUnion[int, np.random.RandomState, None], default None

Set the Tensorflow random number generators to a reproducible deterministic state using this seed. Pass an int for reproducible results across multiple function calls.

warm_startbool, default False

If True, subsequent calls to fit will _not_ reset the model parameters but will reset the epoch to zero. If False, subsequent fit calls will reset the entire model. This has no impact on partial_fit, which always trains for a single epoch starting from the current epoch.

batch_sizeUnion[int, None], default None

Number of samples per gradient update. This will be applied to both fit and predict. To specify different numbers, pass fit__batch_size=32 and predict__batch_size=1000 (for example). To auto-adjust the batch size to use all samples, pass batch_size=-1.

class_weightUnion[Dict[Any, float], str, None], default None

Weights associated with classes in the form {class_label: weight}. If not given, all classes are supposed to have weight one. The “balanced” mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as n_samples / (n_classes * np.bincount(y)). Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified.

Attributes:
model_keras.Model

The instantiated and compiled Keras Model. For pre-built models, this will just be a reference to the passed Model instance.

history_Dict[str, List[Any]]

Dictionary of the format {metric_str_name: [epoch_0_data, epoch_1_data, ..., epoch_n_data]}.

initialized_bool

Checks if the estimator is intialized.

target_encoder_sklearn-transformer

Transformer used to pre/post process the target y.

feature_encoder_sklearn-transformer

Transformer used to pre/post process the features/input X.

n_outputs_expected_int

The number of outputs the Keras Model is expected to have, as determined by target_transformer_.

target_type_str

One of:

  • ‘continuous’: y is an array-like of floats that are not all integers, and is 1d or a column vector.

  • ‘continuous-multioutput’: y is a 2d array of floats that are not all integers, and both dimensions are of size > 1.

  • ‘binary’: y contains <= 2 discrete values and is 1d or a column vector.

  • ‘multiclass’: y contains more than two discrete values, is not a sequence of sequences, and is 1d or a column vector.

  • ‘multiclass-multioutput’: y is a 2d array that contains more than two discrete values, is not a sequence of sequences, and both dimensions are of size > 1.

  • ‘multilabel-indicator’: y is a label indicator matrix, an array of two dimensions with at least two columns, and at most 2 unique values.

  • ‘unknown’: y is array-like but none of the above, such as a 3d array, sequence of sequences, or an array of non-sequence objects.

y_shape_Tuple[int]

Shape of the target y that the estimator was fitted on.

y_dtype_np.dtype

Dtype of the target y that the estimator was fitted on.

X_shape_Tuple[int]

Shape of the input X that the estimator was fitted on.

X_dtype_np.dtype

Dtype of the input X that the estimator was fitted on.

n_features_in_int

The number of features 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.

classes_Iterable

The classes seen during fit.

n_classes_int

The number of classes seen during fit.

Parameters:
  • model (None | Callable[[...], Model] | Model) –

  • build_fn (None | Callable[[...], Model] | Model) –

  • warm_start (bool) –

  • random_state (int | RandomState | None) –

  • optimizer (str | Optimizer | Type[Optimizer]) –

  • loss (str | Loss | Type[Loss] | Callable | None) –

  • metrics (List[str | Metric | Type[Metric] | Callable] | None) –

  • batch_size (int | None) –

  • validation_batch_size (int | None) –

  • verbose (int) –

  • callbacks (List[Callback | Type[Callback]] | None) –

  • validation_split (float) –

  • shuffle (bool) –

  • run_eagerly (bool) –

  • epochs (int) –

  • class_weight (Dict[Any, float] | str | None) –

property current_epoch: int

Returns the current training epoch.

Returns:
int

Current training epoch.

property feature_encoder

Retrieve a transformer for features / X.

Metadata will be collected from get_metadata if the transformer implements that method. Override this method to implement a custom data transformer for the features.

Returns:
sklearn transformer

Transformer implementing the sklearn transformer interface.

fit(X, y, sample_weight=None, **kwargs)[source]

Constructs a new classifier with model & fit the model to (X, y).

Parameters:
XUnion[array-like, sparse matrix, dataframe, of shape (n_samples, n_features)

Training samples, where n_samples is the number of samples and n_features is the number of features.

yUnion[array-like, dataframe,, of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.

sample_weightarray-like of shape (n_samples,), default=None

Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight.

**kwargsDict[str, Any]

Extra arguments to route to Model.fit.

Returns:
KerasClassifier

A reference to the instance that can be chain called (est.fit(X,y).transform(X)).

Return type:

KerasClassifier

Warning

Passing estimator parameters as keyword arguments (aka as **kwargs) to fit is not supported by the Scikit-Learn API, and will be removed in a future version of SciKeras. These parameters can also be specified by prefixing fit__ to a parameter at initialization (KerasClassifier(..., fit__batch_size=32, predict__batch_size=1000)) or by using set_params (est.set_params(fit__batch_size=32, predict__batch_size=1000)).

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.

initialize(X, y)[source]

Initialize the model without any fitting. You only need to call this model if you explicitly do not want to do any fitting (for example with a pretrained model). You should _not_ call this right before calling fit, calling fit will do this automatically.

Parameters:
XUnion[array-like, sparse matrix, dataframe, of shape (n_samples, n_features)

Training samples where n_samples is the number of samples and n_features is the number of features.

yUnion[array-like, dataframe,, of shape (n_samples,) or (n_samples, n_outputs), default None

True labels for X.

Returns:
KerasClassifier

A reference to the KerasClassifier instance for chained calling.

Return type:

KerasClassifier

property initialized_: bool

Checks if the estimator is intialized.

Returns:
bool

True if the estimator is initialized (i.e., it can be used for inference or is ready to train), otherwise False.

partial_fit(X, y, classes=None, sample_weight=None, **kwargs)[source]

Fit classifier for a single epoch, preserving the current epoch and all model parameters and state.

Parameters:
XUnion[array-like, sparse matrix, dataframe, of shape (n_samples, n_features)

Training samples, where n_samples is the number of samples and n_features is the number of features.

yUnion[array-like, dataframe,, of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.

classes: ndarray of shape (n_classes,), default=None

Classes across all calls to partial_fit. Can be obtained by via np.unique(y_all), where y_all is the target vector of the entire dataset. This argument is only needed for the first call to partial_fit and can be omitted in the subsequent calls. Note that y doesn’t need to contain all labels in classes. If you do not pass this argument, SciKeras will use classes=np.all(y) with the y passed in the first call.

sample_weightarray-like of shape (n_samples,), default=None

Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight.

**kwargsDict[str, Any]

Extra arguments to route to Model.fit.

Returns:
KerasClassifier

A reference to the instance that can be chain called (ex: instance.fit(X,y).transform(X) )

Return type:

KerasClassifier

predict(X, **kwargs)[source]

Returns predictions for the given test data.

Parameters:
XUnion[array-like, sparse matrix, dataframe, of shape (n_samples, n_features)

Training samples where n_samples is the number of samples and n_features is the number of features.

**kwargsDict[str, Any]

Extra arguments to route to Model.predict.

Returns:
array-like

Predictions, of shape shape (n_samples,) or (n_samples, n_outputs).

Warning

Passing estimator parameters as keyword arguments (aka as **kwargs) to predict is not supported by the Scikit-Learn API, and will be removed in a future version of SciKeras. These parameters can also be specified by prefixing predict__ to a parameter at initialization (BaseWrapper(..., fit__batch_size=32, predict__batch_size=1000)) or by using set_params (est.set_params(fit__batch_size=32, predict__batch_size=1000)).

predict_proba(X, **kwargs)[source]

Returns class probability estimates for the given test data.

Parameters:
XUnion[array-like, sparse matrix, dataframe, of shape (n_samples, n_features)

Training samples, where n_samples is the number of samples and n_features is the number of features.

**kwargsDict[str, Any]

Extra arguments to route to Model.predict.

Returns:
array-like, shape (n_samples, n_outputs)

Class probability estimates. In the case of binary classification, to match the scikit-learn API, SciKeras will return an array of shape (n_samples, 2) (instead of (n_sample, 1) as in Keras).

Warning

Passing estimator parameters as keyword arguments (aka as **kwargs) to predict_proba is not supported by the Scikit-Learn API, and will be removed in a future version of SciKeras. These parameters can also be specified by prefixing predict__ to a parameter at initialization (KerasClassifier(..., fit__batch_size=32, predict__batch_size=1000)) or by using set_params (est.set_params(fit__batch_size=32, predict__batch_size=1000)).

score(X, y, sample_weight=None)[source]

Returns the score on the given test data and labels.

No default scoring function is implemented in BaseWrapper, you must subclass and implement one.

Parameters:
XUnion[array-like, sparse matrix, dataframe, of shape (n_samples, n_features)

Test input samples, where n_samples is the number of samples and n_features is the number of features.

yUnion[array-like, dataframe,, of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.

sample_weightarray-like of shape (n_samples,), default=None

Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight.

Returns:
float

Score for the test data set.

Return type:

float

static scorer(y_true, y_pred, **kwargs)[source]

Scoring function for KerasClassifier.

KerasClassifier uses sklearn_accuracy_score by default. To change this, override this method.

Parameters:
y_truearray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels.

y_predarray-like of shape (n_samples,) or (n_samples, n_outputs)

Predicted labels.

**kwargs: dict

Extra parameters passed to the scorer.

Returns:
float

Score for the test data set.

Return type:

float

set_fit_request(*, sample_weight='$UNCHANGED$')[source]

Request metadata passed to the fit 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 fit 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 fit.

  • 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:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in fit.

Returns:
selfobject

The updated object.

Parameters:
Return type:

KerasClassifier

set_params(**params)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object. This also supports routed parameters, eg: classifier__optimizer__learning_rate.

Parameters:
**paramsdict

Estimator parameters.

Returns:
BaseWrapper

Estimator instance.

Return type:

BaseWrapper

set_partial_fit_request(*, classes='$UNCHANGED$', sample_weight='$UNCHANGED$')[source]

Request metadata passed to the partial_fit 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 partial_fit 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 partial_fit.

  • 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:
classesstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for classes parameter in partial_fit.

sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in partial_fit.

Returns:
selfobject

The updated object.

Parameters:
Return type:

KerasClassifier

set_score_request(*, sample_weight='$UNCHANGED$')[source]

Request metadata passed to the score 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 score 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 score.

  • 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:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns:
selfobject

The updated object.

Parameters:
Return type:

KerasClassifier

property target_encoder

Retrieve a transformer for targets / y.

For KerasClassifier.predict_proba to work, this transformer must accept a return_proba argument in inverse_transform with a default value of False.

Metadata will be collected from get_metadata if the transformer implements that method. Override this method to implement a custom data transformer for the target.

Returns:
sklearn-transformer

Transformer implementing the sklearn transformer interface.