Multi-Target Linear Regression in scikit-learn: Return the (n_targets, n_features) Coefficient Matrix

Quick Overview

Fit one scikit-learn LinearRegression on a two-column target and return the slope matrix with one row per target and one column per feature. It tests the coef_ layout for multi-output models, where the intercepts live, and why least squares fits each target independently.

Multi-Target Linear Regression in scikit-learn: Return the (n_targets, n_features) Coefficient Matrix

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def multi_target_coefficients(X: np.ndarray, Y: np.ndarray) -> np.ndarray`. Fit a single `sklearn.linear_model.LinearRegression` (with intercept) on `X` of shape `(40, 3)` and multi-output `Y` of shape `(40, 2)` in one `fit` call, and return the coefficient matrix. Requirements: - Return `coef_` as an `np.ndarray` of shape `(2, 3)`, which is `(n_targets, n_features)`. - Row `i` holds the slopes for target column `i`, in `X` column order. - Do not include intercepts (that would give `(2, 4)`). The intercepts (`intercept_`, shape `(2,)`) are out of scope for the return value. Example: ```python rng = np.random.default_rng(0) X = rng.normal(size=(40, 3)) B = np.array([[1.0, -2.0, 0.5], [0.0, 3.0, 1.0]]) Y = X @ B.T + np.array([4.0, -1.0]) # noise-free, intercepts 4 and -1 coef = multi_target_coefficients(X, Y) # coef.shape == (2, 3) # coef is close to B: row 0 is target 0, row 1 is target 1 ``` ```hint Mind the layout Check how scikit-learn lays out `coef_` for a 2-D target before you transpose, reshape or stack anything. ``` ### Constraints & Assumptions - `X` has shape `(40, 3)` and `Y` has shape `(40, 2)`. - The model includes an intercept and is fit in a single `fit` call. ### What a Strong Answer Covers - One `LinearRegression` fit on the full 2-D `Y`, with an intercept. - The layout of `coef_`: which axis is targets, which is features, and how rows map to columns of `Y`. - Keeping the intercepts out of the returned matrix, and where they are stored. - Why the joint fit gives the same coefficients as separate per-column fits. ### Follow-up Questions - How would you also recover the per-target intercepts? - Why is a joint multi-output fit equivalent to independent per-column fits here? - What shape does `coef_` have if `Y` is 1-D, or 2-D with a single column? - Which methods or data conditions would make a joint fit differ from separate fits?

Overview: Fit one scikit-learn LinearRegression on a two-column target and return the slope matrix with one row per target and one column per feature. It tests the coef_ layout for multi-output models, where the intercepts live, and why least squares fits each target independently.

Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement def multi_target_coefficients(X: np.ndarray, Y: np.ndarray) -> np.ndarray. Fit a single sklearn.linear_model.LinearRegression (with intercept) on X of shape (40, 3) and multi-output Y of shape (40, 2) in one fit call, and return the coefficient matrix.

Requirements:

  • Return coef_ as an np.ndarray of shape (2, 3) , which is (n_targets, n_features) .
  • Row i holds the slopes for target column i , in X column order.
  • Do not include intercepts (that would give (2, 4) ). The intercepts ( intercept_ , shape (2,) ) are out of scope for the return value.

Example:

rng = np.random.default_rng(0)
X = rng.normal(size=(40, 3))
B = np.array([[1.0, -2.0, 0.5],
              [0.0,  3.0, 1.0]])
Y = X @ B.T + np.array([4.0, -1.0])   # noise-free, intercepts 4 and -1
coef = multi_target_coefficients(X, Y)
# coef.shape == (2, 3)
# coef is close to B: row 0 is target 0, row 1 is target 1

Constraints & Assumptions

  • X has shape (40, 3) and Y has shape (40, 2) .
  • The model includes an intercept and is fit in a single fit call.

What a Strong Answer Covers Guidance

  • One LinearRegression fit on the full 2-D Y , with an intercept.
  • The layout of coef_ : which axis is targets, which is features, and how rows map to columns of Y .
  • Keeping the intercepts out of the returned matrix, and where they are stored.
  • Why the joint fit gives the same coefficients as separate per-column fits.

Follow-up Questions Guidance

  • How would you also recover the per-target intercepts?
  • Why is a joint multi-output fit equivalent to independent per-column fits here?
  • What shape does coef_ have if Y is 1-D, or 2-D with a single column?
  • Which methods or data conditions would make a joint fit differ from separate fits?