Skip to main content

binarize

Function binarize 

Source
pub fn binarize<F>(x: &Array2<F>, threshold: F) -> Result<Array2<F>, FerroError>
where F: Float,
Expand description

Boolean thresholding of a dense array, element by element.

Values strictly greater than threshold become 1.0; all other values (less than or equal to the threshold) become 0.0. The result is a new, shape-preserving array.

This is the estimator-less functional form of Binarizer, mirroring scikit-learn’s binarize(X, *, threshold=0.0, copy=True) (sklearn/preprocessing/_data.py:2120-2174), whose dense path is cond = X > threshold; X[cond] = 1; X[not_cond] = 0 (:2170-2173) — the load-bearing strict greater-than. scikit-learn’s keyword default is threshold=0.0 (only positive values map to 1.0); here the caller passes the threshold explicitly.

binarize is decorated @validate_params({"threshold": [Interval(Real, None, None, closed="neither")]}) (_data.py:2112-2118), an OPEN interval (-inf, inf) that EXCLUDES NaN and ±inf. A non-finite threshold therefore raises InvalidParameterError (a ValueError) BEFORE any element comparison; this function mirrors that by returning FerroError::InvalidParameter for a non-finite threshold.

Binarizer’s Transform::transform delegates its element mapping to this function, so the two share one implementation.

§Errors

Returns FerroError::InvalidParameter if threshold is NaN or ±inf (sklearn Interval(Real, None, None, closed="neither"), _data.py:2114).

§Examples

use ferrolearn_preprocess::binarizer::binarize;
use ndarray::array;

let x = array![[0.4, 0.6, 0.5], [0.6, 0.1, 0.2]];
let out = binarize(&x, 0.5).unwrap();
// out = [[0.0, 1.0, 0.0], [1.0, 0.0, 0.0]]