Quadratic Fit With patsy I() in the statsmodels Formula API

Quick Overview

Fit a quadratic regression of y on x with the statsmodels formula API and return the fitted parameters with patsy's exact term labels. It tests why I() is needed for arithmetic inside a formula, how patsy names and orders terms, and how to handle higher-order polynomial terms.

Quadratic Fit With patsy I() in the statsmodels Formula API

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def squared_term_params(df: pd.DataFrame) -> pd.Series` for a DataFrame with float columns `x` and `y`. Fit `'y ~ x + I(x**2)'` with the statsmodels formula API (`I()` makes `**` arithmetic) and return the fitted `.params` unchanged. Requirements: - Index exactly `['Intercept', 'x', 'I(x ** 2)']` in that order (no rename or reorder). - Values are `float64`. Example, for a `df` whose points follow `y = 1 + 2x + 0.5x^2` plus a little noise: ```python params = squared_term_params(df) # Intercept 1.02 # x 2.01 # I(x ** 2) 0.49 # dtype: float64 ``` ```hint Term labels Compare the label patsy gives the squared term with the string you typed in the formula, character by character. ``` ### What a Strong Answer Covers - Fitting with `smf.ols` and a formula so patsy builds the intercept, linear and squared columns. - Why `I()` is needed for `x**2` to mean exponentiation inside a formula. - The exact index labels and order that patsy produces, and why returning `.params` untouched meets them. - What the fit does with missing values or too few distinct `x` values. ### Follow-up Questions - Why does patsy need `I()` around `x**2`? - What model does `'y ~ x**2'` fit if you leave out `I()`? - How would you add higher-order terms cleanly? - How would you test whether the quadratic term is needed at all?

Overview: Fit a quadratic regression of y on x with the statsmodels formula API and return the fitted parameters with patsy's exact term labels. It tests why I() is needed for arithmetic inside a formula, how patsy names and orders terms, and how to handle higher-order polynomial terms.

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

Implement def squared_term_params(df: pd.DataFrame) -> pd.Series for a DataFrame with float columns x and y. Fit 'y ~ x + I(x**2)' with the statsmodels formula API (I() makes ** arithmetic) and return the fitted .params unchanged.

Requirements:

  • Index exactly ['Intercept', 'x', 'I(x ** 2)'] in that order (no rename or reorder).
  • Values are float64 .

Example, for a df whose points follow y = 1 + 2x + 0.5x^2 plus a little noise:

params = squared_term_params(df)
# Intercept      1.02
# x              2.01
# I(x ** 2)      0.49
# dtype: float64

What a Strong Answer Covers Guidance

  • Fitting with smf.ols and a formula so patsy builds the intercept, linear and squared columns.
  • Why I() is needed for x**2 to mean exponentiation inside a formula.
  • The exact index labels and order that patsy produces, and why returning .params untouched meets them.
  • What the fit does with missing values or too few distinct x values.

Follow-up Questions Guidance

  • Why does patsy need I() around x**2 ?
  • What model does 'y ~ x**2' fit if you leave out I() ?
  • How would you add higher-order terms cleanly?
  • How would you test whether the quadratic term is needed at all?