Linear regression — residuals
Background
A residual is how wrong a single prediction is. The lesson defines it as true minus predicted:
A positive residual means the model under-predicted that point; a negative residual means it over-predicted. Residuals are the raw material the loss (SSR) and the gradients are built from.
Problem statement
Implement residuals(y_true, y_pred) returning the elementwise difference y_true - y_pred.
Input
y_true— array-like of actual target values.y_pred— array-like of predicted values, the same length.
Output
Returns an np.ndarray where element is .
Examples
Example 1
Input: y_true = [5], y_pred = [8]
Output: [-3.]
Explanation: the lesson's quiz — predict but the truth is , so the residual is (the model over-predicted).
Example 2
Input: y_true = [149, 161, 449, 599], y_pred = [140, 170, 400, 550]
Output: [9. -9. 49. 49.]
Explanation: the lesson's test table, residual = true − predicted for each row.
Constraints
- Convert both inputs to arrays so the subtraction is elementwise.
- Keep the order:
y_true - y_pred, not the reverse.
Notes
- Squaring these residuals and summing gives the SSR; correlating them with the inputs gives the gradients. Everything downstream starts here.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference quiz: true 5, predicted 8
- •Example lesson test table (true - predicted)
- •Sample perfect predictions give all zeros