PolynomialFeatures
Module: strata.preprocessing • Kind: struct • Traits: Copyable, Movable, Transformer
Source: strata/preprocessing/polynomial.mojo
struct PolynomialFeatures[compute_dtype: DType = DType.float64](Copyable, Movable, Transformer)from strata.preprocessing import PolynomialFeaturesGenerate polynomial and interaction features.
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, if an input sample is 2D and of the form , the degree-2 polynomial features with bias are .
Parameters (Compile-Time)
Section titled “Parameters (Compile-Time)”| Parameter | Description |
|---|---|
compute_dtype |
Computational precision data type. Default DType.float64. |
Constructors
Section titled “Constructors”def __init__( out self, degree: Int = 2, interaction_only: Bool = False, include_bias: Bool = True,)Initialize the PolynomialFeatures transformer.
| Argument | Type | Description |
|---|---|---|
degree |
Int |
Maximum polynomial degree (>= 0). Default 2. |
interaction_only |
Bool |
Whether to produce only interaction terms. Default False. |
include_bias |
Bool |
Whether to include a bias column (degree 0). Default True. |
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
powers_ |
Exponent matrix with shape (n_output_features_, n_features_in_). |
n_features_in_ |
Number of features seen during fit. |
n_output_features_ |
Total number of polynomial output features. |
is_fitted |
Boolean flag indicating if estimator has been fitted. |
Methods Overview
Section titled “Methods Overview”| Method | Description |
|---|---|
PolynomialFeatures.fit() |
Compute the number of output features and combination powers. |
PolynomialFeatures.transform() |
Transform data matrix X to polynomial feature combinations. |
PolynomialFeatures.fit_transform() |
Fit to data, then transform it. |
Method Details
Section titled “Method Details”PolynomialFeatures.fit()
Section titled “PolynomialFeatures.fit()”def fit[in_dtype: DType](mut self, X: Matrix[in_dtype])
def fit[feat_dtype: DType, target_dtype: DType]( mut self, dataset: Dataset[feat_dtype, target_dtype],)Overload Note: This method accepts either a standard
X: Matrixor a unifieddataset: Datasetcontainer.
Compute the number of output features and combination powers.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[in_dtype] |
Input feature matrix with shape (n_samples, n_features). |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix. (Can be provided alternatively in place of X) |
PolynomialFeatures.transform()
Section titled “PolynomialFeatures.transform()”def transform[in_dtype: DType]( self, X: Matrix[in_dtype],) -> Matrix[in_dtype]
def transform[feat_dtype: DType, target_dtype: DType]( self, dataset: Dataset[feat_dtype, target_dtype],) -> Dataset[feat_dtype, target_dtype]Overload Note: This method accepts either a standard
X: Matrixor a unifieddataset: Datasetcontainer.
Transform data matrix X to polynomial feature combinations.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[in_dtype] |
Data matrix with shape (n_samples, n_features). |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix. (Can be provided alternatively in place of X) |
Returns: Matrix[in_dtype] — Matrix[in_dtype]: Expanded polynomial matrix with shape (n_samples, n_output_features_).
PolynomialFeatures.fit_transform()
Section titled “PolynomialFeatures.fit_transform()”def fit_transform[in_dtype: DType]( mut self, X: Matrix[in_dtype],) -> Matrix[in_dtype]
def fit_transform[feat_dtype: DType, target_dtype: DType]( mut self, dataset: Dataset[feat_dtype, target_dtype],) -> Dataset[feat_dtype, target_dtype]Overload Note: This method accepts either a standard
X: Matrixor a unifieddataset: Datasetcontainer.
Fit to data, then transform it.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[in_dtype] |
Input matrix with shape (n_samples, n_features). |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix. (Can be provided alternatively in place of X) |
Returns: Matrix[in_dtype] — Matrix[in_dtype]: Expanded polynomial matrix.
Section titled “Returns: Matrix[in_dtype] — Matrix[in_dtype]: Expanded polynomial matrix.”Example
Section titled “Example”from strata.preprocessing import PolynomialFeaturesfrom strata.core import Matrix
var poly = PolynomialFeatures[DType.float64](degree=2)poly.fit(X)var X_poly = poly.transform(X)