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
TThe numeric type for calculations (e.g., float, double).
- Inheritance
-
PolynomialFeatures<T>
- Implements
- 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
degreeintThe maximum degree of polynomial features. Defaults to 2.
interactionOnlyboolIf true, only interaction features are produced (no x², x³, etc.). Defaults to false.
includeBiasboolIf true, includes a bias (constant 1) column. Defaults to true.
columnIndicesint[]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
IncludeBias
Gets whether a bias (constant) column is included.
public bool IncludeBias { get; }
Property Value
InteractionOnly
Gets whether only interaction features are generated.
public bool InteractionOnly { get; }
Property Value
NInputFeatures
Gets the number of input features.
public int NInputFeatures { get; }
Property Value
NOutputFeatures
Gets the number of output features after transformation.
public int NOutputFeatures { get; }
Property Value
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public override bool SupportsInverseTransform { get; }
Property Value
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
dataMatrix<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
inputFeatureNamesstring[]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
dataMatrix<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
dataMatrix<T>The data to transform.
Returns
- Matrix<T>
The data with polynomial features.