Skip to main content

PolynomialFeatures

#include <Skigen/Preprocessing>

template <typename Scalar = double>
class Skigen::PolynomialFeatures(degree=2, include_bias=true, interaction_only=false)

Generate polynomial and interaction features.

Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree. For example, if an input sample is two dimensional and of the form [a, b], the degree-2 polynomial features are [1, a, b, a², ab, b²].

Mirrors sklearn.preprocessing.PolynomialFeatures.


Parameters:

  • degree : int, default=2 Maximum polynomial degree (int, default 2).

  • include_bias : bool, default=true If true, include a bias column of ones (bool, default true).

  • interaction_only : bool, default=false If true, only interaction features are produced (bool, default false).


Attributes:

  • degree : int Maximum polynomial degree.

  • n_output_features : Eigen::Index Number of output features after transformation.


Methods

fit(X)

Compute the powers matrix for later transformation.

Parameters:

  • X : MatrixType Training data of shape (n_samples, n_features).

Returns:

  • result : PolynomialFeatures Reference to the fitted transformer (*this).

transform(X)

Transform data to polynomial features.

Parameters:

  • X : MatrixType Data matrix of shape (n_samples, n_features).

Returns:

  • result : MatrixType Transformed matrix of shape (n_samples, n_output_features).

Throws:

  • std::runtime_error — if the model has not been fitted.

inverse_transform()

Not supported — polynomial feature generation is not reversible.

Throws:

  • std::runtime_error — Always throws.

Example

// Degree 2 with bias: 1, x1, x2, x1², x1·x2, x2²
Skigen::PolynomialFeatures<double> poly2(2);
Eigen::MatrixXd X2 = poly2.fit_transform(X);

std::cout << "Degree 2 with bias (" << X2.rows() << "x" << X2.cols()
<< "):\n" << X2 << "\n\n";

// Degree 2 without bias: x1, x2, x1², x1·x2, x2²
Skigen::PolynomialFeatures<double> poly2_no_bias(2, false);
Eigen::MatrixXd X2nb = poly2_no_bias.fit_transform(X);

std::cout << "Degree 2 no bias (" << X2nb.rows() << "x" << X2nb.cols()
<< "):\n" << X2nb << "\n\n";