Skip to main content

PolynomialFeatures

Generates a new feature matrix consisting of all polynomial combinations of the input features up to a given degree. This enables fitting polynomial relationships with a linear model.

Feature Expansion

For input features [x1,x2][x_1, x_2] with degree d=2d = 2 and bias:

ϕ(x)=[1,  x1,  x2,  x12,  x1x2,  x22]\phi(x) = [1,\; x_1,\; x_2,\; x_1^2,\; x_1 x_2,\; x_2^2]

The number of output features is:

output features=(p+dd)(with bias),(p+dd)1(without bias)\text{output features} = \binom{p + d}{d} \quad \text{(with bias)}, \qquad \binom{p + d}{d} - 1 \quad \text{(without bias)}

where pp is the number of input features and dd is the degree. For example, p=2,d=3p = 2,\, d = 3 produces (53)=10\binom{5}{3} = 10 features.

Interaction Only

When interaction_only = true, only interaction features are generated (no powers xjkx_j^k for k>1k > 1). For [x1,x2][x_1, x_2] with d=2d = 2, this yields [1,  x1,  x2,  x1x2][1,\; x_1,\; x_2,\; x_1 x_2].

Usage with Linear Models

Polynomial features combined with a linear model create a polynomial regression pipeline:

auto pipe = Skigen::make_pipeline(
Skigen::PolynomialFeatures<>(3),
Skigen::LinearRegression<>()
);

This fits y=w0+w1x+w2x2+w3x3y = w_0 + w_1 x + w_2 x^2 + w_3 x^3 while keeping the model linear in the parameters.

Mirrors sklearn.preprocessing.PolynomialFeatures.

Constructor

Skigen::PolynomialFeatures<Scalar> poly(int degree = 2,
bool include_bias = true,
bool interaction_only = false);
ParameterDefaultDescription
degree2Maximum polynomial degree
include_biastrueInclude a bias column of ones
interaction_onlyfalseOnly interaction features (no powers)

Methods

MethodDescription
fit(X)Compute output feature structure
transform(X)Generate polynomial features
fit_transform(X)Fit and transform in one call

Fitted Attributes

AccessorTypeDescription
n_output_features()Eigen::IndexNumber of output features

Example

#include <Skigen/Preprocessing>

// X has 2 features → degree=2 generates: 1, x1, x2, x1², x1·x2, x2²
Skigen::PolynomialFeatures poly(2);
auto X_poly = poly.fit_transform(X);