Class LabelEncoder<T>
- Namespace
- AiDotNet.Preprocessing.Encoders
- Assembly
- AiDotNet.dll
Encodes categorical values as integer labels (0, 1, 2, ...).
public class LabelEncoder<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
-
LabelEncoder<T>
- Implements
- Inherited Members
Remarks
LabelEncoder transforms categorical values to consecutive integers. Each unique value is assigned a unique integer starting from 0. This is useful for encoding target labels or ordinal features.
For Beginners: This encoder converts categories to numbers: - Each unique value gets a unique number starting from 0 - Values are sorted alphabetically/numerically before encoding
Example: ["cat", "dog", "cat", "bird", "dog"] → [1, 2, 1, 0, 2] (Mapping: bird=0, cat=1, dog=2)
Constructors
LabelEncoder(int[]?)
Creates a new instance of LabelEncoder<T>.
public LabelEncoder(int[]? columnIndices = null)
Parameters
columnIndicesint[]The column indices to encode, or null for all columns.
Properties
NClasses
Gets the number of unique classes for each encoded column.
public int[]? NClasses { get; }
Property Value
- int[]
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public override bool SupportsInverseTransform { get; }
Property Value
Methods
FitCore(Matrix<T>)
Learns the encoding mapping from the training data.
protected override void FitCore(Matrix<T> data)
Parameters
dataMatrix<T>The training data matrix where each column is a feature.
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 (LabelEncoder doesn't change number of features).
InverseTransformCore(Matrix<T>)
Reverses the label encoding to get original values.
protected override Matrix<T> InverseTransformCore(Matrix<T> data)
Parameters
dataMatrix<T>The encoded data.
Returns
- Matrix<T>
The original categorical values.
TransformCore(Matrix<T>)
Transforms the data by encoding categorical values as integers.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>The data to transform.
Returns
- Matrix<T>
The encoded data with integer labels.