ytm.utils.Binarizer#

class ytm.utils.Binarizer(bins: int, edge_bins: bool = False, method: Literal['uniform', 'quantile'] = 'uniform', encoding: Literal['thermometer', 'onehot'] = 'thermometer')#

Bases: object

Binarize continuous features into thermometer or one-hot encoded bins.

Transforms features into binary representations suitable for TM input.

Parameters:
binsint

Number of inner bins (thresholds). Thermometer output has bins bits per feature; one-hot has bins + 1.

edge_binsbool, default=False

If True, add two extra bins covering values below min_val and above max_val.

method{“uniform”, “quantile”}, default=”uniform”

Strategy for computing thresholds. "uniform" spaces thresholds evenly between min and max. "quantile" places thresholds at equal-frequency percentiles.

encoding{“thermometer”, “onehot”}, default=”thermometer”

"thermometer" sets all bits up to the bin index. "onehot" sets exactly one bit.

Examples

>>> binarizer = Binarizer(bins=8)
>>> binarizer.fit(X_train)
>>> X_bin = binarizer.transform(X_train)
>>> X_tr = binarizer.collapse_bins(X_bin)
collapse_bins(X: ndarray) → ndarray#

Reverse a binarized array back to integer bin indices.

Parameters:
Xndarray of shape (N, …, binned_dim)

Binarized array produced by transform().

Returns:
ndarray of shape (N, …)

Integer bin index per feature.

fit(X: ndarray, thresholds=None, min_val=None, max_val=None) → None#

Compute bin thresholds from data.

Parameters:
Xndarray of shape (N, …)

Input data used to compute thresholds.

thresholdsarray-like of shape (bins,), optional

Explicit inner thresholds. Ignores method if provided.

min_valfloat, optional

Override minimum value. Defaults to X.min().

max_valfloat, optional

Override maximum value. Defaults to X.max().

fit_transform(X: ndarray) → ndarray#

Fit thresholds and binarize X in one step.

Parameters:
Xndarray of shape (N, …)

Input data.

Returns:
ndarray

Binarized output. See transform() for shape details.

transform(X: ndarray) → ndarray#

Binarize X using the fitted thresholds.

Parameters:
Xndarray of shape (N, …)

Input data.

Returns:
ndarray of shape (N, …, bins) or (N, …, bins+1)

Binarized output. Last dimension size depends on encoding and edge_bins.