Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

KBinsDiscretizer

KBinsDiscretizer converts continuous features into intervals using uniform, quantile, or one-dimensional k-means boundaries. It can emit sparse one-hot, dense one-hot, or ordinal integer representations.

How it works

KBinsDiscretizer learns boundaries that divide each continuous feature into a chosen number of intervals. During transformation it represents those intervals as sparse one-hot vectors, a dense one-hot array, or ordinal integer identifiers, using one of three binning strategies.

Syntax

class sklearn.preprocessing.KBinsDiscretizer(n_bins=5, *, encode='onehot', strategy='quantile', quantile_method='averaged_inverted_cdf', dtype=None, subsample=200000, random_state=None)

Parameters

NameTypeDescription
n_binsint or array-like of shape (n_features,), default=5Number of intervals: one integer for all features or an array with one count per feature; each value must be at least 2.
encode{'onehot', 'onehot-dense', 'ordinal'}, default='onehot'Output representation: 'onehot' sparse matrix, 'onehot-dense' dense array, or 'ordinal' integer bin identifiers.
strategy{'uniform', 'quantile', 'kmeans'}, default='quantile'Boundary strategy: equal-width, quantile-based, or one-dimensional k-means.
quantile_methodstr, default='averaged_inverted_cdf'Quantile computation method used when strategy='quantile'.
dtypedtype, default=NoneOutput data type; when None, the estimator chooses an appropriate type.
subsampleint or None, default=200000Maximum number of samples used to fit each feature; None disables subsampling.
random_stateint, RandomState instance or None, default=NoneControls randomness when subsampling is used.

Returns

fit returns the fitted estimator; transform returns a sparse matrix, dense array, or ordinal array according to encode.

Key points

  • The transformer changes continuous feature values into interval-based representations.
  • The encode option chooses sparse one-hot, dense one-hot, or ordinal integer output.
  • The uniform strategy partitions each feature into equal-width bins.
  • The quantile strategy aims for similarly populated bins in every feature.
  • The kmeans strategy assigns values according to nearest one-dimensional cluster centers.
  • Use n_bins as one shared count or provide a per-feature array of counts.

Common mistakes

  • Every feature must use at least two bins; smaller values raise ValueError.
  • Features whose effective bin count is one are not encoded normally and appear at the right side of the transformed output.
  • Very narrow bins can increase overfitting, so select binning settings inside cross-validation.

Example: Discretize one feature

from sklearn.preprocessing import KBinsDiscretizer
import numpy as np

X = np.array([[0.1], [0.4], [0.8], [1.2]])
enc = KBinsDiscretizer(
    n_bins=2, encode="ordinal", strategy="uniform"
)
X_binned = enc.fit_transform(X)
print(X_binned.ravel())

Fits two equal-width bins to one feature and returns each sample's integer bin identifier.

When to use it

Feature engineering for linear models

Preparing continuous inputs for a linear model that needs nonlinear, piecewise behavior.

One-hot interval features

Replacing real-valued measurements with compact categorical indicators.

Quantization and compression

Reducing the number of discrete levels in numeric signals such as image intensities.

Interview questions

QWhat does KBinsDiscretizer do, and what can encode control?

It converts continuous features into intervals. The learned interval membership is returned as sparse one-hot output by default, dense one-hot output when requested, or integer bin identifiers with ordinal encoding.

Follow-up: How do the strategies differ? Uniform uses equal widths, quantile targets similarly populated bins, and k-means assigns boundaries from one-dimensional cluster centers.

QHow is n_bins specified and constrained?

n_bins can be one integer applied across features or an array specifying each feature's count. Values below two are invalid.

Follow-up: What happens when a feature has too few distinct values? The implementation can reduce its effective number of bins; such ignored features are placed to the right of the transformed output.

QCompare the available binning strategies.

Uniform creates equal-width intervals, quantile creates intervals with approximately equal sample counts, and kmeans uses nearest one-dimensional cluster centers. Each strategy defines boundaries independently for features.

Follow-up: Why might quantile be sensitive to repeated values? Repeated values can make requested boundaries coincide, reducing the number of effective intervals.

  • sklearn.preprocessing.Binarizer
  • sklearn.preprocessing.OneHotEncoder
  • sklearn.preprocessing.OrdinalEncoder
  • sklearn.preprocessing.PowerTransformer

Source: KBinsDiscretizer — scikit-learn 1.9.1 documentation. Checked 2026-10-15.