A threshold is a cutoff that turns a numerical value into a binary decision. For example, a wine with total sulfur dioxide below a chosen cutoff can be predicted to be good; a wine at or above it is not. The direction of the comparison matters: other classifiers predict positive when a score is above the cutoff. section 4.3.6 section 13.6.1
A wine-quality shortcut
The mean total sulfur dioxide for middling wines is 141.83. Using that value as a cutoff predicts 2,727 of 4,898 wines to be good. Comparing those predictions with the actual criterion, quality score greater than 5, gives 2,018 matches: about 74% of predicted good wines are actually good, while about 62% of actual good wines are identified. It is a useful shortcut, but not a perfect classifier. 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 Boolean mask: True for each wine below the sulfur dioxide threshold. section 4.3.6
Keep the prediction threshold separate from the rule defining the actual label. Comparing the two produces true positives, false positives, true negatives, and false negatives. Boolean masks make these groups easy to count. section 4.3.6 section 14.3
Listing 13.17 Masking for classification metrics
negLabel_mask = metrics_t[METRICS_LABEL_NDX] <= classificationThreshold negPred_mask = metrics_t[METRICS_PRED_NDX] <= classificationThreshold posLabel_mask = ~negLabel_mask posPred_mask = ~negPred_mask
For a model's positive scores, this binary classification example treats values at or below 0.5 as negative and inverts the mask for positives. section 13.6.1
Changing a threshold changes the balance of mistakes. For a positive score classified as positive above the cutoff, lowering it can catch more positives but also admit more false positives; raising it can reduce false positives while missing more positives. Precision measures the fraction of positive predictions that are correct, while recall measures the fraction of actual positives found. F1 combines both rather than rewarding either one alone. section 14.3.1 section 14.3.2 section 14.3.4
Threshold adjustment has limits. When positive and negative cases overlap heavily, moving the cutoff alone may not separate them well; data balance and the model's ability to separate cases also matter. section 14.4



















