Preprocessing polynomialfeatures transform - CyrilB1531/lodestar GitHub Wiki

Development build. This page describes main, not a released package. The latest published Lodestar.Preprocessing is 0.2.0 โ€” read its documentation.

Home โ€บ Preprocessing โ€บ Feature transforming

PolynomialFeatures.Transform

Expands a row-major matrix into its polynomial terms.

public static double[] Transform(ReadOnlySpan<double> samples, int featureCount, PolynomialFeaturesOptions options = null)

Parameters โ€” samples is the matrix, row-major, featureCount values per row; the span is read, never modified. options chooses the degree, whether to keep only interactions, and whether to emit the bias; null takes the reference's defaults, which are degree two with the bias and without the interaction restriction.

Returns โ€” a new matrix, OutputFeatureCount values per row.

Exceptions โ€” ArgumentOutOfRangeException when featureCount is not positive or the degree is negative. ArgumentException when samples holds no row, a partial one, or a non-finite value; or when the degree is 0 and the bias is off, which leaves no term at all โ€” the reference refuses that pair in as many words. ArgumentException also when the terms, or the rows times the terms, are more than one array holds (#1314); the terms are counted before any is listed (#1230).

Example โ€” two features at the default degree, which is what a linear model needs to see a curve or an interaction.

using Lodestar.Preprocessing;

double[] row = [3.0, 4.0];

double[] expanded = PolynomialFeatures.Transform(row, featureCount: 2);

double bias = expanded[0];        // => 1
double squareOfFirst = expanded[3];  // => 9
double product = expanded[4];     // => 12

The six columns are 1, x0, x1, x0ยฒ, x0ยทx1, x1ยฒ โ€” which is what FeatureNames says, and the order a coefficient vector will come back in.

Remarks โ€” the count grows fast, which is the whole cost of this transformer. Four features at degree three is thirty-five columns; eight at degree four is four hundred and ninety-five. Ask OutputFeatureCount before expanding a wide matrix, because the expansion allocates all of it.

PolynomialFeaturesOptions.InteractionOnly keeps the products and drops the powers, which is what a caller wants when the question is whether two features act together rather than whether one of them curves.

using Lodestar.Preprocessing;

var options = new PolynomialFeaturesOptions
{
    Degree = 2,
    InteractionOnly = true,
    IncludeBias = false,
};

string[] names = PolynomialFeatures.FeatureNames(3, options);

string last = names[5];   // => x1 x2
int count = names.Length; // => 6

Applies to โ€” net10.0, netstandard2.0.

See also โ€” PolynomialFeaturesOptions, PolynomialFeatures.FeatureNames, the Python equivalence table.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ