# KBinsDiscretizer

### *class* cuml.preprocessing.KBinsDiscretizer(\*args, \*\*kwargs)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_discretization.py#L39)

Bin continuous data into intervals.

* **Parameters:**
  **n_bins**
  : The number of bins to produce. Raises ValueError if `n_bins < 2`.

  **encode**
  : Method used to encode the transformed result.
    <br/>
    onehot
    : Encode the transformed result with one-hot encoding
      and return a sparse matrix. Ignored features are always
      stacked to the right.
    <br/>
    onehot-dense
    : Encode the transformed result with one-hot encoding
      and return a dense array. Ignored features are always
      stacked to the right.
    <br/>
    ordinal
    : Return the bin identifier encoded as an integer value.

  **strategy**
  : Strategy used to define the widths of the bins.
    <br/>
    uniform
    : All bins in each feature have identical widths.
    <br/>
    quantile
    : All bins in each feature have the same number of points.
    <br/>
    kmeans
    : Values in each bin have the same nearest center of a 1D k-means
      cluster.
* **Attributes:**
  **n_bins_**
  : Number of bins per feature. Bins whose width are too small
    (i.e., <= 1e-8) are removed with a warning.

  **bin_edges_**
  : The edges of each bin. Contain arrays of varying shapes `(n_bins_, )`
    Ignored features will have empty arrays.

### Methods

| [`fit`](#cuml.preprocessing.KBinsDiscretizer.fit)(X[, y])                             | Fit the estimator.                                         |
|------------------------------------------------------------------------------------------|------------------------------------------------------------|
| [`get_feature_names_out`](#cuml.preprocessing.KBinsDiscretizer.get_feature_names_out)([input_features]) | Get output feature names for transformation.               |
| [`inverse_transform`](#cuml.preprocessing.KBinsDiscretizer.inverse_transform)(Xt)                   | Transform discretized data back to original feature space. |
| [`transform`](#cuml.preprocessing.KBinsDiscretizer.transform)(X)                            | Discretize the data.                                       |

#### SEE ALSO
[`cuml.preprocessing.Binarizer`](../cuml.preprocessing.Binarizer/index.html.md#cuml.preprocessing.Binarizer)
: Class used to bin values as `0` or `1` based on a parameter `threshold`.

### Notes

In bin edges for feature `i`, the first and last values are used only for
`inverse_transform`. During transform, bin edges are extended to:

```default
np.concatenate([-np.inf, bin_edges_[i][1:-1], np.inf])
```

You can combine `KBinsDiscretizer` with
[`cuml.compose.ColumnTransformer`](../cuml.compose.ColumnTransformer/index.html.md#cuml.compose.ColumnTransformer) if you only want to preprocess
part of the features.

`KBinsDiscretizer` might produce constant features (e.g., when
`encode = 'onehot'` and certain bins do not contain any data).
These features can be removed with feature selection algorithms
(e.g., [`sklearn.feature_selection.VarianceThreshold`](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.VarianceThreshold.html#sklearn.feature_selection.VarianceThreshold)).

### Examples

```pycon
>>> from cuml.preprocessing import KBinsDiscretizer
>>> import cupy as cp
>>> X = [[-2, 1, -4,   -1],
...      [-1, 2, -3, -0.5],
...      [ 0, 3, -2,  0.5],
...      [ 1, 4, -1,    2]]
>>> X = cp.array(X)
>>> est = KBinsDiscretizer(n_bins=3, encode='ordinal', strategy='uniform')
>>> est.fit(X)
KBinsDiscretizer(...)
>>> Xt = est.transform(X)
>>> Xt
array([[0, 0, 0, 0],
       [1, 1, 1, 0],
       [2, 2, 2, 1],
       [2, 2, 2, 2]], dtype=int32)
```

Sometimes it may be useful to convert the data back into the original
feature space. The `inverse_transform` function converts the binned
data into the original feature space. Each value will be equal to the mean
of the two bin edges.

```pycon
>>> est.bin_edges_[0]
array([-2., -1.,  0.,  1.])
>>> est.inverse_transform(Xt)
array([[-1.5,  1.5, -3.5, -0.5],
       [-0.5,  2.5, -2.5, -0.5],
       [ 0.5,  3.5, -1.5,  0.5],
       [ 0.5,  3.5, -1.5,  1.5]])
```

<!-- !! processed by numpydoc !! -->

#### fit(X, y=None) → [KBinsDiscretizer](#cuml.preprocessing.KBinsDiscretizer)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_discretization.py#L149)

Fit the estimator.

* **Parameters:**
  **X**
  : Data to be discretized.

  **y**
  : Ignored. This parameter exists only for compatibility with
    [`sklearn.pipeline.Pipeline`](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline).
* **Returns:**
  self

<!-- !! processed by numpydoc !! -->

#### fit_transform(X, y=None, \*\*fit_params)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/_thirdparty/sklearn/utils/skl_dependencies.py#L42)

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:**
  **X**

  **y**
  : Target values.

  **\*\*fit_params**
  : Additional fit parameters.
* **Returns:**
  **X_new**
  : Transformed array.

<!-- !! processed by numpydoc !! -->

#### get_feature_names_out(input_features=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_discretization.py#L371)

Get output feature names for transformation.

* **Parameters:**
  **input_features**
  : Input feature names.
* **Returns:**
  **feature_names_out**
  : Transformed feature names.

<!-- !! processed by numpydoc !! -->

#### get_params(deep=True)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/base.py#L191)

Returns a dict of all params owned by this class. If the child class
has appropriately overridden the `_get_param_names` method and does not
need anything other than what is there in this method, then it doesn’t
have to override this method

<!-- !! processed by numpydoc !! -->

#### inverse_transform(Xt)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_discretization.py#L331)

Transform discretized data back to original feature space.

Note that this function does not regenerate the original data
due to discretization rounding.

* **Parameters:**
  **Xt**
  : Transformed data in the binned space.
* **Returns:**
  **Xinv**
  : Data in the original feature space.

<!-- !! processed by numpydoc !! -->

#### set_params(\*\*params)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/base.py#L200)

Accepts a dict of params and updates the corresponding ones owned by
this class. If the child class has appropriately overridden the
`_get_param_names` method and does not need anything other than what is,
there in this method, then it doesn’t have to override this method

<!-- !! processed by numpydoc !! -->

#### transform(X)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_discretization.py#L300)

Discretize the data.

* **Parameters:**
  **X**
  : Data to be discretized.
* **Returns:**
  **Xt**
  : Data in the binned space.

<!-- !! processed by numpydoc !! -->
