package sklearn

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type tag = [
  1. | `KBinsDiscretizer
]
type t = [ `BaseEstimator | `KBinsDiscretizer | `Object | `TransformerMixin ] Obj.t
val of_pyobject : Py.Object.t -> t
val to_pyobject : [> tag ] Obj.t -> Py.Object.t
val as_transformer : t -> [ `TransformerMixin ] Obj.t
val as_estimator : t -> [ `BaseEstimator ] Obj.t
val create : ?n_bins:[ `Arr of [> `ArrayLike ] Np.Obj.t | `I of int ] -> ?encode:[ `Onehot | `Onehot_dense | `Ordinal ] -> ?strategy:[ `Uniform | `Quantile | `Kmeans ] -> unit -> t

Bin continuous data into intervals.

Read more in the :ref:`User Guide <preprocessing_discretization>`.

.. versionadded:: 0.20

Parameters ---------- n_bins : int or array-like, shape (n_features,) (default=5) The number of bins to produce. Raises ValueError if ``n_bins < 2``.

encode : 'onehot', 'onehot-dense', 'ordinal', (default='onehot') Method used to encode the transformed result.

onehot Encode the transformed result with one-hot encoding and return a sparse matrix. Ignored features are always stacked to the right. onehot-dense Encode the transformed result with one-hot encoding and return a dense array. Ignored features are always stacked to the right. ordinal Return the bin identifier encoded as an integer value.

strategy : 'uniform', 'quantile', 'kmeans', (default='quantile') Strategy used to define the widths of the bins.

uniform All bins in each feature have identical widths. quantile All bins in each feature have the same number of points. kmeans Values in each bin have the same nearest center of a 1D k-means cluster.

Attributes ---------- n_bins_ : int array, shape (n_features,) Number of bins per feature. Bins whose width are too small (i.e., <= 1e-8) are removed with a warning.

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

See Also -------- sklearn.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::

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

You can combine ``KBinsDiscretizer`` with :class:`sklearn.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., :class:`sklearn.feature_selection.VarianceThreshold`).

Examples -------- >>> X = [-2, 1, -4, -1], ... [-1, 2, -3, -0.5], ... [ 0, 3, -2, 0.5], ... [ 1, 4, -1, 2] >>> est = KBinsDiscretizer(n_bins=3, encode='ordinal', strategy='uniform') >>> est.fit(X) KBinsDiscretizer(...) >>> Xt = est.transform(X) >>> Xt # doctest: +SKIP array([ 0., 0., 0., 0.], [ 1., 1., 1., 0.], [ 2., 2., 2., 1.], [ 2., 2., 2., 2.])

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.

>>> 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])

val fit : ?y:Py.Object.t -> x:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> t

Fit the estimator.

Parameters ---------- X : numeric array-like, shape (n_samples, n_features) Data to be discretized.

y : None Ignored. This parameter exists only for compatibility with :class:`sklearn.pipeline.Pipeline`.

Returns ------- self

val fit_transform : ?y:[> `ArrayLike ] Np.Obj.t -> ?fit_params:(string * Py.Object.t) list -> x:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> [> `ArrayLike ] Np.Obj.t

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 : array-like, sparse matrix, dataframe of shape (n_samples, n_features)

y : ndarray of shape (n_samples,), default=None Target values.

**fit_params : dict Additional fit parameters.

Returns ------- X_new : ndarray array of shape (n_samples, n_features_new) Transformed array.

val get_params : ?deep:bool -> [> tag ] Obj.t -> Dict.t

Get parameters for this estimator.

Parameters ---------- deep : bool, default=True If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns ------- params : mapping of string to any Parameter names mapped to their values.

val inverse_transform : xt:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> [> `ArrayLike ] Np.Obj.t

Transform discretized data back to original feature space.

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

Parameters ---------- Xt : numeric array-like, shape (n_sample, n_features) Transformed data in the binned space.

Returns ------- Xinv : numeric array-like Data in the original feature space.

val set_params : ?params:(string * Py.Object.t) list -> [> tag ] Obj.t -> t

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.

Parameters ---------- **params : dict Estimator parameters.

Returns ------- self : object Estimator instance.

val transform : x:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> [> `ArrayLike ] Np.Obj.t

Discretize the data.

Parameters ---------- X : numeric array-like, shape (n_samples, n_features) Data to be discretized.

Returns ------- Xt : numeric array-like or sparse matrix Data in the binned space.

val n_bins_ : t -> [> `ArrayLike ] Np.Obj.t

Attribute n_bins_: get value or raise Not_found if None.

val n_bins_opt : t -> [> `ArrayLike ] Np.Obj.t option

Attribute n_bins_: get value as an option.

val bin_edges_ : t -> Py.Object.t

Attribute bin_edges_: get value or raise Not_found if None.

val bin_edges_opt : t -> Py.Object.t option

Attribute bin_edges_: get value as an option.

val to_string : t -> string

Print the object to a human-readable representation.

val show : t -> string

Print the object to a human-readable representation.

val pp : Stdlib.Format.formatter -> t -> unit

Pretty-print the object to a formatter.