#297Linear regression — predict with a lineEasy

Linear regression — predict with a line

Background

Linear regression models a continuous output as a straight line. With a single input feature the model is

y^=wx+b\hat{y} = w x + b

where ww (the weight / slope, written mm in the lesson) tilts the line and bb (the bias / intercept, written cc) shifts it up or down. Predicting is just evaluating this line at each input — the very first thing every later step (residuals, SSR, gradients) is built on.

Problem statement

Implement predict(x, w, b) that returns the line's prediction y^=wx+b\hat{y} = w x + b for every value in x.

Input

  • x — array-like of input features (any length).
  • wfloat, the slope.
  • bfloat, the intercept.

Output

Returns an np.ndarray of the same length as x, where element ii is wxi+bw x_i + b.

Examples

Example 1

Input:  x = [2, 4, 5], w = 3, b = -4
Output: [2. 8. 11.]

Explanation: the lesson's line y=3x4y = 3x - 4 gives 324=23\cdot2-4=2, 344=83\cdot4-4=8, 354=113\cdot5-4=11.

Example 2

Input:  x = [51], w = 1, b = 1
Output: [52.]

Explanation: the lesson's starting guess m=1,c=1m=1, c=1 predicts 5252 at x=51x=51 — far from the true 149149, which is why training is needed.

Constraints

  • Convert x to a NumPy array so the arithmetic is elementwise.
  • Do not loop in Python — one vectorised expression is enough.

Notes

  • This is the model's forward pass. Every other quantity in the lesson — residuals, SSR, gradients — starts from this prediction.
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 line y = 3x - 4
  • Example starting guess m=1, c=1 predicts 52 at x=51
  • Sample negative slope y = -2x + 10