smartyfy.mrindigi.com

AI & Machine Learning Master Course — 051. Classification outputs and decision rules

Smartyfy AI & Machine Learning Master Course

Course ID: smartyfy-ai-ml-master-200

Lesson 051: Classification outputs and decision rules

Prerequisites: Lesson 050. Learning outcome: Separate class scores, probabilities, thresholds, and final actions.

1. A classifier can produce several different objects

A parcel-inspection classifier might output a score, an estimated probability of damage, a class label, and an operational action. These are related but distinct. A score can rank parcels without being a probability. A probability estimate describes uncertainty under a model. A class label applies a decision rule. An action may also account for inspection capacity, costs, and review requirements.

Suppose two parcels receive damage probabilities 0.49 and 0.51. A threshold of 0.5 produces different labels even though the estimates are close. Conversely, probabilities 0.51 and 0.99 receive the same label while expressing very different model confidence. Retain continuous outputs for evaluation and decisions that need more information than a binary flag.

2. Define the positive class and score direction

A binary task requires an explicit positive class. Here damage is positive and undamaged is negative. Reversing that convention changes the meanings of precision, recall, false positives, and false negatives. Write down the outcome definition rather than assuming that label 1 has a universal meaning.

Probability columns follow an estimator's class ordering. Inspect classes_ before selecting a column from predict_proba. A column at index one refers to the second learned class, which need not match a casually assumed business label. Decision-function outputs also have documented direction and shape conventions; do not convert an arbitrary score into a probability by simply clipping it to zero and one.

Calibration asks whether events assigned a given probability occur at a corresponding frequency on appropriate data. A model can rank positives above negatives well while giving poor probability estimates. A high score, a high probability, and a reliable probability are therefore three different claims requiring different evidence.

3. Derive a threshold from a simple cost model

Assume correct decisions have zero cost, a false positive costs C_FP, and a false negative costs C_FN. Both costs are nonnegative and C_FP + C_FN > 0. For a reliable probability p of a positive outcome, predicting positive has expected cost (1−p)C_FP. Predicting negative has expected cost pC_FN. Choose positive when its expected cost is no larger.

Rearranging gives threshold C_FP/(C_FP+C_FN). With false-positive cost one and false-negative cost four, the threshold is 0.2. At p=0.3, positive has expected cost 0.7, while negative has expected cost 1.2. The cost-optimal action under these assumptions is positive even though damage is less likely than no damage.

This formula is conditional on calibrated probabilities for the relevant population and the stated constant costs. Capacity limits, intervention effects, different costs by parcel, abstention, or uncertain labels can change the decision problem. It is not a universal instruction to use 0.2 whenever a dataset is imbalanced.

Expected positive-action cost 1−p and negative-action cost 4p intersect at probability 0.2 and cost 0.8. At p = 0.3, the costs are 0.7 and 1.2. The same six invented estimates, 0.1, 0.3, 0.4, 0.6, 0.8 and 0.9, produce three positive decisions at threshold 0.5 and five at 0.2. The required calibration and constant-cost assumptions are stated.
Probability estimation and decision-making are separate steps. Under these costs and calibration assumptions, choosing positive at p≥0.2 minimizes conditional expected cost. Open full-size diagram

4. Select decisions without retraining the score unnecessarily

Changing a threshold changes class decisions while leaving the underlying scores and probabilities fixed. Lowering it usually flags more observations, which can increase recall and also false positives. Inspect the resulting counts and workload. Ranking metrics alone do not tell you how many parcels will be sent for inspection at a chosen threshold.

If a threshold is selected empirically, use development validation or an appropriate cross-validated threshold procedure. Do not optimize it on the final test labels. A model fitted on training data and a threshold tuned on that same training data can also overfit. Treat the threshold as part of the complete system requiring independent assessment.

An abstention rule can route uncertain or unsupported cases to review, but abstention is an action with workload and missed-service consequences. Define how it is evaluated, who reviews the case, and what happens if no reviewer is available. Calling an uncertain prediction “human reviewed” without such a process does not make the system reliable.

CPU lab: decisions change while probabilities stay fixed

This small CPU script uses NumPy and scikit-learn metrics. It was checked with versions 2.3.5 and 1.8.0. Six invented probabilities and outcomes illustrate a decision rule; no model is trained and no calibration claim is made about these six values.

At threshold 0.5, the counts are two true negatives, one false positive, one false negative, and two true positives. The stated cost is five. At the independently cost-derived threshold 0.2, the counts become one, two, zero, three, giving cost two. This realized sample result illustrates the calculation, not a proof that every future batch will have lower cost. The probability array is unchanged.

import numpy as np
from sklearn.metrics import confusion_matrix
p=np.array([.1,.3,.4,.6,.8,.9])
y=np.array([0,1,0,1,0,1])
false_positive_cost,false_negative_cost=1,4
for threshold in [.5,.2]:
    decision=(p>=threshold).astype(int)
    tn,fp,fn,tp=confusion_matrix(y,decision,labels=[0,1]).ravel()
    cost=false_positive_cost*fp+false_negative_cost*fn
    print('threshold:',threshold,'TN FP FN TP:',tn,fp,fn,tp,'cost:',cost)
optimal=false_positive_cost/(false_positive_cost+false_negative_cost)
assert optimal==.2
q=.3
print('Expected loss positive/negative:',(1-q)*false_positive_cost,q*false_negative_cost)
print('Class probabilities unchanged:',p.tolist())

Four practice questions with worked answers

Practice 1

With false-positive cost 2 and false-negative cost 8, derive the simple probability threshold.

Answer: The threshold is 2/(2+8)=0.2. At exactly 0.2, both actions have expected cost 1.6, so a documented tie policy decides. The formula assumes reliable probabilities, zero correct-decision costs, and no additional action constraints; it is not estimated by choosing the best final-test outcome.

Practice 2

A model has classes_[“damaged”,“safe”]. Which probability column represents damage?

Answer: The first column, index zero, corresponds to damaged. Selecting column one by habit would instead use safe probability and reverse the intended meaning. Verify the actual ordering and positive-class definition in both the model output and the metric computation.

Practice 3

Why can a ranking score be useful but unsuitable for the cost-threshold formula?

Answer: Ordering only requires higher scores to correspond more often to positives. The formula uses numerical probabilities in expected-cost arithmetic, so the scale must have an appropriate probability interpretation and calibration. Applying 0.2 to an arbitrary margin does not reproduce the derived decision rule.

Practice 4

A threshold is chosen to maximize final-test F1. Can that same F1 be called untouched final performance?

Answer: No. The labels influenced a system parameter, so the reported value is selection-influenced. Tune the threshold on development data or within a suitable inner procedure, freeze it, and assess the complete system on independent observations. Record any previous test reuse honestly.

Five multiple-choice questions

Question 1

What changes when a fixed model’s threshold changes?

  • A. Its probability estimates necessarily
  • B. Its learned coefficients necessarily
  • C. Its class decisions

Correct: C. The decision rule changes, while the fitted scoring function can remain exactly the same.

Question 2

What is needed for the simple expected-cost formula?

  • A. Reliable relevant probabilities and stated costs
  • B. Only a high ranking score
  • C. A balanced training sample

Correct: A. Ranking or balanced sampling alone does not establish the probability scale or decision costs.

Question 3

Why inspect classes_?

  • A. To assume the most frequent class is always first
  • B. To find the order of probability columns
  • C. To rank classes by their average confidence

Correct: B. The column-to-class mapping must match the event whose probability is being used.

Question 4

Does a good ranking guarantee good calibration?

  • A. No, ordering and probability accuracy differ
  • B. Only with a threshold of 0.5
  • C. Yes, by monotonicity

Correct: A. A transformation can preserve ranking while making the probability values too extreme or too conservative.

Question 5

Where should an empirically chosen threshold be tuned?

  • A. On development data or an appropriate inner validation process
  • B. On the same training predictions without further checks
  • C. On final-test labels

Correct: A. Threshold selection is model development and must remain separate from final assessment.

Common pitfalls

Do not treat 0.5 as a universally optimal threshold. Do not interpret a library’s positive class without checking labels. Avoid reporting the cost of a threshold chosen using the same final outcomes as though it were an independent deployment estimate.

Summary

Separate scores, probabilities, class decisions, and actions. Define label direction, check calibration when probabilities drive costs, and treat threshold selection as part of development. A useful decision rule depends on both evidence and the actual consequences of errors.

Primary sources and further reading