question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

formatting of values in plot_confusion_matrix

See original GitHub issue

I’m clearly overlooking something because I’m very confused. Right now the default for values_format is .2g with seems not ideal as it formats 110 as 1.1+e01:

"{:.2g}".format(110)

‘1.1e+02’

Changing the precision doesn’t really help. There’s a pretty easy way to fix this, but not with any standard python formatting from what I can see: https://stackoverflow.com/questions/4626338/python-float-formatting-like-g-but-with-more-digits

Here’s a comparison:

from math import log10
for numbers in [1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789]:
    print("digits: ", len(str(numbers)))
    print(".2g:", "{:.2g}".format(numbers))
    print(".5g:", "{:.5g}".format(numbers))
    print("why:", "{:.2g}".format(numbers) if log10(numbers) > 7 else "{:d}".format(numbers))

The last one is always shorter and more readable than .2g as it uses the scientific notation only when doing so makes the overall representation shorten.

I think having this as the default would be much more user friendly, though I feel this should be a python built-in. Right now the default is None so we could just replace the None behavior by this.

Wdyt?

cc @thomasjpfan

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
j4ggrcommented, Jan 16, 2020

This argument “values_format” seems not to work anyway:

fig, ax = plt.subplots(1, 1)
disp = plot_confusion_matrix(
    log_model, X_test, y_test,
    normalize='true', 
    display_labels=['yes', 'no'], 
    values_format='.4f', 
    ax=ax)
ax.set_title('Confusion matrix, normalized')
ax.xaxis.tick_top()
ax.xaxis.set_label_position('top')

confusion_matrix_plot.pdf

1reaction
cmarmocommented, Mar 5, 2020

Closed by #16159

Read more comments on GitHub >

github_iconTop Results From Across the Web

matplotlib - How to format number in the plot_confusion_matrix
Try passing a blank value format as the argument to the plot_confusion_matrix . The docs state. values_format : str, default=None.
Read more >
Change number format of confusion matrix
I would like to change the format of the numbers that, when they exceed the value 99, appear in scientific format. I would...
Read more >
sklearn.metrics.ConfusionMatrixDisplay
Plot the confusion matrix given an estimator, the data, and the label. ConfusionMatrixDisplay. ... Format specification for values in confusion matrix.
Read more >
Plot a confusion matrix - YouTube
... 0.22: Plot a confusion matrix in one line of code!Highly customizable, including the colormap, display labels, and value formatting.
Read more >
Confusion Matrix Visualization - Medium
The confusion matrix is a 2 dimensional array comparing predicted category labels to the true label. For binary classification, these are the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found