From a2d1c59e8b02559b35c7e836791b42033cfa284c Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Mon, 29 Mar 2021 22:05:07 -0400 Subject: [PATCH 1/3] FIX Uses proper max from colormap in ConfusionMatrixDisplay --- sklearn/metrics/_plot/confusion_matrix.py | 2 +- .../_plot/tests/test_confusion_matrix_display.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/sklearn/metrics/_plot/confusion_matrix.py b/sklearn/metrics/_plot/confusion_matrix.py index 9fcecec775e6e..891ceede25b9d 100644 --- a/sklearn/metrics/_plot/confusion_matrix.py +++ b/sklearn/metrics/_plot/confusion_matrix.py @@ -122,7 +122,7 @@ def plot(self, *, include_values=True, cmap='viridis', n_classes = cm.shape[0] self.im_ = ax.imshow(cm, interpolation='nearest', cmap=cmap) self.text_ = None - cmap_min, cmap_max = self.im_.cmap(0), self.im_.cmap(256) + cmap_min, cmap_max = self.im_.cmap(0), self.im_.cmap(1.0) if include_values: self.text_ = np.empty_like(cm, dtype=object) diff --git a/sklearn/metrics/_plot/tests/test_confusion_matrix_display.py b/sklearn/metrics/_plot/tests/test_confusion_matrix_display.py index ed0bc04117396..ed5d89858fb20 100644 --- a/sklearn/metrics/_plot/tests/test_confusion_matrix_display.py +++ b/sklearn/metrics/_plot/tests/test_confusion_matrix_display.py @@ -380,3 +380,17 @@ def test_confusion_matrix_with_unknown_labels(pyplot, constructor_name): display_labels = [tick.get_text() for tick in disp.ax_.get_xticklabels()] expected_labels = [str(i) for i in range(n_classes + 1)] assert_array_equal(expected_labels, display_labels) + + +def test_colormap_max(pyplot): + """Check that the max color is used for the color of the text.""" + + from matplotlib import cm + gray = cm.get_cmap('gray', 1024) + cm = np.array([[1.0, 0.0], [0.0, 1.0]]) + + disp = ConfusionMatrixDisplay(cm) + disp.plot(cmap=gray) + + color = disp.text_[1, 0].get_color() + assert_allclose(color, [1.0, 1.0, 1.0, 1.0]) From 1727b76fbdecfb1f36ec6dace6dc4b976ccb12b3 Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Mon, 29 Mar 2021 22:09:16 -0400 Subject: [PATCH 2/3] DOC Adds whats new --- doc/whats_new/v1.0.rst | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/doc/whats_new/v1.0.rst b/doc/whats_new/v1.0.rst index be894774f5a27..3c773eee18a78 100644 --- a/doc/whats_new/v1.0.rst +++ b/doc/whats_new/v1.0.rst @@ -150,7 +150,7 @@ Changelog - |Feature| The new :class:`linear_model.SGDOneClassSVM` provides an SGD implementation of the linear One-Class SVM. Combined with kernel approximation techniques, this implementation approximates the solution of - a kernelized One Class SVM while benefitting from a linear + a kernelized One Class SVM while benefitting from a linear complexity in the number of samples. :pr:`10027` by :user:`Albert Thomas `. @@ -224,6 +224,9 @@ Changelog are integral. :pr:`9843` by :user:`Jon Crall `. +- |Fix| :meth:`metrics.ConfusionMatrixDisplay.plot` uses the correct max + for colormap. :pr:`19784` by `Thomas Fan`_. + :mod:`sklearn.model_selection` .............................. @@ -281,10 +284,10 @@ Changelog :mod:`sklearn.utils` .................... -- |Enhancement| Deprecated the default value of the `random_state=0` in +- |Enhancement| Deprecated the default value of the `random_state=0` in :func:`~sklearn.utils.extmath.randomized_svd`. Starting in 1.2, the default value of `random_state` will be set to `None`. - :pr:`19459` by :user:`Cindy Bezuidenhout ` and + :pr:`19459` by :user:`Cindy Bezuidenhout ` and :user:`Clifford Akai-Nettey`. :mod:`sklearn.calibration` From 9b625cfeaac677f20b57b68288bf3a799219ec5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Dupr=C3=A9=20la=20Tour?= Date: Mon, 7 Jun 2021 16:38:21 -0700 Subject: [PATCH 3/3] Use variable name not colliding with matplotlib.cm --- sklearn/metrics/_plot/tests/test_confusion_matrix_display.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn/metrics/_plot/tests/test_confusion_matrix_display.py b/sklearn/metrics/_plot/tests/test_confusion_matrix_display.py index ed5d89858fb20..b1498afae89ae 100644 --- a/sklearn/metrics/_plot/tests/test_confusion_matrix_display.py +++ b/sklearn/metrics/_plot/tests/test_confusion_matrix_display.py @@ -387,9 +387,9 @@ def test_colormap_max(pyplot): from matplotlib import cm gray = cm.get_cmap('gray', 1024) - cm = np.array([[1.0, 0.0], [0.0, 1.0]]) + confusion_matrix = np.array([[1.0, 0.0], [0.0, 1.0]]) - disp = ConfusionMatrixDisplay(cm) + disp = ConfusionMatrixDisplay(confusion_matrix) disp.plot(cmap=gray) color = disp.text_[1, 0].get_color()