Perceptron Learning Algorithm Training Practice Problem
This data science coding problem helps you practice Perceptron, perceptron learning algorithm training, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Perceptron.
- Problem ID: 217
- Problem key: 217-perceptron-learning-algorithm-training
- URL: https://datacrack.app/solve/217-perceptron-learning-algorithm-training
- Difficulty: medium
- Topic: Perceptron
- Module: Introduction to Machine Learning
Problem Statement
# 🧩 Perceptron Learning Algorithm Training
---
### 🎯 Goal
Train a Perceptron by repeating prediction and the learning rule across a dataset.
You already learned:
- Problem 1: how a Perceptron predicts.
- Problem 2: how one Perceptron update works after one mistake.
Now we combine them into the full Perceptron Learning Algorithm, often called **PLA**.
---
### 📖 Introduction
PLA repeats this process:
$$
\text{Predict}
\rightarrow
\text{Check Mistake}
\rightarrow
\text{Update if Wrong}
\rightarrow
\text{Move to Next Sample}
$$
One full pass over the dataset is called an **epoch**.
For each sample $x_i$:
1. Compute the score:
$$
z_i=w^Tx_i+b
$$
2. Predict:
$$
\hat{y}_i =
\begin{cases}
1, & z_i \ge 0 \\
-1, & z_i < 0
\end{cases}
$$
3. If the prediction is wrong, update:
$$
w \leftarrow w + \eta y_i x_i
$$
$$
b \leftarrow b + \eta y_i
$$
---
### 📥 Input
- `X`: list of training samples.
- `y`: list of true labels, each either `-1` or `1`.
- `learning_rate`: update size.
- `epochs`: number of full passes through the dataset.
### 📤 Output
Return a dictionary with:
- `weights`: learned weights rounded to 6 decimals.
- `bias`: learned bias rounded to 6 decimals.
- `mistakes_per_epoch`: number of mistakes made in each epoch.
- `predictions`: final predictions for all training samples.
---
### 💻 Task
Implement `train_perceptron(X, y, learning_rate=1.0, epochs=3)`.
Your function should:
1. Initialize all weights to `0` and bias to `0`.
2. Loop through the dataset for the given number of epochs.
3. Predict each sample.
4. Update only when the prediction is wrong.
5. Track how many mistakes happen in each epoch.
6. After training, predict every training sample using the final weights and bias.
7. Return the learned model information.
---
### 🧩 Starter Code
```python
def train_perceptron(X, y, learning_rate=1.0, epochs=3):
# Initialize the weights and bias
# Repeat training for the given number of epochs
# Reset the mistake count for this epoch
# Go through each training sample
# Calculate the linear score
# Convert the score into a prediction (-1 or 1)
# If the prediction is wrong, update the weights and bias
# Count the mistake
# Store the number of mistakes for this epoch
# Use the final weights and bias to predict all training samples
return {
"weights": weights,
"bias": bias,
"mistakes_per_epoch": mistakes_per_epoch,
"predictions": predictions
}
```
---
### 💡 Example
```python
train_perceptron(
X=[[2, 1], [1, 2], [-1, -1], [-2, -1]],
y=[1, 1, -1, -1],
learning_rate=1.0,
epochs=3
)
```
Expected output:
```python
{
"weights": [1.0, 1.0],
"bias": -1.0,
"mistakes_per_epoch": [1, 0, 0],
"predictions": [1, 1, -1, -1]
}
```
---
### ⚠️ Common Mistakes
- Reinitializing weights inside every epoch. Initialize once before training starts.
- Updating on correct predictions.
- Forgetting to count mistakes per epoch.
- Returning predictions from before training instead of after training.