Table of Contents

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

T

The numeric type for calculations (e.g., float, double).

Inheritance
TransformerBase<T, Matrix<T>, Matrix<T>>
OrdinalEncoder<T>
Implements
IDataTransformer<T, Matrix<T>, Matrix<T>>
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

categories List<double[]>

Optional list of category orderings for each column. If null, categories are inferred from data.

handleUnknown UnknownValueHandling

How to handle unknown categories. Defaults to Error.

unknownValue double

The value to use for unknown categories when handling is UseEncodedValue. Defaults to -1.

columnIndices int[]

The column indices to encode, or null for all columns.

Properties

HandleUnknown

Gets how unknown categories are handled.

public UnknownValueHandling HandleUnknown { get; }

Property Value

UnknownValueHandling

SupportsInverseTransform

Gets whether this transformer supports inverse transformation.

public override bool SupportsInverseTransform { get; }

Property Value

bool

UnknownValue

Gets the value used for unknown categories.

public double UnknownValue { get; }

Property Value

double

Methods

FitCore(Matrix<T>)

Learns the encoding mapping from the training data or uses provided categories.

protected override void FitCore(Matrix<T> data)

Parameters

data Matrix<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

inputFeatureNames string[]

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

data Matrix<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

data Matrix<T>

The data to transform.

Returns

Matrix<T>

The encoded data with ordinal integers.