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












