From aef6df0086462d1bea150854f8d8e310be2737d3 Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Thu, 14 Jul 2022 15:42:29 -0700 Subject: [PATCH] Update F1 score implementation (#87) * alternate f1 implementation * add tests * bump patch version --- Project.toml | 2 +- src/metrics.jl | 2 +- test/metrics.jl | 27 +++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index fb96e08..dc688f6 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Lighthouse" uuid = "ac2c24cd-07f0-4848-96b2-1b82c3ea0e59" authors = ["Beacon Biosignals, Inc."] -version = "0.14.11" +version = "0.14.12" [deps] ArrowTypes = "31f734f8-188a-4ce0-8406-c8a06bd891cd" diff --git a/src/metrics.jl b/src/metrics.jl index 12c4692..4cb572d 100644 --- a/src/metrics.jl +++ b/src/metrics.jl @@ -87,7 +87,7 @@ function binary_statistics(confusion::AbstractMatrix, class_index::Integer) (false_negatives / actual_positives) precision = (true_positives == 0 && predicted_positives == 0) ? NaN : (true_positives / predicted_positives) - f1 = (2 * precision * true_positive_rate) / (precision + true_positive_rate) + f1 = true_positives / (true_positives + 0.5 * (false_positives + false_negatives)) return (; predicted_positives, predicted_negatives, actual_positives, actual_negatives, true_positives, true_negatives, false_positives, false_negatives, true_positive_rate, true_negative_rate, false_positive_rate, diff --git a/test/metrics.jl b/test/metrics.jl index 350cf77..1195985 100644 --- a/test/metrics.jl +++ b/test/metrics.jl @@ -86,6 +86,33 @@ @test isnan(stats.precision) @test isnan(stats.f1) + c = [0 0 + 0 8] + stats = binary_statistics(c, 1) + @test stats.true_positives == 0 + @test stats.true_negatives == 8 + @test stats.false_positives == 0 + @test stats.false_negatives == 0 + @test isnan(stats.f1) + + c = [0 2 + 0 6] + stats = binary_statistics(c, 1) + @test stats.true_positives == 0 + @test stats.true_negatives == 6 + @test stats.false_positives == 2 + @test stats.false_negatives == 0 + @test stats.f1 == 0 + + c = [0 0 + 2 6] + stats = binary_statistics(c, 1) + @test stats.true_positives == 0 + @test stats.true_negatives == 6 + @test stats.false_positives == 0 + @test stats.false_negatives == 2 + @test stats.f1 == 0 + for p in 0:0.1:1 @test Lighthouse._cohens_kappa(p, p) == 0 if p > 0