ytm.utils.Binarizer#
- class ytm.utils.Binarizer(bins: int, edge_bins: bool = False, method: Literal['uniform', 'quantile'] = 'uniform', encoding: Literal['thermometer', 'onehot'] = 'thermometer')#
Bases:
objectBinarize 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
binsbits per feature; one-hot hasbins + 1.- edge_binsbool, default=False
If True, add two extra bins covering values below
min_valand abovemax_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
methodif 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
Xin one step.- Parameters:
- Xndarray of shape (N, …)
Input data.
- Returns:
- ndarray
Binarized output. See
transform()for shape details.