liveBook · Manning

Threshold

As explained in Deep Learning with PyTorch, Second Edition

A cutoff value used here to turn a continuous wine-quality-related variable into a binary prediction.

A threshold is a cutoff that turns a numerical value into a yes-or-no decision. For a quick wine-quality prediction, compare each wine’s total sulfur dioxide with 141.83: values below the cutoff predict a good wine. This is a crude rule, not a trained model. section 4.3.6

# In[16]:
total_sulfur_threshold = 141.83
total_sulfur_data = data[:,6]
predicted_indexes = torch.lt(total_sulfur_data, total_sulfur_threshold)

predicted_indexes.shape, predicted_indexes.dtype, predicted_indexes.sum()

# Out[16]:
(torch.Size([4898]), torch.bool, tensor(2727))

The PyTorch comparison produces a torch.bool mask with one prediction per wine. Its 2,727 True values mark the wines predicted to be good. section 4.3.6

A threshold can also define the answer used to check a prediction. Here, an actual wine is counted as good when its quality score is greater than 5. That label rule is separate from the sulfur dioxide rule making the prediction. Comparing their Boolean masks with & identifies wines both predicted and actually counted as good. section 4.3.6

# In[18]:
n_matches = torch.sum(actual_indexes & predicted_indexes).item()
n_predicted = torch.sum(predicted_indexes).item()
n_actual = torch.sum(actual_indexes).item()

n_matches, n_matches / n_predicted, n_matches / n_actual

# Out[18]:
(2018, 0.74000733406674, 0.6193984039287906)

The masks agree on 2,018 wines. That is about 74% of the 2,727 predicted-good wines, but only about 62% of the 3,258 actually good wines: the cutoff misses many good wines. section 4.3.6

The same idea applies to a binary classifier’s output. With classificationThreshold set to 0.5, predictions at or below the cutoff become negative; predictions above it become positive. Boolean masks then support counts and per-class metrics. Moving the cutoff can reduce missed positives or false alarms, but neither precision nor recall alone tells the whole story. When classes overlap heavily, moving the threshold has limited power to fix the predictions. section 13.6.1 section 14.4

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Works through numeric comparisons and Boolean-mask counts, but does not give an algorithm for choosing an optimal cutoff.
Examples
Uses a 141.83 sulfur dioxide cutoff for wines and a 0.5 classification cutoff for nodule predictions.
What it assumes
Introduces Boolean tensor masks and advanced indexing without requiring prior familiarity with masked arrays.
What you can do after
Apply it via PyTorch to create Boolean predictions and compare predicted-positive counts with actual-positive counts.
How it connects
Connects Boolean masks and advanced indexing with binary classification, precision, recall, and F1 score.

Also explained in

Compare how 13 books explain threshold →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.