Skip to content

feature/pretty-MultiLabelConfusionMatrix-toString #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static <T extends Classifiable<T>> double accuracy(T label, ConfusionMatr
double support = cm.support(label);
// handle div-by-zero
if (support == 0d) {
logger.warning("No predictions: accuracy ill-defined");
logger.warning("No predictions for " + label + ": accuracy ill-defined");
return Double.NaN;
}
return cm.tp(label) / cm.support(label);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* A {@link ConfusionMatrix} which accepts {@link MultiLabel}s.
Expand Down Expand Up @@ -158,15 +159,18 @@ public double confusion(MultiLabel predicted, MultiLabel truth) {

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < mcm.length; i++) {
DenseMatrix cm = mcm[i];
sb.append(cm.toString());
sb.append("\n");
}
sb.append("]");
return sb.toString();
return getDomain().getDomain().stream()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is sensitive to the iteration order of the HashSet inside the domain, whereas the old one was fixed on construction (though when it was constructed it was fixed based on the iteration order of the HashSet in the domain).

This comment is more a todo for later when the label domains have consistent iteration order (probably lexicographic like the features).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point. That variable iteration order has confused me several times. There's always LinkedHashSet? Or maybe https://docs.oracle.com/javase/9/docs/api/java/util/Set.html#of-E...- would have stable iteration order like Guava's ImmutableHashSet? How long will Java 8 be required?

.map(multiLabel -> {
final int tp = (int) tp(multiLabel);
final int fn = (int) fn(multiLabel);
final int fp = (int) fp(multiLabel);
final int tn = (int) tn(multiLabel);
return String.join("\n",
multiLabel.toString(),
String.format(" [tn: %,d fn: %,d]", tn, fn),
String.format(" [fp: %,d tp: %,d]", fp, tp));
}
).collect(Collectors.joining("\n"));
}

static ConfusionMatrixTuple tabulate(ImmutableOutputInfo<MultiLabel> domain, List<Prediction<MultiLabel>> predictions) {
Expand Down