Feature engineering for linear models
Preparing continuous inputs for a linear model that needs nonlinear, piecewise behavior.
Scikit-Learn · Class
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.
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.
class sklearn.preprocessing.KBinsDiscretizer(n_bins=5, *, encode='onehot', strategy='quantile', quantile_method='averaged_inverted_cdf', dtype=None, subsample=200000, random_state=None)| Name | Type | Description |
|---|---|---|
n_bins | int or array-like of shape (n_features,), default=5 | Number 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_method | str, default='averaged_inverted_cdf' | Quantile computation method used when strategy='quantile'. |
dtype | dtype, default=None | Output data type; when None, the estimator chooses an appropriate type. |
subsample | int or None, default=200000 | Maximum number of samples used to fit each feature; None disables subsampling. |
random_state | int, RandomState instance or None, default=None | Controls randomness when subsampling is used. |
fit returns the fitted estimator; transform returns a sparse matrix, dense array, or ordinal array according to encode.
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.
Preparing continuous inputs for a linear model that needs nonlinear, piecewise behavior.
Replacing real-valued measurements with compact categorical indicators.
Reducing the number of discrete levels in numeric signals such as image intensities.
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.
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.
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.Binarizersklearn.preprocessing.OneHotEncodersklearn.preprocessing.OrdinalEncodersklearn.preprocessing.PowerTransformer