Quadratic Design Matrix With PolynomialFeatures

Quick Overview

Write a Python function that turns a 1-D NumPy array into a quadratic design matrix with scikit-learn PolynomialFeatures, returning columns x and x squared. It tests input reshaping for scikit-learn, the include_bias option, output column order, and how polynomial features feed a linear regression.

Quadratic Design Matrix With PolynomialFeatures

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def quadratic_design(x: np.ndarray) -> np.ndarray`. Given a 1-D float ndarray `x` of length n, use `sklearn.preprocessing.PolynomialFeatures` to build and return the quadratic design matrix. Requirements: - Reshape input to `(n, 1)` before transforming. - Construct with `degree=2` and `include_bias=False`. - Return a float ndarray of shape `(n, 2)`: column 0 is `x`, column 1 is `x**2`. - Return the ndarray only: no model fitting, no feature names. Example: ```python x = np.array([1.0, 2.0, 3.0]) quadratic_design(x) # array([[1., 1.], # [2., 4.], # [3., 9.]]) ``` ```hint Input shape scikit-learn transformers read their input as rows of samples and columns of features. Think about which of those a 1-D array of length n should become here. ``` ### What a Strong Answer Covers - Reshaping the 1-D input into a single-feature 2-D array, and why scikit-learn needs it. - What `include_bias=False` removes, and why the intercept is usually left to the model. - The column order of the output when there is one input feature. - Returning the transformed ndarray with shape `(n, 2)` and a float dtype, without fitting a model. ### Follow-up Questions - Why is a bias column redundant when this matrix feeds `LinearRegression` with its default `fit_intercept=True`? - When `x` is all positive, `x` and `x**2` are strongly correlated. How does that affect the fitted coefficients, and what would you change? - What columns does `PolynomialFeatures(degree=2, include_bias=False)` produce for two input features, and in what order? - In a train and test workflow, should the transformer be fit on the test set?

Overview: Write a Python function that turns a 1-D NumPy array into a quadratic design matrix with scikit-learn PolynomialFeatures, returning columns x and x squared. It tests input reshaping for scikit-learn, the include_bias option, output column order, and how polynomial features feed a linear regression.

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

Implement def quadratic_design(x: np.ndarray) -> np.ndarray. Given a 1-D float ndarray x of length n, use sklearn.preprocessing.PolynomialFeatures to build and return the quadratic design matrix.

Requirements:

  • Reshape input to (n, 1) before transforming.
  • Construct with degree=2 and include_bias=False .
  • Return a float ndarray of shape (n, 2) : column 0 is x , column 1 is x**2 .
  • Return the ndarray only: no model fitting, no feature names.

Example:

x = np.array([1.0, 2.0, 3.0])
quadratic_design(x)
# array([[1., 1.],
#        [2., 4.],
#        [3., 9.]])

What a Strong Answer Covers Guidance

  • Reshaping the 1-D input into a single-feature 2-D array, and why scikit-learn needs it.
  • What include_bias=False removes, and why the intercept is usually left to the model.
  • The column order of the output when there is one input feature.
  • Returning the transformed ndarray with shape (n, 2) and a float dtype, without fitting a model.

Follow-up Questions Guidance

  • Why is a bias column redundant when this matrix feeds LinearRegression with its default fit_intercept=True ?
  • When x is all positive, x and x**2 are strongly correlated. How does that affect the fitted coefficients, and what would you change?
  • What columns does PolynomialFeatures(degree=2, include_bias=False) produce for two input features, and in what order?
  • In a train and test workflow, should the transformer be fit on the test set?