Linear Prediction Practice Problem
This data science coding problem helps you practice Linear Regression, linear prediction, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Linear Regression.
- Problem ID: 4
- Problem key: 4-linear-prediction
- URL: https://datacrack.app/solve/4-linear-prediction
- Difficulty: easy
- Topic: Linear Regression
- Module: Introduction to Machine Learning
Problem Statement
# 🧩 Linear Prediction
---
### 🎯 Goal
In **Linear Regression**, predictions are made using the equation:
$$
\hat{y} = Xw + b
$$
This represents how the model combines input features with learned weights to generate predictions.
---
### 🔍 Explanation of Symbols
| Symbol | Meaning | Shape / Type |
|:-------:|:--------|:-------------|
| **$X$** | Input features matrix — each row = one data point, each column = a feature | $(n, m)$ |
| **$w$** | Weight vector — one weight per feature | $(m,)$ |
| **$b$** | Bias term — added to each prediction | scalar |
| **$\hat{y}$** | Predicted values (vector) | $(n,)$ |
- **$n$** = number of samples (rows in $X$)
- **$m$** = number of features (columns in $X$)
---
### 📥 Input
- `X`: NumPy array of shape (n, m) → input data
- `w`: NumPy array of shape (m,) → weights
- `b`: float → bias term
### 📤 Output
- NumPy array of shape (n,) → predicted values $\hat{y}$
---
### 💻 Task
Implement a Python function `predict(X, w, b)` that computes:
$$
\hat{y} = Xw + b
$$
using **NumPy matrix operations**.
---
### 🧩 Starter Code
```python
import numpy as np
import random
random.seed(420)
def predict(X, w, b):
"""
Compute predictions for a linear regression model.
Args:
X (np.ndarray): Feature matrix of shape (n, m)
w (np.ndarray): Weight vector of shape (m,)
b (float): Bias term
Returns:
np.ndarray: Predicted values of shape (n,)
"""
# 🧠 TODO: Implement the equation y_hat = Xw + b
pass
```
Starter Code
import numpy as np
import random
random.seed(420)
def predict(X, w, b):
"""
Compute predictions for a linear regression model.
Args:
X (np.ndarray): Feature matrix of shape (n, m)
w (np.ndarray): Weight vector of shape (m,)
b (float): Bias term
Returns:
np.ndarray: Predicted values of shape (n,)
"""
# 🧠 TODO: Implement the equation y_hat = Xw + b
pass