Table of Contents

Class PolynomialFeatures<T>

Namespace
AiDotNet.Preprocessing.FeatureGeneration
Assembly
AiDotNet.dll

Generates polynomial and interaction features.

public class PolynomialFeatures<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>>
PolynomialFeatures<T>
Implements
IDataTransformer<T, Matrix<T>, Matrix<T>>
Inherited Members

Remarks

PolynomialFeatures generates a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree. For example, with degree=2 and input [a, b], generates [1, a, b, a², ab, b²].

For Beginners: This transformer creates new features from existing ones: - Polynomial terms: a, a², a³, etc. - Interaction terms: a*b, a*b*c, etc. - Useful for capturing non-linear relationships

Example with degree=2 and features [x₁, x₂]: Output: [1, x₁, x₂, x₁², x₁x₂, x₂²]

Constructors

PolynomialFeatures(int, bool, bool, int[]?)

Creates a new instance of PolynomialFeatures<T>.

public PolynomialFeatures(int degree = 2, bool interactionOnly = false, bool includeBias = true, int[]? columnIndices = null)

Parameters

degree int

The maximum degree of polynomial features. Defaults to 2.

interactionOnly bool

If true, only interaction features are produced (no x², x³, etc.). Defaults to false.

includeBias bool

If true, includes a bias (constant 1) column. Defaults to true.

columnIndices int[]

The column indices to use for polynomial generation. If null, uses all columns.

Properties

Degree

Gets the degree of polynomial features.

public int Degree { get; }

Property Value

int

IncludeBias

Gets whether a bias (constant) column is included.

public bool IncludeBias { get; }

Property Value

bool

InteractionOnly

Gets whether only interaction features are generated.

public bool InteractionOnly { get; }

Property Value

bool

NInputFeatures

Gets the number of input features.

public int NInputFeatures { get; }

Property Value

int

NOutputFeatures

Gets the number of output features after transformation.

public int NOutputFeatures { get; }

Property Value

int

SupportsInverseTransform

Gets whether this transformer supports inverse transformation.

public override bool SupportsInverseTransform { get; }

Property Value

bool

Remarks

Inverse transform is not supported because polynomial feature generation is not reversible.

Methods

FitCore(Matrix<T>)

Learns the polynomial feature combinations from the training data.

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 output feature names with polynomial notation.

InverseTransformCore(Matrix<T>)

Inverse transformation is not supported for polynomial features.

protected override Matrix<T> InverseTransformCore(Matrix<T> data)

Parameters

data Matrix<T>

The transformed data.

Returns

Matrix<T>

Never returns - always throws.

Exceptions

NotSupportedException

Always thrown because polynomial expansion is not reversible.

TransformCore(Matrix<T>)

Transforms the data by generating polynomial features.

protected override Matrix<T> TransformCore(Matrix<T> data)

Parameters

data Matrix<T>

The data to transform.

Returns

Matrix<T>

The data with polynomial features.