Convert predicted probabilities to 0/1 class labels by thresholding. Default threshold 0.5: class = 1 if p>=0.5 else 0. A second loop at 0.7 shows how raising the threshold tightens the positive-class criterion. Library: (np.array(probs) >= 0.5).astype(int). RESULT: labels at 0.5.

By hand

probs=[0.1,0.4,0.6,0.75,0.85,0.3]. At 0.5: [0,0,1,1,1,0]. At 0.7: [0,0,0,1,1,0] — p=0.6 flips from 1 to 0, p=0.4 stays 0.

naive.py
Replay: real traced execution (multi-file project)
probs = [0.1, 0.4, 0.6, 0.75, 0.85, 0.3]
labels = []
for p in probs:
    labels.append(1 if p >= 0.5 else 0)
labels_07 = []
for p in probs:
    labels_07.append(1 if p >= 0.7 else 0)
print('RESULT:', labels)
  1. probs ← [0.1, 0.4, 0.6, 0.75, 0.85, 0.3]

    1probs = [0.1, 0.4, 0.6, 0.75, 0.85, 0.3]2labels = []
    values this step[0.1, 0.4, 0.6, 0.75, 0.85, 0.3]probs
  2. labels ← []

    1probs = [0.1, 0.4, 0.6, 0.75, 0.85, 0.3]2labels = []3for p in probs:
    values this step[]labels
  3. p ← 0.1

    2labels = []3for p in probs:4    labels.append(1 if p >= 0.5 else 0)
    values this step0.1p
  4. labels ← [0]

    3for p in probs:4    labels.append(1 if p >= 0.5 else 0)5labels_07 = []
    values this step[] [0]labels
  5. p ← 0.4

    2labels = []3for p in probs:4    labels.append(1 if p >= 0.5 else 0)
    values this step0.1 0.4p
  6. labels ← [0, 0]

    3for p in probs:4    labels.append(1 if p >= 0.5 else 0)5labels_07 = []
    values this step[0] [0, 0]labels
  7. p ← 0.6

    2labels = []3for p in probs:4    labels.append(1 if p >= 0.5 else 0)
    values this step0.4 0.6p
  8. labels ← [0, 0, 1]

    3for p in probs:4    labels.append(1 if p >= 0.5 else 0)5labels_07 = []
    values this step[0, 0] [0, 0, 1]labels
  9. p ← 0.75

    2labels = []3for p in probs:4    labels.append(1 if p >= 0.5 else 0)
    values this step0.6 0.75p
  10. labels ← [0, 0, 1, 1]

    3for p in probs:4    labels.append(1 if p >= 0.5 else 0)5labels_07 = []
    values this step[0, 0, 1] [0, 0, 1, 1]labels
  11. p ← 0.85

    2labels = []3for p in probs:4    labels.append(1 if p >= 0.5 else 0)
    values this step0.75 0.85p
  12. labels ← [0, 0, 1, 1, 1]

    3for p in probs:4    labels.append(1 if p >= 0.5 else 0)5labels_07 = []
    values this step[0, 0, 1, 1] [0, 0, 1, 1, 1]labels
  13. p ← 0.3

    2labels = []3for p in probs:4    labels.append(1 if p >= 0.5 else 0)
    values this step0.85 0.3p
  14. labels ← [0, 0, 1, 1, 1, 0]

    3for p in probs:4    labels.append(1 if p >= 0.5 else 0)5labels_07 = []
    values this step[0, 0, 1, 1, 1] [0, 0, 1, 1, 1, 0]labels
  15. for p in probs:

    2labels = []3for p in probs:4    labels.append(1 if p >= 0.5 else 0)
  16. labels_07 ← []

    4    labels.append(1 if p >= 0.5 else 0)5labels_07 = []6for p in probs:
    values this step[]labels_07
  17. p ← 0.1

    5labels_07 = []6for p in probs:7    labels_07.append(1 if p >= 0.7 else 0)
    values this step0.3 0.1p
  18. labels_07 ← [0]

    6for p in probs:7    labels_07.append(1 if p >= 0.7 else 0)8print('RESULT:', labels)
    values this step[] [0]labels_07
  19. p ← 0.4

    5labels_07 = []6for p in probs:7    labels_07.append(1 if p >= 0.7 else 0)
    values this step0.1 0.4p
  20. labels_07 ← [0, 0]

    6for p in probs:7    labels_07.append(1 if p >= 0.7 else 0)8print('RESULT:', labels)
    values this step[0] [0, 0]labels_07
  21. p ← 0.6

    5labels_07 = []6for p in probs:7    labels_07.append(1 if p >= 0.7 else 0)
    values this step0.4 0.6p
  22. labels_07 ← [0, 0, 0]

    6for p in probs:7    labels_07.append(1 if p >= 0.7 else 0)8print('RESULT:', labels)
    values this step[0, 0] [0, 0, 0]labels_07
  23. p ← 0.75

    5labels_07 = []6for p in probs:7    labels_07.append(1 if p >= 0.7 else 0)
    values this step0.6 0.75p
  24. labels_07 ← [0, 0, 0, 1]

    6for p in probs:7    labels_07.append(1 if p >= 0.7 else 0)8print('RESULT:', labels)
    values this step[0, 0, 0] [0, 0, 0, 1]labels_07
  25. p ← 0.85

    5labels_07 = []6for p in probs:7    labels_07.append(1 if p >= 0.7 else 0)
    values this step0.75 0.85p
  26. labels_07 ← [0, 0, 0, 1, 1]

    6for p in probs:7    labels_07.append(1 if p >= 0.7 else 0)8print('RESULT:', labels)
    values this step[0, 0, 0, 1] [0, 0, 0, 1, 1]labels_07
  27. p ← 0.3

    5labels_07 = []6for p in probs:7    labels_07.append(1 if p >= 0.7 else 0)
    values this step0.85 0.3p
  28. labels_07 ← [0, 0, 0, 1, 1, 0]

    6for p in probs:7    labels_07.append(1 if p >= 0.7 else 0)8print('RESULT:', labels)
    values this step[0, 0, 0, 1, 1] [0, 0, 0, 1, 1, 0]labels_07
  29. for p in probs:

    5labels_07 = []6for p in probs:7    labels_07.append(1 if p >= 0.7 else 0)
  30. stdout ← RESULT: [0, 0, 1, 1, 1, 0]

    7    labels_07.append(1 if p >= 0.7 else 0)8print('RESULT:', labels)
    values this stepRESULT: [0, 0, 1, 1, 1, 0]stdout

With NumPy

(np.array(probs) >= threshold).astype(int) applies the comparison element-wise and converts the boolean array to ints in one expression.

library.py
import numpy as np
from dalib.display import set_display
set_display()

probs = [0.1, 0.4, 0.6, 0.75, 0.85, 0.3]
labels = (np.array(probs) >= 0.5).astype(int).tolist()
labels_07 = (np.array(probs) >= 0.7).astype(int).tolist()
print('threshold=0.5:', labels)
print('threshold=0.7:', labels_07)
print('RESULT:', labels)
threshold=0.5: [0, 0, 1, 1, 1, 0]
threshold=0.7: [0, 0, 0, 1, 1, 0]
RESULT: [0, 0, 1, 1, 1, 0]

Implementation notes

  • Threshold 0.5 is the default: it minimises classification error when classes are balanced and the model is well-calibrated.
  • Raising the threshold (e.g. to 0.7) requires higher confidence to predict positive — reduces false positives but increases false negatives. Lowering it does the opposite. This trade-off is the precision/recall curve.
  • Cross-reference: logistic-predict-given-weights (this chapter) computes the probabilities that are thresholded here.
  • Cross-reference: log-loss-small (this chapter) evaluates probabilities before thresholding — log loss works on raw probs, not 0/1 labels.