Predict from a DataFrame with linear regression while preserving its index

Quick Overview

Fit a linear regression with an intercept on a training DataFrame and return predictions for a second frame as a float Series on that frame's own index. It tests how model outputs lose row labels, safe column selection by name, and pandas alignment pitfalls.

Predict from a DataFrame with linear regression while preserving its index

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def predict_frame(train: pd.DataFrame, new: pd.DataFrame) -> pd.Series`. Fit a linear regression with intercept predicting `'y'` from `['a', 'b', 'c']` on `train`, then return predictions for `new` aligned to its non-default index. Use `sklearn.linear_model.LinearRegression` or a normal-equation fit. - Predict every row of `new` in its original row order. - Return a `pd.Series` of dtype `float64`. - The index must be `new.index` (for example `['r1', 'r2', 'r3', 'r4', 'r5']`), not a fresh `RangeIndex`. - Do not set a `name` on the Series. ```hint Where the labels come from `model.predict` returns a plain NumPy array with no row labels. Decide how the labels of `new` get onto the result, and check that the method you pick cannot reorder or blank out values. ``` ### Constraints & Assumptions - `train` has 40 rows indexed 0 to 39, with columns `a`, `b`, `c` and `y`. - `new` has 5 rows with the feature columns only, indexed by string labels such as `'r1'` to `'r5'`. ### What a Strong Answer Covers - Fits OLS with an intercept on `a`, `b` and `c` from `train` - Selects the feature columns of `new` by name, in the training order, before predicting - Builds the output on `new.index`, in row order, as float64 with no name - Explains why attaching the index positionally is correct and where label alignment can silently produce `NaN` ### Follow-up Questions - Why can passing a NumPy array to `predict` silently misalign results? - How would you guard against `new` having columns in a different order than training? - How would you handle missing feature values or an empty `new` frame?

Overview: Fit a linear regression with an intercept on a training DataFrame and return predictions for a second frame as a float Series on that frame's own index. It tests how model outputs lose row labels, safe column selection by name, and pandas alignment pitfalls.

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

Implement def predict_frame(train: pd.DataFrame, new: pd.DataFrame) -> pd.Series. Fit a linear regression with intercept predicting 'y' from ['a', 'b', 'c'] on train, then return predictions for new aligned to its non-default index. Use sklearn.linear_model.LinearRegression or a normal-equation fit.

  • Predict every row of new in its original row order.
  • Return a pd.Series of dtype float64 .
  • The index must be new.index (for example ['r1', 'r2', 'r3', 'r4', 'r5'] ), not a fresh RangeIndex .
  • Do not set a name on the Series.

Constraints & Assumptions

  • train has 40 rows indexed 0 to 39, with columns a , b , c and y .
  • new has 5 rows with the feature columns only, indexed by string labels such as 'r1' to 'r5' .

What a Strong Answer Covers Guidance

  • Fits OLS with an intercept on a , b and c from train
  • Selects the feature columns of new by name, in the training order, before predicting
  • Builds the output on new.index , in row order, as float64 with no name
  • Explains why attaching the index positionally is correct and where label alignment can silently produce NaN

Follow-up Questions Guidance

  • Why can passing a NumPy array to predict silently misalign results?
  • How would you guard against new having columns in a different order than training?
  • How would you handle missing feature values or an empty new frame?