BasicsLinear RegressionNumPyGoogleMetaAmazon
Problem
Given feature matrix X of shape (N, D), weight vector w of shape (D,), scalar bias b, and true targets y of shape (N,), implement linear_predict_and_residuals(X, w, b, y) returning (y_hat, residuals) where y_hat = X @ w + b and residuals = y - y_hat.
Why It Matters: Linear predictions y^=Xw+b and residuals r=y−y^ are the core building blocks of OLS regression, residual diagnostics, and gradient boosting.
Sample Inputs & Expected Outputs
Example 1 — 2x2 feature matrix
>>> linear_predict_and_residuals([[1, 2], [3, 4]], [2.0, -1.0], 1.0, [2.0, 5.0])
(array([1., 3.]), array([1., 2.]))
y_hat = [12 + 2*(-1) + 1, 32 + 4(-1) + 1] = [1, 3]; residuals = y - y_hat = [1, 2].*
Example 2 — Exact fit (zero error)
>>> linear_predict_and_residuals([[2], [5]], [3.0], -1.0, [5.0, 14.0])
(array([5., 14.]), array([0., 0.]))
Predictions match y exactly, so residuals are all 0.
Constraints & Edge Cases
- Return
(y_hat, residuals)as 1D float NumPy arrays.
Constraints & Evaluation Criteria
- Return `(y_hat, residuals)` as 1D float NumPy arrays.
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
X=[[1,2],[3,4]], w=[2,-1], b=1
Expected Output / Criteria
y_hat=[1,3], res=[1,2]