0
0
ML Pythonml~20 mins

Threshold tuning in ML Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Threshold Tuning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the effect of threshold on classification

In a binary classification model, what happens to the precision and recall when you increase the decision threshold from 0.5 to 0.8?

ABoth precision and recall increase
BPrecision decreases and recall increases
CPrecision increases and recall decreases
DBoth precision and recall decrease
Attempts:
2 left
💡 Hint

Think about how raising the threshold affects which predictions are labeled positive.

Predict Output
intermediate
2:00remaining
Output of threshold tuning code snippet

What is the output of the following Python code that applies threshold tuning on model probabilities?

ML Python
import numpy as np
probs = np.array([0.2, 0.6, 0.8, 0.4, 0.9])
threshold = 0.7
preds = (probs >= threshold).astype(int)
print(preds.tolist())
A[0, 1, 0, 0, 1]
B[0, 0, 1, 0, 1]
C[0, 1, 1, 0, 1]
D[1, 1, 1, 0, 1]
Attempts:
2 left
💡 Hint

Check which probabilities are greater than or equal to 0.7.

Model Choice
advanced
2:00remaining
Choosing threshold for imbalanced data

You have a highly imbalanced dataset with very few positive cases. Which threshold tuning strategy is best to maximize recall while keeping false positives reasonable?

ARandomly select threshold to avoid bias
BSet a high threshold to reduce false positives, ignoring recall
CUse threshold 0.5 without tuning since it is default
DSet a low threshold to catch most positives, then use precision-recall curve to find balance
Attempts:
2 left
💡 Hint

Recall is about catching positives; think about how threshold affects it.

Metrics
advanced
2:00remaining
Effect of threshold on F1 score

Given a model with these confusion matrix values at threshold 0.5: TP=40, FP=10, FN=20, TN=130. If threshold is increased to 0.7, TP=30, FP=5, FN=30, TN=135. What happens to the F1 score?

AF1 score decreases
BF1 score increases
CF1 score stays the same
DCannot determine without precision and recall
Attempts:
2 left
💡 Hint

Calculate precision and recall for both thresholds, then compute F1.

🔧 Debug
expert
3:00remaining
Debugging threshold tuning code with unexpected output

Consider this Python code snippet for threshold tuning. It produces unexpected predictions. What is the cause?

import numpy as np
probs = np.array([0.3, 0.7, 0.5, 0.9])
threshold = 0.5
preds = (probs > threshold).astype(int)
print(preds.tolist())
AUsing '>' excludes probabilities equal to threshold, causing some positives to be missed
BThe threshold variable is not used correctly; should be a list
Castype(int) converts floats incorrectly causing wrong predictions
DNumpy array probs is not sorted, causing wrong output
Attempts:
2 left
💡 Hint

Check how the comparison operator affects predictions equal to threshold.