#298Linear regression — residualsEasy

Linear regression — residuals

Background

A residual is how wrong a single prediction is. The lesson defines it as true minus predicted:

residuali=yiy^i\text{residual}_i = y_i - \hat{y}_i

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 ii is ytrue,iypred,iy_{\text{true},i} - y_{\text{pred},i}.

Examples

Example 1

Input:  y_true = [5], y_pred = [8]
Output: [-3.]

Explanation: the lesson's quiz — predict y^=8\hat{y}=8 but the truth is 55, so the residual is 58=35-8=-3 (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.
Python
Loading...

▶ 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