From 6536901bb2892e8c5f9910da5ca1962fdc9a44be Mon Sep 17 00:00:00 2001 From: "bibhash.mitra" Date: Fri, 13 Dec 2019 12:07:08 +0530 Subject: [PATCH 1/4] Corrected the missing zero_division argument --- sklearn/metrics/_classification.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sklearn/metrics/_classification.py b/sklearn/metrics/_classification.py index c7101f7a38eeb..b1a67e520916c 100644 --- a/sklearn/metrics/_classification.py +++ b/sklearn/metrics/_classification.py @@ -1960,7 +1960,8 @@ class 2 1.00 0.67 0.80 3 # compute averages with specified averaging method avg_p, avg_r, avg_f1, _ = precision_recall_fscore_support( y_true, y_pred, labels=labels, - average=average, sample_weight=sample_weight) + average=average, sample_weight=sample_weight, + zero_division=zero_division) avg = [avg_p, avg_r, avg_f1, np.sum(s)] if output_dict: From cedaa0f1da999493f80850049178c483034ba9f6 Mon Sep 17 00:00:00 2001 From: Olivier Grisel Date: Sat, 21 Dec 2019 00:05:55 +0100 Subject: [PATCH 2/4] Non-regression test for classification_report zero_division warning control --- sklearn/metrics/tests/test_classification.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/sklearn/metrics/tests/test_classification.py b/sklearn/metrics/tests/test_classification.py index 66ea486f955b7..07ef3fc2bd2d1 100644 --- a/sklearn/metrics/tests/test_classification.py +++ b/sklearn/metrics/tests/test_classification.py @@ -20,7 +20,6 @@ from sklearn.utils._testing import assert_array_equal from sklearn.utils._testing import assert_array_almost_equal from sklearn.utils._testing import assert_allclose -from sklearn.utils._testing import assert_warns from sklearn.utils._testing import assert_warns_div0 from sklearn.utils._testing import assert_no_warnings from sklearn.utils._testing import assert_warns_message @@ -154,6 +153,23 @@ def test_classification_report_dictionary_output(): assert type(expected_report['macro avg']['support']) == int +@pytest.mark.parametrize('zero_division', ["warn", 0, 1]) +def test_classification_report_zero_division_warning(zero_division): + y_true, y_pred = ["a", "b", "c"], ["a", "b", "d"] + with warnings.catch_warnings(record=True) as record: + warnings.simplefilter('always') + classification_report( + y_true, y_pred, zero_division=zero_division, output_dict=True) + if zero_division == "warn": + assert len(record) > 1 + for item in record: + msg = ("Use `zero_division` parameter to control this " + "behavior.") + assert msg in str(item.message) + else: + assert record == [] + + def test_multilabel_accuracy_score_subset_accuracy(): # Dense label indicator matrix format y1 = np.array([[0, 1, 1], [1, 0, 1]]) From af53e34f92e8d71609b7ba4aee869158444b12b3 Mon Sep 17 00:00:00 2001 From: Olivier Grisel Date: Sat, 21 Dec 2019 00:11:24 +0100 Subject: [PATCH 3/4] Document change for 0.22.1 --- doc/whats_new/v0.22.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/whats_new/v0.22.rst b/doc/whats_new/v0.22.rst index be5688d3a32ae..0bc56f4366652 100644 --- a/doc/whats_new/v0.22.rst +++ b/doc/whats_new/v0.22.rst @@ -42,6 +42,10 @@ Changelog is invalid. Previously, it runs fine with no normalization. :pr:`15888` by `Hanmin Qin`_. +- |Fix| :func:`metrics.classification_report` does no longer ignore the + value of the ``zero_division`` keyword argument. :pr:`15879` + by :user:`Bibhash Chandra Mitra `. + :mod:`sklearn.utils` .................... From 30f11d1c65c02d30f52a826a43caf3a41537515d Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Sat, 21 Dec 2019 18:14:20 +0100 Subject: [PATCH 4/4] cosmit --- sklearn/metrics/tests/test_classification.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sklearn/metrics/tests/test_classification.py b/sklearn/metrics/tests/test_classification.py index e88482f57205b..947ca047438d8 100644 --- a/sklearn/metrics/tests/test_classification.py +++ b/sklearn/metrics/tests/test_classification.py @@ -157,7 +157,6 @@ def test_classification_report_dictionary_output(): def test_classification_report_zero_division_warning(zero_division): y_true, y_pred = ["a", "b", "c"], ["a", "b", "d"] with warnings.catch_warnings(record=True) as record: - warnings.simplefilter('always') classification_report( y_true, y_pred, zero_division=zero_division, output_dict=True) if zero_division == "warn": @@ -167,7 +166,7 @@ def test_classification_report_zero_division_warning(zero_division): "behavior.") assert msg in str(item.message) else: - assert record == [] + assert not record def test_multilabel_accuracy_score_subset_accuracy():