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.
Home › Preprocessing › Encoding and imputation
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>.
Exceptions — ArgumentOutOfRangeException 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,1Remarks — 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.