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
where (the weight / slope, written in the lesson) tilts the line and (the bias / intercept, written ) 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 for every value in x.
Input
x— array-like of input features (any length).w—float, the slope.b—float, the intercept.
Output
Returns an np.ndarray of the same length as x, where element is .
Examples
Example 1
Input: x = [2, 4, 5], w = 3, b = -4
Output: [2. 8. 11.]
Explanation: the lesson's line gives , , .
Example 2
Input: x = [51], w = 1, b = 1
Output: [52.]
Explanation: the lesson's starting guess predicts at — far from the true , which is why training is needed.
Constraints
- Convert
xto 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.
▶ 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