0
0
ML Pythonml~10 mins

Threshold tuning in ML Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the threshold for binary classification.

ML Python
threshold = [1]
Drag options to blanks, or click blank then click option'
A0
B1.0
C0.5
D-0.5
Attempts:
3 left
💡 Hint
Common Mistakes
Using threshold values outside the 0-1 range.
Setting threshold to 0 or 1 which makes classification trivial.
2fill in blank
medium

Complete the code to convert probabilities to binary predictions using the threshold.

ML Python
predictions = (probabilities > [1]).astype(int)
Drag options to blanks, or click blank then click option'
A1.0
B0.5
C0.7
D0.3
Attempts:
3 left
💡 Hint
Common Mistakes
Using threshold greater than 1 or less than 0.
Not converting boolean array to integers.
3fill in blank
hard

Fix the error in the code to compute true positives using the threshold.

ML Python
true_positives = ((y_true == 1) & (y_scores > [1])).sum()
Drag options to blanks, or click blank then click option'
A0
B-1
C1
D0.5
Attempts:
3 left
💡 Hint
Common Mistakes
Using threshold 0 or 1 which may not capture positives correctly.
Using negative threshold values.
4fill in blank
hard

Fill both blanks to create a dictionary of thresholds and their corresponding F1 scores.

ML Python
f1_scores = {thr: f1_score(y_true, (y_scores > [1]).astype(int)) for thr in [2]
Drag options to blanks, or click blank then click option'
Athr
B[0.1, 0.3, 0.5, 0.7, 0.9]
Cthresholds
Dy_scores
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for threshold or list.
Not converting boolean to int before computing F1 score.
5fill in blank
hard

Fill all three blanks to select the best threshold based on maximum F1 score.

ML Python
best_threshold = max(f1_scores, key=lambda [1]: f1_scores[[2]])
print(f"Best threshold: [3]")
Drag options to blanks, or click blank then click option'
Athr
Cbest_threshold
Dthreshold
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inside lambda and key lookup.
Printing a variable that is not defined.