Preprocessing encoders onehot - CyrilB1531/lodestar GitHub Wiki

Home › Preprocessing › Encoding and imputation

Encoders.OneHot

Fits a one-hot encoder on a row-major matrix of categories.

public static OneHotEncoder<T> OneHot<T>(ReadOnlySpan<T> values, int featureCount, OneHotEncoderOptions options = null) where T : IComparable<T>, IEquatable<T>

Type parameters — T is the category type; string and int are the two the reference takes.

Parameters — values is the categories, row-major: featureCount per row. featureCount is how many values each row carries. options chooses which category to drop and what to do with an unseen value; null drops none and refuses.

Returns — a fitted OneHotEncoder<T>. Its categories are sorted. The order is numpy's: a string sorts by code point, not by UTF-16 unit (#1254), and a floating NaN sorts last (#1229).

Exceptions — ArgumentOutOfRangeException when featureCount is not positive, or when options holds a Drop or an Unknown that is not a defined value, a MinFrequency or a MaxCategories below 1, or a MinFrequencyShare outside (0, 1). ArgumentException when values holds no row, a partial one, or a null, or when options sets both MinFrequency and MinFrequencyShare; and when values holds an infinite floating category, as scikit-learn's allow-nan check refuses it (#1229).

Example — two features, and the column layout they produce.

using Lodestar.Preprocessing;

// Two features per row: the first has three categories, the second two.
string[] values = ["b", "x", "a", "y", "c", "x", "a", "y"];

OneHotEncoder<string> encoder = Encoders.OneHot(values, featureCount: 2);

int columns = encoder.EncodedFeatureCount;                   // => 5
string row = string.Join(",", encoder.Transform(["a", "y"])); // => 1,0,0,0,1

Remarks — the first feature's columns come first, in its own sorted order, then the second's. That is the reference's layout, and it is why EncodedFeatureCount is worth reading before slicing the result.

Applies to — net10.0, netstandard2.0.

See also — OneHotEncoderOptions, Encoders.Ordinal.

⚠️ **GitHub.com Fallback** ⚠️