Statsmodels OLS: Predict on New Exog With a Matching Intercept Column

Quick Overview

Fit a statsmodels OLS regression with an intercept and return predictions for a set of new rows. It tests building matching design matrices at fit and predict time, how coefficients line up with columns by position, and the add_constant pitfall on small or single-row inputs.

Statsmodels OLS: Predict on New Exog With a Matching Intercept Column

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `predict_with_statsmodels(X, y, X_new)`: fit a linear regression of `y` on `X` with `sm.OLS` including an intercept, then return predictions for `X_new`. Requirements: - Fit `sm.OLS(y, sm.add_constant(X))`. `OLS` does not add a constant automatically. - Add a matching constant column to `X_new`. The predict-time design must have the same columns in the same order as at fit time (4 columns in the example below). - Return a 1-D `np.ndarray` in `X_new` row order, of shape `(5,)` in the example. Example: ```python X.shape # (40, 3), no ones column y.shape # (40,) X_new.shape # (5, 3), same 3 columns, no ones column out = predict_with_statsmodels(X, y, X_new) out.shape # (5,) ``` ```hint Read the defaults Check the default value of `has_constant` in `sm.add_constant`, and think about which inputs to `X_new` could trip it even when the training data does not. ``` ### Constraints & Assumptions - `X` has shape `(n, p)` with no ones column, `(40, 3)` in the example, and `y` has shape `(n,)`. - `X_new` has shape `(m, p)` with the same feature columns in the same order and no ones column, `(5, 3)` in the example. - The model is fit with statsmodels `sm.OLS`. ### What a Strong Answer Covers - Adding the constant to both the training and the new design matrices, since `sm.OLS` never adds one. - Matching the column count and order between fit time and predict time, and why the coefficients depend on column position. - The `has_constant='skip'` default of `add_constant` and the inputs that trigger it, including a single new row. - Returning a 1-D array in `X_new` row order. ### Follow-up Questions - Why can `add_constant` silently drop the constant column? - How would you get prediction intervals instead of point predictions? - How does the formula API (`smf.ols`) change the way you predict on new data?

Overview: Fit a statsmodels OLS regression with an intercept and return predictions for a set of new rows. It tests building matching design matrices at fit and predict time, how coefficients line up with columns by position, and the add_constant pitfall on small or single-row inputs.

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

Implement predict_with_statsmodels(X, y, X_new): fit a linear regression of y on X with sm.OLS including an intercept, then return predictions for X_new.

Requirements:

  • Fit sm.OLS(y, sm.add_constant(X)) . OLS does not add a constant automatically.
  • Add a matching constant column to X_new . The predict-time design must have the same columns in the same order as at fit time (4 columns in the example below).
  • Return a 1-D np.ndarray in X_new row order, of shape (5,) in the example.

Example:

X.shape      # (40, 3), no ones column
y.shape      # (40,)
X_new.shape  # (5, 3), same 3 columns, no ones column

out = predict_with_statsmodels(X, y, X_new)
out.shape    # (5,)

Constraints & Assumptions

  • X has shape (n, p) with no ones column, (40, 3) in the example, and y has shape (n,) .
  • X_new has shape (m, p) with the same feature columns in the same order and no ones column, (5, 3) in the example.
  • The model is fit with statsmodels sm.OLS .

What a Strong Answer Covers Guidance

  • Adding the constant to both the training and the new design matrices, since sm.OLS never adds one.
  • Matching the column count and order between fit time and predict time, and why the coefficients depend on column position.
  • The has_constant='skip' default of add_constant and the inputs that trigger it, including a single new row.
  • Returning a 1-D array in X_new row order.

Follow-up Questions Guidance

  • Why can add_constant silently drop the constant column?
  • How would you get prediction intervals instead of point predictions?
  • How does the formula API ( smf.ols ) change the way you predict on new data?