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.
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:
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:
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:
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:
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_transformon train and test separately instead offit_transformon train andtransformon test. - Forgetting that feature count grows quickly with polynomial expansion.
- Leaving
include_bias=Truewhen 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_transformon training data andtransformon validation or test data. - Prefer a
Pipelineso preprocessing and modeling stay aligned. - Watch for feature explosion, scaling issues, and overfitting.
Related reading
- Quantize a Keras neural network model
- Quantize Tensorflow Graph to float16
- Question about Backpropagation Algorithm with Artificial Neural Networks -- Order of updating
- R - Calculate Test MSE given a trained model from a training set and a test set
- Query that will find users who post THE SAME SET of marks as user2
- R - How to create a stacker ensemble?
- R - XGBoost Error building DMatrix
- R caret svmRadial keep sigma constant and use grid search for C
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.