Build an Interaction-Only Design Matrix with PolynomialFeatures

Quick Overview

Expand a three-feature matrix into main effects and pairwise interaction terms with scikit-learn PolynomialFeatures, without squared terms or a bias column. It tests whether you know the flags, the output column order, and how interaction terms change a linear model.

Build an Interaction-Only Design Matrix with PolynomialFeatures

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def interaction_only_design(X: np.ndarray) -> np.ndarray`. Given a 2-D float ndarray `X` of shape `(n, 3)`, transform it with `sklearn.preprocessing.PolynomialFeatures(degree=2, interaction_only=True, include_bias=False)` and return the float ndarray. The result must have shape `(n, 6)` with columns in the order `[x0, x1, x2, x0*x1, x0*x2, x1*x2]`. It excludes the bias column and all squared terms (no `x0^2`). For example: ```text Input: X = [[1.0, 2.0, 3.0], [0.5, -1.0, 4.0]] Output: [[1.0, 2.0, 3.0, 2.0, 3.0, 6.0], [0.5, -1.0, 4.0, -0.5, 2.0, -4.0]] ``` The first three columns are the inputs unchanged. In the second row, `x0*x1 = 0.5 * -1.0 = -0.5`, `x0*x2 = 0.5 * 4.0 = 2.0` and `x1*x2 = -1.0 * 4.0 = -4.0`. ```hint Check the layout Work out which degree-2 terms remain once self-powers are removed, then confirm the order sklearn emits them in instead of assuming it. ``` ### Constraints & Assumptions - `X` is a dense 2-D float array with exactly 3 columns, so no input validation is needed. - `n` can be any positive number of rows. - The return value is a dense float ndarray. ### What a Strong Answer Covers - The correct `PolynomialFeatures` flags and what each one removes from the full degree-2 expansion. - The output shape `(n, 6)` and the exact column order, with a way to confirm it such as `get_feature_names_out()`. - Returning a float ndarray. - When an interaction-only design is the right choice, for example binary features where squaring adds nothing. ### Follow-up Questions - How would you generalize this to any number of input features? - Why might you scale features before fitting a model on this design? - Once `x0*x1` is in a linear model, how do you interpret the coefficient on `x0`?

Overview: Expand a three-feature matrix into main effects and pairwise interaction terms with scikit-learn PolynomialFeatures, without squared terms or a bias column. It tests whether you know the flags, the output column order, and how interaction terms change a linear model.

General logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement def interaction_only_design(X: np.ndarray) -> np.ndarray. Given a 2-D float ndarray X of shape (n, 3), transform it with sklearn.preprocessing.PolynomialFeatures(degree=2, interaction_only=True, include_bias=False) and return the float ndarray.

The result must have shape (n, 6) with columns in the order [x0, x1, x2, x0*x1, x0*x2, x1*x2]. It excludes the bias column and all squared terms (no x0^2).

For example:

Input:  X = [[1.0, 2.0, 3.0],
             [0.5, -1.0, 4.0]]
Output: [[1.0, 2.0, 3.0, 2.0, 3.0, 6.0],
         [0.5, -1.0, 4.0, -0.5, 2.0, -4.0]]

The first three columns are the inputs unchanged. In the second row, x0*x1 = 0.5 * -1.0 = -0.5, x0*x2 = 0.5 * 4.0 = 2.0 and x1*x2 = -1.0 * 4.0 = -4.0.

Constraints & Assumptions

  • X is a dense 2-D float array with exactly 3 columns, so no input validation is needed.
  • n can be any positive number of rows.
  • The return value is a dense float ndarray.

What a Strong Answer Covers Guidance

  • The correct PolynomialFeatures flags and what each one removes from the full degree-2 expansion.
  • The output shape (n, 6) and the exact column order, with a way to confirm it such as get_feature_names_out() .
  • Returning a float ndarray.
  • When an interaction-only design is the right choice, for example binary features where squaring adds nothing.

Follow-up Questions Guidance

  • How would you generalize this to any number of input features?
  • Why might you scale features before fitting a model on this design?
  • Once x0*x1 is in a linear model, how do you interpret the coefficient on x0 ?