Class BinaryEncoder<T>
- Namespace
- AiDotNet.Preprocessing.Encoders
- Assembly
- AiDotNet.dll
Encodes categorical features using binary representation.
public class BinaryEncoder<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
-
BinaryEncoder<T>
- Implements
- Inherited Members
Remarks
BinaryEncoder first ordinal encodes the categories, then converts those integers to their binary representation. This creates log2(n) columns instead of n columns that one-hot encoding would create.
For Beginners: If you have 8 categories, one-hot creates 8 columns. Binary encoding uses only 3 columns (since 8 = 2^3): - Category 0 → [0, 0, 0] - Category 1 → [0, 0, 1] - Category 2 → [0, 1, 0] - Category 7 → [1, 1, 1]
This is useful for high-cardinality categorical features where one-hot would create too many columns.
Constructors
BinaryEncoder(BinaryEncoderHandleUnknown, int[]?)
Creates a new instance of BinaryEncoder<T>.
public BinaryEncoder(BinaryEncoderHandleUnknown handleUnknown = BinaryEncoderHandleUnknown.AllZeros, int[]? columnIndices = null)
Parameters
handleUnknownBinaryEncoderHandleUnknownHow to handle unknown categories. Defaults to AllZeros.
columnIndicesint[]The column indices to encode, or null for all columns.
Properties
HandleUnknown
Gets how unknown categories are handled.
public BinaryEncoderHandleUnknown HandleUnknown { get; }
Property Value
NOutputFeatures
Gets the number of output features after transformation.
public int NOutputFeatures { get; }
Property Value
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public override bool SupportsInverseTransform { get; }
Property Value
Methods
FitCore(Matrix<T>)
Learns the categories and binary encoding from the training data.
protected override void FitCore(Matrix<T> data)
Parameters
dataMatrix<T>The training data matrix.
GetFeatureNamesOut(string[]?)
Gets the output feature names after transformation.
public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)
Parameters
inputFeatureNamesstring[]
Returns
- string[]
InverseTransformCore(Matrix<T>)
Reverses the binary encoding to get original category values.
protected override Matrix<T> InverseTransformCore(Matrix<T> data)
Parameters
dataMatrix<T>The binary encoded data.
Returns
- Matrix<T>
The original categorical values.
TransformCore(Matrix<T>)
Transforms the data by converting categories to binary representation.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>The data to transform.
Returns
- Matrix<T>
The binary encoded data.