Binary Classification Prediction Practice Problem
This data science coding problem helps you practice Logistic Regression, binary classification prediction, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Logistic Regression.
- Problem ID: 9
- Problem key: 9-binary-classification-prediction
- URL: https://datacrack.app/solve/9-binary-classification-prediction
- Difficulty: easy
- Topic: Logistic Regression
- Module: Introduction to Machine Learning
Problem Statement
# Binary Classification Prediction
---
### 🎯 Goal
Introduce **binary classification** — where outputs belong to one of two classes (e.g., spam / not spam, admitted / not admitted).
You’ll compute predicted **class labels** (0 or 1) based on model outputs (probabilities).
---
### 🔍 Explanation of Symbols
| Symbol | Meaning | Shape / Type |
| :-------------: | :------------------------------------------------------- | :------------ |
| **$x_i$** | Input feature vector for sample *i* | (n_features,) |
| **$\hat{y}_i$** | Predicted probability that sample *i* belongs to class 1 | float (0–1) |
| **$y_i$** | True label (0 or 1) | integer |
| **$t$** | Threshold for classification (default = 0.5) | float |
---
### 📥 Input / 📤 Output
* **Input:**
* `y_prob`: NumPy array of predicted probabilities, shape *(n,)*
* `threshold`: float value to decide class membership (default 0.5)
* **Output:**
* NumPy array *(n,)* of predicted class labels (0 or 1)
---
### 💻 Task
Implement a function that converts predicted probabilities into binary class predictions:
$$
\hat{y}_{\text{class}, i} =
\begin{cases}
1, & \text{if } \hat{y}_i \ge t \\\\
0, & \text{otherwise}
\end{cases}
$$
---
### 🧩 Starter Code
```python
import numpy as np
def binary_classification_prediction(y_prob, threshold=0.5):
"""
Convert predicted probabilities to binary class labels.
Args:
y_prob (np.ndarray): predicted probabilities (n,)
threshold (float): classification threshold (default = 0.5)
Returns:
np.ndarray: predicted class labels (0 or 1)
"""
# TODO: implement threshold-based classification
pass
```
---
### 💡 Example
```python
y_prob = np.array([0.8, 0.45, 0.3, 0.9])
binary_classification_prediction(y_prob)
```
**Expected Output:**
```
array([1, 0, 0, 1])
```
---
Starter Code
import numpy as np
def binary_classification_prediction(y_prob, threshold=0.5):
"""
Convert predicted probabilities to binary class labels.
Args:
y_prob (np.ndarray): predicted probabilities (n,)
threshold (float): classification threshold (default = 0.5)
Returns:
np.ndarray: predicted class labels (0 or 1)
"""
# TODO: implement threshold-based classification
pass