skkeras.scikit_learn module#
Wrapper for using the Scikit-Learn API with Keras models.
- class skkeras.scikit_learn.BaseWrapper(build_fn=<function _build_fn>, **kwargs)[source]#
Bases:
BaseEstimatorBase class for the Keras scikit-learn wrapper.
- # Arguments
build_fn : callable function or class instance **kwargs : model parameters & fitting parameters
The build_fn should construct, compile and return a Keras model, which will then be used to fit/predict. One of the following three values could be passed to build_fn: 1. A function 2. An instance of a class that implements the __call__ method 3. None. This means you implement a class that inherits from either KerasClassifier or KerasRegressor. The __call__ method of the present class will then be treated as the default build_fn.
kwargs takes both model parameters and fitting parameters. Legal model parameters are the arguments of build_fn. Note that like all other estimators in scikit-learn, build_fn should provide default values for its arguments, so that you could create the estimator without passing any values to kwargs.
kwargs could also accept parameters for calling fit, predict, and score methods (e.g., epochs, batch_size). fitting (predicting) parameters are selected in the following order: 1. Values passed to the dictionary arguments of fit, predict and score methods 2. Values passed to kwargs 3. The default values of the keras.models.Model fit, predict and score methods
When using scikit-learn’s grid_search API, legal tunable parameters are those you could pass to kwargs, including fitting parameters. In other words, you could use grid_search to search for the best batch_size or epochs as well as the model parameters.
- fit(X, y, sample_weight=None, **kwargs)[source]#
Constructs a new model with build_fn & fit the model to (X, y).
- # Arguments
- XInput data. It could be:
A Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs).
A dict mapping input names to the corresponding array/tensors, if the model has named inputs.
None (default) if feeding from framework-native tensors.
- yTarget data. Like the input data X, it could be either Numpy
array(s), framework-native tensor(s), list of Numpy arrays (if the model has multiple outputs) or None (default) if feeding from framework-native tensors. If output layers in the model are named, you can also pass a dictionary mapping output names to Numpy arrays.
- sample_weightNumpy array of weights for the training samples, or
a list of Numpy arrays (if the model has multiple outputs).
- **kwargsdictionary arguments
Legal arguments are the arguments of Model.fit
- # Returns
- historyobject
details about the training history at each epoch.
- predict(X, **kwargs)[source]#
Returns predictions for the given test data.
- # Arguments
- XInput data. It could be:
A Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs).
A dict mapping input names to the corresponding array/tensors, if the model has named inputs.
None (default) if feeding from framework-native tensors.
- **kwargsdictionary arguments
Legal arguments are the arguments of Model.predict.
- # Returns
Numpy array(s) of predictions.
- score(X, y, **kwargs)[source]#
Returns the mean loss on the given test data and labels.
- # Arguments
- XInput data. It could be:
A Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs).
A dict mapping input names to the corresponding array/tensors, if the model has named inputs.
None (default) if feeding from framework-native tensors.
- yTarget data. Like the input data X, it could be either Numpy
array(s), framework-native tensor(s), list of Numpy arrays (if the model has multiple outputs) or None (default) if feeding from framework-native tensors. If output layers in the model are named, you can also pass a dictionary mapping output names to Numpy arrays.
- **kwargsdictionary arguments
Legal arguments are the arguments of Model.evaluate.
- # Returns
Scalar test loss (if the model has a single output and no metrics) or list of scalars (if the model has multiple outputs and/or metrics). The attribute model.metrics_names will give you the display labels for the scalar outputs.
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') BaseWrapper#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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.Added in version 1.3.
- Parameters:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.- Returns:
self – The updated object.
- Return type:
object
- class skkeras.scikit_learn.KerasClassifier(build_fn=<function _build_fn>, **kwargs)[source]#
Bases:
ClassifierMixin,BaseWrapperImplementation of the scikit-learn classifier API for Keras.
- fit(X, y, sample_weight=None, **kwargs)[source]#
Constructs a new model with build_fn & fit the model to (X, y).
- # Arguments
- XInput data. It could be:
A Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs).
A dict mapping input names to the corresponding array/tensors, if the model has named inputs.
None (default) if feeding from framework-native tensors.
- yTarget data. Like the input data X, it could be either Numpy
array(s), framework-native tensor(s), list of Numpy arrays (if the model has multiple outputs) or None (default) if feeding from framework-native tensors. If output layers in the model are named, you can also pass a dictionary mapping output names to Numpy arrays.
- sample_weightNumpy array of weights for the training samples, or
a list of Numpy arrays (if the model has multiple outputs).
- **kwargsdictionary arguments
Legal arguments are the arguments of Model.fit
- # Returns
- historyobject
details about the training history at each epoch.
- predict(X, **kwargs)[source]#
Returns predictions for the given test data.
- # Arguments
- XInput data. It could be:
A Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs).
A dict mapping input names to the corresponding array/tensors, if the model has named inputs.
None (default) if feeding from framework-native tensors.
- **kwargsdictionary arguments
Legal arguments are the arguments of Model.predict.
- # Returns
Numpy array(s) of predictions.
- predict_proba(X, **kwargs)#
Returns predictions for the given test data.
- # Arguments
- XInput data. It could be:
A Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs).
A dict mapping input names to the corresponding array/tensors, if the model has named inputs.
None (default) if feeding from framework-native tensors.
- **kwargsdictionary arguments
Legal arguments are the arguments of Model.predict.
- # Returns
Numpy array(s) of predictions.
- score(X, y, **kwargs)[source]#
Return accuracy on provided data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Test samples.
y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.
sample_weight (array-like of shape (n_samples,), default=None) – Sample weights.
- Returns:
score – Mean accuracy of
self.predict(X)w.r.t. y.- Return type:
float
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') KerasClassifier#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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.Added in version 1.3.
- Parameters:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.- Returns:
self – The updated object.
- Return type:
object
- class skkeras.scikit_learn.KerasRegressor(build_fn=<function _build_fn>, **kwargs)[source]#
Bases:
RegressorMixin,BaseWrapperImplementation of the scikit-learn regressor API for Keras.
- fit(X, y, sample_weight=None, **kwargs)[source]#
Constructs a new model with build_fn & fit the model to (X, y).
- # Arguments
- XInput data. It could be:
A Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs).
A dict mapping input names to the corresponding array/tensors, if the model has named inputs.
None (default) if feeding from framework-native tensors.
- yTarget data. Like the input data X, it could be either Numpy
array(s), framework-native tensor(s), list of Numpy arrays (if the model has multiple outputs) or None (default) if feeding from framework-native tensors. If output layers in the model are named, you can also pass a dictionary mapping output names to Numpy arrays.
- sample_weightNumpy array of weights for the training samples, or
a list of Numpy arrays (if the model has multiple outputs).
- **kwargsdictionary arguments
Legal arguments are the arguments of Model.fit
- # Returns
- historyobject
details about the training history at each epoch.
- predict(X, **kwargs)[source]#
Returns predictions for the given test data.
- # Arguments
- XInput data. It could be:
A Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs).
A dict mapping input names to the corresponding array/tensors, if the model has named inputs.
None (default) if feeding from framework-native tensors.
- **kwargsdictionary arguments
Legal arguments are the arguments of Model.predict.
- # Returns
Numpy array(s) of predictions.
- score(X, y, **kwargs)#
Returns the mean loss on the given test data and labels.
- # Arguments
- XInput data. It could be:
A Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs).
A dict mapping input names to the corresponding array/tensors, if the model has named inputs.
None (default) if feeding from framework-native tensors.
- yTarget data. Like the input data X, it could be either Numpy
array(s), framework-native tensor(s), list of Numpy arrays (if the model has multiple outputs) or None (default) if feeding from framework-native tensors. If output layers in the model are named, you can also pass a dictionary mapping output names to Numpy arrays.
- **kwargsdictionary arguments
Legal arguments are the arguments of Model.evaluate.
- # Returns
Scalar test loss (if the model has a single output and no metrics) or list of scalars (if the model has multiple outputs and/or metrics). The attribute model.metrics_names will give you the display labels for the scalar outputs.
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') KerasRegressor#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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.Added in version 1.3.
- Parameters:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.- Returns:
self – The updated object.
- Return type:
object