Perceptron Learning Rule Practice Problem
This data science coding problem helps you practice Perceptron, perceptron learning rule, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Perceptron.
- Problem ID: 216
- Problem key: 216-perceptron-learning-rule
- URL: https://datacrack.app/solve/216-perceptron-learning-rule
- Difficulty: easy
- Topic: Perceptron
- Module: Introduction to Machine Learning
Problem Statement
# 🧩 Perceptron Learning Rule
---
### 🎯 Goal
Perform one Perceptron learning step.
In the previous problem, the Perceptron made a prediction using:
$$
z = w^Tx + b
$$
and:
$$
\hat{y} =
\begin{cases}
1, & z \ge 0 \\
-1, & z < 0
\end{cases}
$$
Now we ask: **what happens when the prediction is wrong?**
---
### 📖 Introduction
The Perceptron learns only when it makes a mistake.
If the prediction is correct, the weights and bias stay the same.
If the prediction is wrong, the update is:
$$
w \leftarrow w + \eta y_i x_i
$$
$$
b \leftarrow b + \eta y_i
$$
This is one update for one training sample.
Why does $x_i$ appear in the weight update? Because each feature changes in proportion to its value in the sample that was misclassified.
The sign of $y_i$ controls the direction:
- If $y_i=1$, the update moves the model toward predicting `1` for this sample.
- If $y_i=-1$, the update moves the model toward predicting `-1` for this sample.
The learning rate $\eta$ controls how large the update is.
---
### 🔍 Explanation of Symbols
| Symbol | Meaning |
|:--:|:--|
| $x_i$ | one training sample |
| $y_i$ | true label, either $-1$ or $1$ |
| $\hat{y}_i$ | predicted label |
| $w$ | current weights |
| $b$ | current bias |
| $\eta$ | learning rate, the size of the update step |
---
### 📥 Input
- `sample`: one feature vector.
- `true_label`: either `-1` or `1`.
- `weights`: current model weights.
- `bias`: current bias.
- `learning_rate`: update size.
### 📤 Output
Return a dictionary with:
- `prediction`: the current Perceptron prediction.
- `mistake`: `True` if the prediction is wrong, otherwise `False`.
- `updated_weights`: the weights after the learning step.
- `updated_bias`: the bias after the learning step.
Round updated numeric values to 6 decimals.
---
### 💻 Task
Implement `perceptron_learning_step(sample, true_label, weights, bias, learning_rate)`.
Your function should:
1. Calculate the score.
2. Predict the class.
3. Check whether the prediction is wrong.
4. Update weights and bias only if there is a mistake.
5. Return the prediction, mistake flag, updated weights, and updated bias.
---
### 🧩 Starter Code
```python
def perceptron_learning_step(sample, true_label, weights, bias, learning_rate):
# Calculate the linear score
# Convert the score into a prediction (-1 or 1)
# Check if the prediction is wrong
# If there is a mistake, update the weights
# If there is a mistake, update the bias
# Return the results
pass
```
---
### 💡 Example
```python
perceptron_learning_step(
sample=[2, 1],
true_label=1,
weights=[-1, 0],
bias=0,
learning_rate=0.5
)
```
Current score:
$$
z = (2)(-1) + (1)(0) + 0 = -2
$$
Prediction is `-1`, but the true label is `1`, so the model updates:
$$
w \leftarrow [-1,0] + 0.5(1)[2,1] = [0,0.5]
$$
$$
b \leftarrow 0 + 0.5(1) = 0.5
$$
Expected output:
```python
{
"prediction": -1,
"mistake": True,
"updated_weights": [0.0, 0.5],
"updated_bias": 0.5
}
```
---
### ⚠️ Common Mistakes
- Updating even when the prediction is correct.
- Forgetting to update the bias.
- Using the predicted label in the update instead of the true label.
- Mixing the `-1/1` convention with `0/1` labels.