Return scikit-learn PolynomialFeatures output names as a Python list

Quick Overview

Write a function that fits a degree-2 scikit-learn PolynomialFeatures transformer on two input columns and returns its feature names as a plain Python list. It tests the fit-before-names rule, the order of generated terms, return types and how polynomial expansion grows the feature count.

Return scikit-learn PolynomialFeatures output names as a Python list

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def poly_feature_names(X: np.ndarray) -> list`. Given a 2-D float ndarray `X` of shape `(n, 2)`, fit `sklearn.preprocessing.PolynomialFeatures(degree=2, include_bias=False)` and return `get_feature_names_out()` as a plain Python list. - Keep the transformer's column order: main effects first, then the degree-2 terms in the order the transformer emits them (`x0^2`, `x0 x1`, `x1^2`). - Return a `list` of `str`, not an ndarray. ```python X = np.array([[1.0, 2.0], [3.0, 4.0]]) poly_feature_names(X) # ['x0', 'x1', 'x0^2', 'x0 x1', 'x1^2'] ``` ```hint Check the return type Look at what `get_feature_names_out()` actually returns, and what happens when that object is compared with `==` to a Python list. ``` ### Constraints & Assumptions - `X` is a 2-D float NumPy array with exactly 2 columns and at least one row. - The values in `X` do not affect the names; only the number of columns does. ### What a Strong Answer Covers - Fits the transformer with `degree=2` and `include_bias=False` before asking for names - Converts the array of names to a list of Python `str` - Explains the order of the generated terms and how each name maps to a column of `transform(X)` - Notes how the names change for a DataFrame input or a different number of columns ### Follow-up Questions - How many output features does `PolynomialFeatures` produce for d inputs at degree k, with and without the bias column? - What changes with `interaction_only=True`? - Why scale or center features before a polynomial expansion in a linear model, and what does the expansion do to multicollinearity? - How would you get readable names such as `age^2` instead of `x0^2`?

Overview: Write a function that fits a degree-2 scikit-learn PolynomialFeatures transformer on two input columns and returns its feature names as a plain Python list. It tests the fit-before-names rule, the order of generated terms, return types and how polynomial expansion grows the feature count.

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

Implement def poly_feature_names(X: np.ndarray) -> list. Given a 2-D float ndarray X of shape (n, 2), fit sklearn.preprocessing.PolynomialFeatures(degree=2, include_bias=False) and return get_feature_names_out() as a plain Python list.

  • Keep the transformer's column order: main effects first, then the degree-2 terms in the order the transformer emits them ( x0^2 , x0 x1 , x1^2 ).
  • Return a list of str , not an ndarray.
X = np.array([[1.0, 2.0], [3.0, 4.0]])
poly_feature_names(X)
# ['x0', 'x1', 'x0^2', 'x0 x1', 'x1^2']

Constraints & Assumptions

  • X is a 2-D float NumPy array with exactly 2 columns and at least one row.
  • The values in X do not affect the names; only the number of columns does.

What a Strong Answer Covers Guidance

  • Fits the transformer with degree=2 and include_bias=False before asking for names
  • Converts the array of names to a list of Python str
  • Explains the order of the generated terms and how each name maps to a column of transform(X)
  • Notes how the names change for a DataFrame input or a different number of columns

Follow-up Questions Guidance

  • How many output features does PolynomialFeatures produce for d inputs at degree k, with and without the bias column?
  • What changes with interaction_only=True ?
  • Why scale or center features before a polynomial expansion in a linear model, and what does the expansion do to multicollinearity?
  • How would you get readable names such as age^2 instead of x0^2 ?