Class OrdinalEncoder<T>
- Namespace
- AiDotNet.Preprocessing.Encoders
- Assembly
- AiDotNet.dll
Encodes categorical values as ordinal integers with optional custom ordering.
public class OrdinalEncoder<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
-
OrdinalEncoder<T>
- Implements
- Inherited Members
Remarks
OrdinalEncoder transforms categorical values to consecutive integers based on order. Unlike LabelEncoder, it can accept custom category orderings and handle unknown values.
For Beginners: This encoder converts categories to ordered numbers: - You can specify the order of categories - Useful when categories have a natural ordering (e.g., low, medium, high)
Example with custom order ["small", "medium", "large"]: ["large", "small", "medium", "large"] → [2, 0, 1, 2]
Constructors
OrdinalEncoder(List<double[]>?, UnknownValueHandling, double, int[]?)
Creates a new instance of OrdinalEncoder<T>.
public OrdinalEncoder(List<double[]>? categories = null, UnknownValueHandling handleUnknown = UnknownValueHandling.Error, double unknownValue = -1, int[]? columnIndices = null)
Parameters
categoriesList<double[]>Optional list of category orderings for each column. If null, categories are inferred from data.
handleUnknownUnknownValueHandlingHow to handle unknown categories. Defaults to Error.
unknownValuedoubleThe value to use for unknown categories when handling is UseEncodedValue. Defaults to -1.
columnIndicesint[]The column indices to encode, or null for all columns.
Properties
HandleUnknown
Gets how unknown categories are handled.
public UnknownValueHandling HandleUnknown { get; }
Property Value
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public override bool SupportsInverseTransform { get; }
Property Value
UnknownValue
Gets the value used for unknown categories.
public double UnknownValue { get; }
Property Value
Methods
FitCore(Matrix<T>)
Learns the encoding mapping from the training data or uses provided categories.
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 (OrdinalEncoder doesn't change number of features).
InverseTransformCore(Matrix<T>)
Reverses the ordinal 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 ordinal integers.
protected override Matrix<T> TransformCore(Matrix<T> data)
Parameters
dataMatrix<T>The data to transform.
Returns
- Matrix<T>
The encoded data with ordinal integers.