Skip to content

Commit

Permalink
WIP utility singleLabelConfusionMatrix(List<Prediction<MultiLabel>> p…
Browse files Browse the repository at this point in the history
…redictions)

- partially represent false negatives by calling them false positives tied to some predicted label if there is one
  • Loading branch information
nezda committed Apr 22, 2021
1 parent 35735aa commit 0dcab25
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -118,14 +119,19 @@ public static LabelConfusionMatrix singleLabelConfusionMatrix(final List<Predict
}

public static List<Prediction<Label>> mkSingleLabelPredictions(List<Prediction<MultiLabel>> predictions) {
return mkSingleLabelPredictions(predictions, false);
}

public static List<Prediction<Label>> mkSingleLabelPredictions(List<Prediction<MultiLabel>> predictions,
final boolean falseNegativeHeuristic) {
return predictions.stream()
.flatMap(p -> {
final Set<Label> trueLabels = p.getExample().getOutput().getLabelSet();
final Set<Label> predicted = p.getOutput().getLabelSet();
// intersection(trueLabels, predicted) = true positives
// predicted - trueLabels = false positives
// trueLabels - predicted = false negatives
return predicted.stream().map(pred -> {
return Stream.concat(predicted.stream().map(pred -> {
if (trueLabels.contains(pred)) {
return mkPrediction(pred.getLabel(), pred.getLabel());
} else if (trueLabels.size() == 1) {
Expand All @@ -134,7 +140,21 @@ public static List<Prediction<Label>> mkSingleLabelPredictions(List<Prediction<M
// arbitrarily pick first trueLabel
return mkPrediction(trueLabels.iterator().next().getLabel(), pred.getLabel());
}
});
}),
!falseNegativeHeuristic ? Stream.of() :
// partially represent false negatives by calling them false positives tied to some predicted label if there is one
trueLabels.stream().filter(t -> !predicted.contains(t)).flatMap(fnTrueLabel -> {
if (predicted.isEmpty()) {
// nothing to pin this on
return Stream.of();
} else if (predicted.size() == 1) {
return Stream.of(mkPrediction(fnTrueLabel.getLabel(), predicted.iterator().next().getLabel()));
} else {
// arbitrarily pick first predicted label
return Stream.of(mkPrediction(fnTrueLabel.getLabel(), predicted.iterator().next().getLabel()));
}
})
);
}).collect(Collectors.toList());
}

Expand Down

0 comments on commit 0dcab25

Please sign in to comment.