Class Binarizer<T>
- Namespace
- AiDotNet.Preprocessing.Discretizers
- Assembly
- AiDotNet.dll
Binarizes features based on a threshold value.
public class Binarizer<T> : TransformerBase<T, Matrix<T>, Matrix<T>>, IDataTransformer<T, Matrix<T>, Matrix<T>>
Type Parameters
TThe numeric type for calculations (e.g., float, double).
- Inheritance
-
Binarizer<T>
- Implements
- Inherited Members
Remarks
Binarization transforms continuous values to binary (0 or 1) based on a threshold. Values greater than the threshold become 1, values less than or equal become 0.
For Beginners: This transformer converts any values to just 0s and 1s: - If a value is above the threshold → 1 - If a value is at or below the threshold → 0
Example with threshold=5: [3, 6, 2, 8, 5] → [0, 1, 0, 1, 0]
Constructors
Binarizer(double, int[]?)
Creates a new instance of Binarizer<T> with a custom threshold.
public Binarizer(double threshold, int[]? columnIndices = null)
Parameters
thresholddoubleThe threshold value. Values greater than this become 1, others become 0.
columnIndicesint[]The column indices to binarize, or null for all columns.
Binarizer(int[]?)
Creates a new instance of Binarizer<T> with a default threshold of 0.
public Binarizer(int[]? columnIndices = null)
Parameters
columnIndicesint[]The column indices to binarize, or null for all columns.
Properties
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public override bool SupportsInverseTransform { get; }
Property Value
Remarks
Binarization is a lossy transformation - the original values cannot be recovered.
Threshold
Gets the threshold value used for binarization.
public T Threshold { get; }
Property Value
- T
Methods
FitCore(Matrix<T>)
Fits the binarizer to the training data.
protected override void FitCore(Matrix<T> data)
Parameters
dataMatrix<T>The training data matrix.
Remarks
Binarization is a stateless transformation - fitting does nothing except validate the data.
GetFeatureNamesOut(string[]?)
Gets the output feature names after transformation.
public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)
Parameters
inputFeatureNamesstring[]The input feature names.
Returns
- string[]
The same feature names (Binarizer doesn't change number of features).
InverseTransformCore(Matrix<T>)
Inverse transformation is not supported for binarization.
protected override Matrix<T> InverseTransformCore(Matrix<T> data)
Parameters
dataMatrix<T>The binarized data.
Returns
- Matrix<T>
Never returns - always throws.
Exceptions
- NotSupportedException
Always thrown because binarization is lossy.
TransformCore(Matrix<T>)
Transforms the data by applying threshold binarization.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>The data to transform.
Returns
- Matrix<T>
The binarized data (0s and 1s).