Implement an OLS regression report: in-sample MSE, RMSE, MAE and R-squared
Quick Overview
Implement a function that fits ordinary least squares with an intercept on a small two-feature dataset and returns in-sample MSE, RMSE, MAE and R-squared. It tests computing regression metrics from residuals, using the centered total sum of squares, and knowing what in-sample fit can and cannot tell you.
Implement an OLS regression report: in-sample MSE, RMSE, MAE and R-squared
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `regression_report(X, y)` that fits OLS with an intercept on `X` (shape `(25, 2)`) and `y` (shape `(25,)`), predicts on the same `X`, and returns a dict of four in-sample float metrics.
- Return a dict with exactly the keys `'mse'`, `'rmse'`, `'mae'` and `'r2'`, each a float.
- All means divide by `n`, the sample count.
- Metrics are computed on the training data. There is no train/test split.
The metrics are defined as:
```python
mse = mean((y - yhat) ** 2)
rmse = sqrt(mse)
mae = mean(abs(y - yhat))
r2 = 1 - RSS / TSS # TSS centered on y.mean()
```
You may use `sklearn.linear_model.LinearRegression` (it fits an intercept by default) or solve the normal equations directly.
```hint Work from the residuals
Every metric is a mean or a sum built from the residual vector `y - yhat`, plus the spread of `y` around its own mean for r2. Compute the residuals once and cast each result with `float()`.
```
### Constraints & Assumptions
- `X` has shape `(25, 2)` and `y` has shape `(25,)`, so n = 25 and the model has three parameters: an intercept and two slopes.
- The fit and all four metrics use the same 25 rows.
### What a Strong Answer Covers
- Fitting OLS with an intercept, either with `LinearRegression` or with the normal equations on `X` plus a column of ones.
- Computing all four metrics from one residual vector, with every mean divided by `n`.
- Using the centered total sum of squares in r2 and explaining what the intercept guarantees about it.
- Returning plain floats under exactly the four required keys.
- Naming the degenerate case where `y` is constant and the total sum of squares is zero.
### Follow-up Questions
- Why can in-sample `r2` be misleading?
- How would adjusted R-squared change the reported fit for this model, with 25 rows and two predictors?
- When would you report MAE instead of RMSE, and what does a large gap between the two tell you?
- What happens to `r2` under this definition if you fit the model without an intercept?
Overview: Implement a function that fits ordinary least squares with an intercept on a small two-feature dataset and returns in-sample MSE, RMSE, MAE and R-squared. It tests computing regression metrics from residuals, using the centered total sum of squares, and knowing what in-sample fit can and cannot tell you.
Implement an OLS regression report: in-sample MSE, RMSE, MAE and R-squared
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0
Implement regression_report(X, y) that fits OLS with an intercept on X (shape (25, 2)) and y (shape (25,)), predicts on the same X, and returns a dict of four in-sample float metrics.
Return a dict with exactly the keys
'mse'
,
'rmse'
,
'mae'
and
'r2'
, each a float.
All means divide by
n
, the sample count.
Metrics are computed on the training data. There is no train/test split.