Return linear regression slope coefficients as a pandas Series indexed by feature name

Quick Overview

Fit a linear regression with an intercept in Python and return only the slope coefficients as a float64 pandas Series indexed by feature name. It tests separating features from the target, keeping the original column order, and keeping the intercept out of the output.

Return linear regression slope coefficients as a pandas Series indexed by feature name

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `coefficient_series(df, target)` that fits a linear regression with intercept, predicting column `target` from all other columns of `df`. Return the slope coefficients as a Series indexed by feature name. The returned value must meet these requirements: - It is a `pd.Series` of dtype `float64`. - Its index is the feature names in original column order (`['a', 'b', 'c']`), and each value is that feature's coefficient. - It has no intercept entry: length 3, with no `'const'` or `'Intercept'` label. - It has no `name` set. ```hint Where the intercept ends up Check which attribute of the fitted model holds the intercept and which holds the slopes, and whether the library you pick adds the intercept column for you. ``` ### Constraints & Assumptions - The input `df` has columns `['a', 'b', 'c', 'y']` with 40 rows of floats, and `target='y'`. - Any ordinary least squares fit is accepted, for example scikit-learn's `LinearRegression` with its default `fit_intercept=True`. ### What a Strong Answer Covers - Selecting the feature columns by dropping `target` while keeping the original column order. - Fitting OLS with an intercept and keeping the intercept out of the returned Series. - Building the Series with the feature names as its index, dtype `float64`, and no name. - Handling or at least naming edge cases such as missing values, non-numeric columns and perfectly collinear features. ### Follow-up Questions - How would you also return the intercept, and how would you get a standard error and p-value for each coefficient? - What happens to the coefficients when two features are perfectly collinear, and how would you detect it? - How would the slopes change if you fit without an intercept on data that is not centered?

Overview: Fit a linear regression with an intercept in Python and return only the slope coefficients as a float64 pandas Series indexed by feature name. It tests separating features from the target, keeping the original column order, and keeping the intercept out of the output.

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

Implement coefficient_series(df, target) that fits a linear regression with intercept, predicting column target from all other columns of df. Return the slope coefficients as a Series indexed by feature name.

The returned value must meet these requirements:

  • It is a pd.Series of dtype float64 .
  • Its index is the feature names in original column order ( ['a', 'b', 'c'] ), and each value is that feature's coefficient.
  • It has no intercept entry: length 3, with no 'const' or 'Intercept' label.
  • It has no name set.

Constraints & Assumptions

  • The input df has columns ['a', 'b', 'c', 'y'] with 40 rows of floats, and target='y' .
  • Any ordinary least squares fit is accepted, for example scikit-learn's LinearRegression with its default fit_intercept=True .

What a Strong Answer Covers Guidance

  • Selecting the feature columns by dropping target while keeping the original column order.
  • Fitting OLS with an intercept and keeping the intercept out of the returned Series.
  • Building the Series with the feature names as its index, dtype float64 , and no name.
  • Handling or at least naming edge cases such as missing values, non-numeric columns and perfectly collinear features.

Follow-up Questions Guidance

  • How would you also return the intercept, and how would you get a standard error and p-value for each coefficient?
  • What happens to the coefficients when two features are perfectly collinear, and how would you detect it?
  • How would the slopes change if you fit without an intercept on data that is not centered?