Fit OLS with a two-way interaction term and return the interaction coefficient

Quick Overview

Fit an ordinary least squares model with two main effects and their product term, then return the interaction coefficient as a float. It tests building a design matrix by hand, keeping the intercept and main effects, and reading the right coefficient from NumPy, statsmodels or scikit-learn.

Fit OLS with a two-way interaction term and return the interaction coefficient

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `interaction_coefficient(df: pd.DataFrame) -> float`. `df` has float columns `x1`, `x2` and `y`. Fit the OLS model $$ y = b_0 + b_1 x_1 + b_2 x_2 + b_3 (x_1 x_2) $$ and return `b3`. The function must: - Include an intercept and both main effects, `x1` and `x2`. - Build the interaction column explicitly as `x1 * x2`. Formula syntax is fine only if it yields the same term. - Return only the raw coefficient `b3` as a Python `float`. You may use `statsmodels` or `sklearn.linear_model.LinearRegression`. ```hint Intercept Check whether your fitting routine adds an intercept on its own. `numpy.linalg.lstsq` and `statsmodels.OLS` on plain arrays do not, while `LinearRegression` does. ``` ### What a Strong Answer Covers - Builds a design matrix with an intercept, both main effects and the product column. - Fits OLS and picks the coefficient that belongs to the product term. - Converts the result to a Python `float`. - Explains what `b3` means: how the slope of `y` on one predictor changes per unit of the other. ### Follow-up Questions - Why should the main effects stay in the model when you add `x1*x2`? - How does centering `x1` and `x2` affect the coefficients? - How would you test whether the interaction is statistically significant?

Overview: Fit an ordinary least squares model with two main effects and their product term, then return the interaction coefficient as a float. It tests building a design matrix by hand, keeping the intercept and main effects, and reading the right coefficient from NumPy, statsmodels or scikit-learn.

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

Implement interaction_coefficient(df: pd.DataFrame) -> float. df has float columns x1, x2 and y. Fit the OLS model

y=b0+b1x1+b2x2+b3(x1x2)y = b_0 + b_1 x_1 + b_2 x_2 + b_3 (x_1 x_2)

and return b3.

The function must:

  • Include an intercept and both main effects, x1 and x2 .
  • Build the interaction column explicitly as x1 * x2 . Formula syntax is fine only if it yields the same term.
  • Return only the raw coefficient b3 as a Python float .

You may use statsmodels or sklearn.linear_model.LinearRegression.

What a Strong Answer Covers Guidance

  • Builds a design matrix with an intercept, both main effects and the product column.
  • Fits OLS and picks the coefficient that belongs to the product term.
  • Converts the result to a Python float .
  • Explains what b3 means: how the slope of y on one predictor changes per unit of the other.

Follow-up Questions Guidance

  • Why should the main effects stay in the model when you add x1*x2 ?
  • How does centering x1 and x2 affect the coefficients?
  • How would you test whether the interaction is statistically significant?