Perceptron Prediction Practice Problem
This data science coding problem helps you practice Perceptron, perceptron prediction, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Perceptron.
- Problem ID: 215
- Problem key: 215-perceptron-prediction
- URL: https://datacrack.app/solve/215-perceptron-prediction
- Difficulty: easy
- Topic: Perceptron
- Module: Introduction to Machine Learning
Problem Statement
# 🧩 Perceptron Prediction
---
### 🎯 Goal
Use a Perceptron to predict one binary class from input features.
This is your first simple **linear classifier**.
A classifier is a model that chooses a class label. In this topic, the labels are:
- `1` for the positive class
- `-1` for the negative class
A Perceptron is linear because it starts with the same kind of score you saw in Linear Regression:
$$
z = w^T x + b
$$
But Linear Regression returns the score as a numeric prediction. A Perceptron uses the score only to decide which side of a boundary the sample is on.
---
### 📖 Introduction
The Perceptron prediction pipeline is:
$$
\text{Input Features}
\rightarrow
\text{Linear Score}
\rightarrow
\text{Step Rule}
\rightarrow
\text{Class Prediction}
$$
The linear score is:
$$
z = w^T x + b
$$
Then the step rule converts the score into a class:
$$
\hat{y} =
\begin{cases}
1, & z \ge 0 \\
-1, & z < 0
\end{cases}
$$
The decision boundary occurs where:
$$
w^T x + b = 0
$$
The sign of the score determines which side of the boundary the sample is on:
- $z \ge 0$ → predict class $1$
- $z < 0$ → predict class $-1$
---
### 🔍 Explanation of Symbols
| Symbol | Meaning | Shape / Type |
|:--:|:--|:--|
| $x$ | input feature vector for one sample | list of numbers |
| $w$ | weight vector, one weight per feature | list of numbers |
| $b$ | bias term | number |
| $z$ | linear score $w^Tx+b$ | number |
| $\hat{y}$ | predicted class | either $-1$ or $1$ |
We will use labels $-1$ and $1$ throughout this Perceptron topic.
---
### 📥 Input
- `features`: a list of numbers representing one sample.
- `weights`: a list of numbers representing the model weights.
- `bias`: a number added to the linear score.
### 📤 Output
- Return `1` if the score is greater than or equal to `0`.
- Return `-1` if the score is less than `0`.
---
### 💻 Task
Implement `perceptron_predict(features, weights, bias)`.
Your function should:
1. Multiply each feature by its matching weight.
2. Add the products together.
3. Add the bias.
4. Apply the Perceptron step rule.
5. Return the predicted class.
Do not train the model in this problem. The weights and bias are already given.
---
### 🧩 Starter Code
```python
def perceptron_predict(features, weights, bias):
pass
```
---
### 💡 Example
```python
perceptron_predict(
features=[2, 1],
weights=[1, -1],
bias=-0.5
)
```
Manual score:
$$
z = (2)(1) + (1)(-1) - 0.5 = 0.5
$$
Since $0.5 \ge 0$, the prediction is:
```python
1
```
---
### ⚠️ Common Mistakes
- Returning the score instead of the class.
- Using `>` instead of `>=` for the zero case.
- Forgetting to add the bias.
- Mixing labels `0/1` with the Perceptron labels `-1/1` used here.