[bug] ArgumentError in build_metrics during threshold tuning (03-logistic-regression)

Author: vatsalyarCreated Aug 25, 2026Updated Aug 25, 2026
Labelsbug

Where

  • Phase / lesson: Phase 2 · 03-logistic-regression
  • File / URL: phases/02-ml-fundamentals/03-logistic-regression/code/main.jl

What's wrong

During the demo_threshold_tuning execution, if a threshold is strict enough that the model predicts zero positive cases (y_pred contains only 0s), the generator comprehensions inside the build_metrics function become completely empty. Because sum() is called on these empty generators without an init fallback, Julia throws an ArgumentError: reducing over an empty collection is not allowed, causing the script to crash before completing the tuning table.

Reproduce

  1. Clone the repository and navigate to phases/02-ml-fundamentals/03-logistic-regression/code/
  2. Run the script from the terminal using the command: julia main.jl
  3. Wait for the demo_threshold_tuning function to execute and observe the crash when it loops through the higher thresholds.

Environment

  • OS: Windows
  • Python / Node / other runtime version: Julia 1.12.7
  • How you ran it (local, Colab, Docker, etc.): Local terminal

Screenshot or logs

ERROR: LoadError: ArgumentError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer
Stacktrace:
 [1] _empty_reduce_error()
   @ Base .\reduce.jl:312
 [2] mapreduce_empty(f::Function, op::Base.BottomRF{typeof(Base.add_sum)}, T::Type)
   @ Base .\reduce.jl:314
 [3] reduce_empty(op::Base.MappingRF{var"#build_metrics##12#build_metrics##13", Base.BottomRF{typeof(Base.add_sum)}}, ::Type{Int64})
   @ Base .\reduce.jl:351
...
 [13] build_metrics(y_true::Vector{Int64}, y_pred::Vector{Int64})
   @ Main E:\aie\ai-engineering-from-scratch\phases\02-ml-fundamentals\03-logistic-regression\code\main.jl:125
 [14] demo_threshold_tuning(model::LogisticRegression, X_test::Vector{Vector{Float64}}, ys_test::Vector{Int64})
   @ Main E:\aie\ai-engineering-from-scratch\phases\02-ml-fundamentals\03-logistic-regression\code\main.jl:310

Source: rohitg00/ai-engineering-from-scratch