data preprocessing
quadratic featurizer
feature engineering
fit_transform
machine learning

quadratic featurizer preprocessing with fit_transform

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

A quadratic featurizer expands an input vector so the model can learn squared terms and pairwise feature interactions. In scikit-learn, the usual tool is PolynomialFeatures(degree=2), and the main preprocessing question is when to use fit_transform versus transform. The correct answer is the same as with most preprocessors: fit on the training data, then reuse the learned feature mapping on validation and test data.

What a Quadratic Featurizer Produces

If the original feature vector is [x1, x2], a quadratic expansion can produce terms such as:

  • 'x1'
  • 'x2'
  • 'x1^2'
  • 'x1 * x2'
  • 'x2^2'

Depending on configuration, it may also include a bias column of 1s. This is useful because a linear model trained on the expanded features can represent some nonlinear behavior in the original input space.

In scikit-learn, the standard implementation is:

python
from sklearn.preprocessing import PolynomialFeatures

featurizer = PolynomialFeatures(degree=2, include_bias=False)

include_bias=False is common when the model already has an intercept term.

Use fit_transform on Training Data

For the training set, call fit_transform:

python
1import numpy as np
2from sklearn.preprocessing import PolynomialFeatures
3
4X_train = np.array([
5    [2.0, 3.0],
6    [4.0, 5.0],
7    [6.0, 7.0],
8])
9
10featurizer = PolynomialFeatures(degree=2, include_bias=False)
11X_train_quad = featurizer.fit_transform(X_train)
12
13print(X_train_quad)

This both learns the output feature layout and transforms the input in one step. For degree=2, the mapping itself is deterministic, but the preprocessing API still follows the same fit/transform contract used across scikit-learn.

The resulting columns for two input features are typically:

  • 'x1'
  • 'x2'
  • 'x1^2'
  • 'x1 * x2'
  • 'x2^2'

That expanded matrix is what you then pass into the model.

Use transform on Validation and Test Data

Once the featurizer is fit on training data, do not call fit_transform on the test set. Use transform:

python
1X_test = np.array([
2    [8.0, 9.0],
3    [1.5, 2.5],
4])
5
6X_test_quad = featurizer.transform(X_test)
7print(X_test_quad)

This preserves the same column ordering and preprocessing contract. Even though PolynomialFeatures does not learn means or variances the way a scaler does, you should still follow the standard training-only fit pattern. That keeps the pipeline consistent and prevents data leakage habits from creeping into more stateful preprocessors.

Put It in a Pipeline

The cleanest way to use quadratic features is usually with a Pipeline:

python
1from sklearn.pipeline import Pipeline
2from sklearn.preprocessing import PolynomialFeatures
3from sklearn.linear_model import LinearRegression
4
5model = Pipeline([
6    ("quadratic", PolynomialFeatures(degree=2, include_bias=False)),
7    ("regressor", LinearRegression()),
8])
9
10model.fit(X_train, [1.0, 2.0, 3.0])
11predictions = model.predict(X_test)
12print(predictions)

This keeps feature generation tied to the model and avoids mistakes where training and inference paths diverge.

Know the Cost of Quadratic Expansion

Quadratic features grow quickly as the number of original features increases. With many input columns, the expanded matrix becomes wide and can make models slower, more memory-hungry, and more prone to overfitting.

That means quadratic featurization is most useful when:

  • The original feature count is moderate.
  • You suspect interactions matter.
  • The downstream model benefits from explicit nonlinear terms.

It is less attractive when the feature space is already very large or sparse.

Scale Thoughtfully

If the downstream model is sensitive to scale, pair the featurizer with a scaler after expansion. Squared terms can become much larger than the original inputs, and that can destabilize optimization for some models.

Common Pitfalls

  • Calling fit_transform on train and test separately instead of fit_transform on train and transform on test.
  • Forgetting that feature count grows quickly with polynomial expansion.
  • Leaving include_bias=True when the model already includes an intercept.
  • Using quadratic features without considering whether scaling is needed.
  • Adding polynomial expansion by habit when the model or data does not justify it.

Summary

  • A quadratic featurizer adds squared terms and pairwise interactions.
  • In scikit-learn, PolynomialFeatures(degree=2) is the standard tool.
  • Use fit_transform on training data and transform on validation or test data.
  • Prefer a Pipeline so preprocessing and modeling stay aligned.
  • Watch for feature explosion, scaling issues, and overfitting.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.