Linear Prediction
~12 mincode completion
Implement linear_predict(X, weights, bias) that returns the prediction vector of shape (m,).
Examples
2x2 matrix with equal weights and bias
- Input
- linear_predict([[1, 2], [3, 4]], [0.5, 0.5], 1)
- Output
- [2.5, 4.5]
3 samples, integer weights, no bias
- Input
- linear_predict([[1, 0], [0, 1], [1, 1]], [2, 3], 0)
- Output
- [2, 3, 5]
Negative weights
- Input
- linear_predict([[2, 3]], [-1, 2], 5)
- Output
- [9]
Hints
Hint 1
Use a matrix product rather than nested loops, and check which operand transposes.
Hint 2
Do not forget to the bias. That step is easy to skip.
Requirements
X: Feature matrix of shape (m, n)weights: Weight vector of shape (n,)bias: Scalar interceptReturn Prediction vector of shape (m,).
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~12 min
••••••••••••••••
8 employers weight this skill
2 health and bio companies, 2 quant funds, 2 enterprise vendors, 1 AI product company, 1 frontier lab. Top match scores 87.
Python
import numpy as np
def linear_predict(X: np.ndarray, weights: np.ndarray, bias: float) -> np.ndarray:
"""
Compute linear predictions y_hat = X @ weights + bias.
Args:
X: Feature matrix of shape (m, n)
weights: Weight vector of shape (n,)
bias: Scalar intercept
Returns:
Prediction vector of shape (m,).
"""
# YOUR CODE HERE
pass