Fit OLS on a Named Feature Subset and Return Labeled Coefficients

Quick Overview

Write a pandas function that fits ordinary least squares with an intercept on a caller-chosen subset of columns and returns the coefficients as a labeled float Series in the caller's order. Tests column selection by name, intercept handling and control over the output labels when using NumPy or statsmodels.

Fit OLS on a Named Feature Subset and Return Labeled Coefficients

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def fit_named_subset(df: pd.DataFrame, target: str, features: list[str]) -> pd.Series`. Fit OLS with an intercept predicting `df[target]` from only the columns in `features`, ignoring all others, and return the coefficients. Requirements: - Return a `pd.Series` of dtype `float64`: the intercept plus one coefficient per feature. - Index: `'Intercept'` (exact label) first, then the features in the order given by `features`. The frame's column order does not matter. - `statsmodels` (for example `sm.add_constant` with `sm.OLS`) or any equivalent fit is allowed. Example: ```python s = fit_named_subset(df, target='y', features=['c', 'a']) # s.index -> Index(['Intercept', 'c', 'a']) # s.dtype -> float64 # s['c'] is the coefficient on column 'c', s['a'] on column 'a' ``` ```hint Control the labels yourself Check what name your fitting library gives the intercept and what order its output comes back in, then decide how to guarantee the required index. ``` ### Constraints & Assumptions - The grader uses a frame of 40 rows with float columns `['a', 'b', 'c', 'd', 'y']`. - It calls the function with `target='y'` and `features=['c', 'a']`, so `b` and `d` must stay out of the model. ### What a Strong Answer Covers - Selects the feature columns by name in the caller's order and leaves every other column out of the fit. - Adds an intercept and fits OLS on exactly those columns. - Returns a float64 Series indexed `'Intercept'` first, then the features in the given order. - Keeps the labels independent of how the library names or orders its output. - Says how bad input (a missing feature name, the target listed as a feature) should be handled. ### Follow-up Questions - How would you make the function fail clearly when a feature name is missing from `df`? - If two features are highly collinear, how does that affect the returned coefficients? - What happens if the caller includes the target column in `features`?

Overview: Write a pandas function that fits ordinary least squares with an intercept on a caller-chosen subset of columns and returns the coefficients as a labeled float Series in the caller's order. Tests column selection by name, intercept handling and control over the output labels when using NumPy or statsmodels.

Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement def fit_named_subset(df: pd.DataFrame, target: str, features: list[str]) -> pd.Series. Fit OLS with an intercept predicting df[target] from only the columns in features, ignoring all others, and return the coefficients.

Requirements:

  • Return a pd.Series of dtype float64 : the intercept plus one coefficient per feature.
  • Index: 'Intercept' (exact label) first, then the features in the order given by features . The frame's column order does not matter.
  • statsmodels (for example sm.add_constant with sm.OLS ) or any equivalent fit is allowed.

Example:

s = fit_named_subset(df, target='y', features=['c', 'a'])
# s.index -> Index(['Intercept', 'c', 'a'])
# s.dtype -> float64
# s['c'] is the coefficient on column 'c', s['a'] on column 'a'

Constraints & Assumptions

  • The grader uses a frame of 40 rows with float columns ['a', 'b', 'c', 'd', 'y'] .
  • It calls the function with target='y' and features=['c', 'a'] , so b and d must stay out of the model.

What a Strong Answer Covers Guidance

  • Selects the feature columns by name in the caller's order and leaves every other column out of the fit.
  • Adds an intercept and fits OLS on exactly those columns.
  • Returns a float64 Series indexed 'Intercept' first, then the features in the given order.
  • Keeps the labels independent of how the library names or orders its output.
  • Says how bad input (a missing feature name, the target listed as a feature) should be handled.

Follow-up Questions Guidance

  • How would you make the function fail clearly when a feature name is missing from df ?
  • If two features are highly collinear, how does that affect the returned coefficients?
  • What happens if the caller includes the target column in features ?