Preprocessing encoders onehot - CyrilB1531/lodestar GitHub Wiki

Development build. This page describes main, not a released package. The latest published Lodestar.Preprocessing is 0.1.0 — read its documentation.

HomePreprocessingEncoding 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 parametersT is the category type; string and int are the two the reference takes.

Parametersvalues 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>.

ExceptionsArgumentOutOfRangeException when featureCount is not positive, or when options holds a Drop or an Unknown that is not a defined value. ArgumentException when values holds no row, a partial one, or a null.

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 alsoOneHotEncoderOptions, Encoders.Ordinal.

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