Skip to content

Binarizer

Module: strata.preprocessing • Kind: struct • Traits: Copyable, Movable, Transformer
Source: strata/preprocessing/binarizer.mojo

struct Binarizer[compute_dtype: DType = DType.float64](Copyable, Movable, Transformer)
from strata.preprocessing import Binarizer

Binarize feature values according to a threshold.

Values strictly greater than the threshold map to 1, while values less than or equal to the threshold map to 0:

xbin={1if x>threshold0otherwisex_{\text{bin}} = \begin{cases} 1 & \text{if } x > \text{threshold} \\ 0 & \text{otherwise} \end{cases}
Parameter Description
compute_dtype Computational precision data type. Default DType.float64.

def __init__(out self, threshold: Scalar[Self.compute_dtype] = 0.0)

Initialize the Binarizer.

Argument Type Description
threshold Scalar[Self.compute_dtype] Feature threshold boundary. Values greater than this map to 1. Default 0.0.

Attribute Description
n_features_in_ Number of features seen during fit.
is_fitted Boolean flag indicating if estimator has been fitted.

Method Description
Binarizer.fit() Compute fitted parameters from input matrix X.
Binarizer.transform() Transform input features using the fitted parameters.
Binarizer.fit_transform() Fit the estimator to X and return the transformed output.

def fit[in_dtype: DType](mut self, X: Matrix[in_dtype])
def fit[feat_dtype: DType, target_dtype: DType](
mut self,
dataset: Dataset[feat_dtype, target_dtype],
)

Overload Note: This method accepts either a standard X: Matrix or a unified dataset: Dataset container.

Compute fitted parameters from input matrix X.

Parameter Type Description
X Matrix[in_dtype] Feature matrix.
dataset Dataset[feat_dtype, target_dtype] Dataset container holding feature matrix. (Can be provided alternatively in place of X)

def transform[in_dtype: DType](
self,
X: Matrix[in_dtype],
) -> Matrix[in_dtype]
def transform[feat_dtype: DType, target_dtype: DType](
self,
dataset: Dataset[feat_dtype, target_dtype],
) -> Dataset[ feat_dtype, target_dtype ]

Overload Note: This method accepts either a standard X: Matrix or a unified dataset: Dataset container.

Transform input features using the fitted parameters.

Parameter Type Description
X Matrix[in_dtype] Feature matrix.
dataset Dataset[feat_dtype, target_dtype] Dataset container holding feature matrix. (Can be provided alternatively in place of X)

Returns: Matrix[in_dtype]


def fit_transform[in_dtype: DType](
mut self,
X: Matrix[in_dtype],
) -> Matrix[in_dtype]
def fit_transform[feat_dtype: DType, target_dtype: DType](
mut self,
dataset: Dataset[feat_dtype, target_dtype],
) -> Dataset[ feat_dtype, target_dtype ]

Overload Note: This method accepts either a standard X: Matrix or a unified dataset: Dataset container.

Fit the estimator to X and return the transformed output.

Parameter Type Description
X Matrix[in_dtype] Feature matrix.
dataset Dataset[feat_dtype, target_dtype] Dataset container holding feature matrix. (Can be provided alternatively in place of X)
from strata.preprocessing import Binarizer
from strata.core import Matrix
var binarizer = Binarizer[DType.float64](threshold=0.5)
binarizer.fit(X_train)
var X_bin = binarizer.transform(X_train)