# tf.keras.wrappers.scikit_learn.KerasClassifier

<br />

|---|---|
| [![](https://www.tensorflow.org/images/tf_logo_32px.png) TensorFlow 1 version](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/keras/wrappers/scikit_learn/KerasClassifier) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/keras/wrappers/scikit_learn.py#L191-L310) |

Implementation of the scikit-learn classifier API for Keras.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.keras.wrappers.scikit_learn.KerasClassifier`](https://www.tensorflow.org/api_docs/python/tf/keras/wrappers/scikit_learn/KerasClassifier)

<br />

    tf.keras.wrappers.scikit_learn.KerasClassifier(
        build_fn=None, **sk_params
    )

## Methods

### `check_params`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/keras/wrappers/scikit_learn.py#L79-L106)

    check_params(
        params
    )

Checks for user typos in `params`.

<br />

<br />

<br />

| Arguments ||
|---|---|
| `params` | dictionary; the parameters to be checked |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | if any member of `params` is not a valid argument. |

<br />

### `filter_sk_params`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/keras/wrappers/scikit_learn.py#L170-L187)

    filter_sk_params(
        fn, override=None
    )

Filters `sk_params` and returns those in `fn`'s arguments.

<br />

<br />

<br />

| Arguments ||
|---|---|
| `fn` | arbitrary function |
| `override` | dictionary, values to override `sk_params` |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `res` | dictionary containing variables in both `sk_params` and `fn`'s arguments. |

<br />

### `fit`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/keras/wrappers/scikit_learn.py#L195-L223)

    fit(
        x, y, **kwargs
    )

Constructs a new model with `build_fn` \& fit the model to `(x, y)`.

<br />

<br />

<br />

| Arguments ||
|---|---|
| `x` | array-like, shape `(n_samples, n_features)` Training samples where `n_samples` is the number of samples and `n_features` is the number of features. |
| `y` | array-like, shape `(n_samples,)` or `(n_samples, n_outputs)` True labels for `x`. |
| `**kwargs` | dictionary arguments Legal arguments are the arguments of [`Sequential.fit`](https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/keras/Model#fit) |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `history` | object details about the training history at each epoch. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | In case of invalid shape for `y` argument. |

<br />

### `get_params`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/keras/wrappers/scikit_learn.py#L108-L119)

    get_params(
        **params
    )

Gets parameters for this estimator.

<br />

<br />

<br />

| Arguments ||
|---|---|
| `**params` | ignored (exists for API compatibility). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| Dictionary of parameter names mapped to their values. ||

<br />

### `predict`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/keras/wrappers/scikit_learn.py#L225-L242)

    predict(
        x, **kwargs
    )

Returns the class predictions for the given test data.

<br />

<br />

<br />

| Arguments ||
|---|---|
| `x` | array-like, shape `(n_samples, n_features)` Test samples where `n_samples` is the number of samples and `n_features` is the number of features. |
| `**kwargs` | dictionary arguments Legal arguments are the arguments of [`Sequential.predict_classes`](https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/keras/Sequential#predict_classes). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `preds` | array-like, shape `(n_samples,)` Class predictions. |

<br />

### `predict_proba`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/keras/wrappers/scikit_learn.py#L244-L270)

    predict_proba(
        x, **kwargs
    )

Returns class probability estimates for the given test data.

<br />

<br />

<br />

| Arguments ||
|---|---|
| `x` | array-like, shape `(n_samples, n_features)` Test samples where `n_samples` is the number of samples and `n_features` is the number of features. |
| `**kwargs` | dictionary arguments Legal arguments are the arguments of [`Sequential.predict_classes`](https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/keras/Sequential#predict_classes). |

<br />

<br />

<br />

<br />

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

<br />

### `score`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/keras/wrappers/scikit_learn.py#L272-L310)

    score(
        x, y, **kwargs
    )

Returns the mean accuracy on the given test data and labels.

<br />

<br />

<br />

| Arguments ||
|---|---|
| `x` | array-like, shape `(n_samples, n_features)` Test samples where `n_samples` is the number of samples and `n_features` is the number of features. |
| `y` | array-like, shape `(n_samples,)` or `(n_samples, n_outputs)` True labels for `x`. |
| `**kwargs` | dictionary arguments Legal arguments are the arguments of [`Sequential.evaluate`](https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/keras/Model#evaluate). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `score` | float Mean accuracy of predictions on `x` wrt. `y`. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If the underlying model isn't configured to compute accuracy. You should pass `metrics=["accuracy"]` to the `.compile()` method of the model. |

<br />

### `set_params`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/keras/wrappers/scikit_learn.py#L121-L132)

    set_params(
        **params
    )

Sets the parameters of this estimator.

<br />

<br />

<br />

| Arguments ||
|---|---|
| `**params` | Dictionary of parameter names mapped to their values. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| self ||

<br />