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 tf.keras.Model documentation.

Parameters
modelUnion[None, Callable[…, tf.keras.Model], tf.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, tf.keras.optimizers.Optimizer, Type[tf.keras.optimizers.Optimizer]], default “rmsprop”

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

lossUnion[Union[str, tf.keras.losses.Loss, Type[tf.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 tf.keras.losses.Loss or a class inheriting from tf.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_tf.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 (Union[None, Callable[[...], tensorflow.python.keras.engine.training.Model], tensorflow.python.keras.engine.training.Model]) –

  • build_fn (Union[None, Callable[[...], tensorflow.python.keras.engine.training.Model], tensorflow.python.keras.engine.training.Model]) –

  • warm_start (bool) –

  • random_state (Optional[Union[int, numpy.random.mtrand.RandomState]]) –

  • optimizer (Union[str, tensorflow.python.keras.optimizer_v2.optimizer_v2.OptimizerV2, Type[tensorflow.python.keras.optimizer_v2.optimizer_v2.OptimizerV2]]) –

  • loss (Optional[Union[str, tensorflow.python.keras.losses.Loss, Type[tensorflow.python.keras.losses.Loss], Callable]]) –

  • metrics (Optional[List[Union[str, tensorflow.python.keras.metrics.Metric, Type[tensorflow.python.keras.metrics.Metric], Callable]]]) –

  • batch_size (Optional[int]) –

  • validation_batch_size (Optional[int]) –

  • verbose (int) –

  • callbacks (Optional[List[Union[tensorflow.python.keras.callbacks.Callback, Type[tensorflow.python.keras.callbacks.Callback]]]]) –

  • validation_split (float) –

  • shuffle (bool) –

  • run_eagerly (bool) –

  • epochs (int) –

  • class_weight (Optional[Union[Dict[Any, float], str]]) –

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, sparse matrix, 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

scikeras.wrappers.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_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, sparse matrix, 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

scikeras.wrappers.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)[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, sparse matrix, 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.

Returns
KerasClassifier

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

Return type

scikeras.wrappers.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, sparse matrix, 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_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

scikeras.wrappers.BaseWrapper

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.