From 4911e73cea1a7b8e2bf2f381b2cd286edbb4c6d0 Mon Sep 17 00:00:00 2001 From: Kanchana Ranasinghe Date: Wed, 8 Mar 2017 23:30:02 +0530 Subject: [PATCH 1/4] Fix improper wrapping --- sklearn/metrics/tests/test_common.py | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/sklearn/metrics/tests/test_common.py b/sklearn/metrics/tests/test_common.py index 00db32e1ef389..2399f8339b359 100644 --- a/sklearn/metrics/tests/test_common.py +++ b/sklearn/metrics/tests/test_common.py @@ -369,6 +369,7 @@ # No Sample weight support METRICS_WITHOUT_SAMPLE_WEIGHT = [ + "cohen_kappa_score", "confusion_matrix", # Left this one here because the tests in this file do # not work for confusion_matrix, as its output is a # matrix instead of a number. Testing of @@ -888,7 +889,7 @@ def test_averaging_multiclass(n_samples=50, n_classes=3): lb = LabelBinarizer().fit(y_true) y_true_binarize = lb.transform(y_true) - y_pred_binarize = lb.transform(y_pred) + y_pred_binarize = lb.transform(y_pred) for name in METRICS_WITH_AVERAGING: yield (_named_check(check_averaging, name), name, y_true, @@ -1011,19 +1012,9 @@ def check_sample_weight_invariance(name, metric, y1, y2): sample_weight=np.hstack([sample_weight, sample_weight])) -def test_sample_weight_invariance(n_samples=50): +def generate_sample_weight_invariance(n_samples=50): + #create generator function to go through each relevant metric random_state = check_random_state(0) - # regression - y_true = random_state.random_sample(size=(n_samples,)) - y_pred = random_state.random_sample(size=(n_samples,)) - for name in ALL_METRICS: - if name not in REGRESSION_METRICS: - continue - if name in METRICS_WITHOUT_SAMPLE_WEIGHT: - continue - metric = ALL_METRICS[name] - yield _named_check(check_sample_weight_invariance, name), name,\ - metric, y_true, y_pred # binary random_state = check_random_state(0) @@ -1031,8 +1022,6 @@ def test_sample_weight_invariance(n_samples=50): y_pred = random_state.randint(0, 2, size=(n_samples, )) y_score = random_state.random_sample(size=(n_samples,)) for name in ALL_METRICS: - if name in REGRESSION_METRICS: - continue if (name in METRICS_WITHOUT_SAMPLE_WEIGHT or name in METRIC_UNDEFINED_BINARY): continue @@ -1050,8 +1039,6 @@ def test_sample_weight_invariance(n_samples=50): y_pred = random_state.randint(0, 5, size=(n_samples, )) y_score = random_state.random_sample(size=(n_samples, 5)) for name in ALL_METRICS: - if name in REGRESSION_METRICS: - continue if (name in METRICS_WITHOUT_SAMPLE_WEIGHT or name in METRIC_UNDEFINED_BINARY_MULTICLASS): continue @@ -1087,6 +1074,10 @@ def test_sample_weight_invariance(n_samples=50): yield (_named_check(check_sample_weight_invariance, name), name, metric, y_true, y_pred) +def test_sample_weight_invariance(n_samples=50): + #iterate through each metric in generator function + for metrics in generate_sample_weight_invariance(n_samples): + pass @ignore_warnings def test_no_averaging_labels(): From c550ab8dba468ca6bd0e03006ce0bb42ad41790f Mon Sep 17 00:00:00 2001 From: Kanchana Ranasinghe Date: Wed, 8 Mar 2017 23:52:43 +0530 Subject: [PATCH 2/4] Fix wrapping of test_sample_weight_invariance function --- sklearn/metrics/tests/test_common.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/sklearn/metrics/tests/test_common.py b/sklearn/metrics/tests/test_common.py index 2399f8339b359..045abe8765589 100644 --- a/sklearn/metrics/tests/test_common.py +++ b/sklearn/metrics/tests/test_common.py @@ -369,7 +369,6 @@ # No Sample weight support METRICS_WITHOUT_SAMPLE_WEIGHT = [ - "cohen_kappa_score", "confusion_matrix", # Left this one here because the tests in this file do # not work for confusion_matrix, as its output is a # matrix instead of a number. Testing of @@ -889,7 +888,7 @@ def test_averaging_multiclass(n_samples=50, n_classes=3): lb = LabelBinarizer().fit(y_true) y_true_binarize = lb.transform(y_true) - y_pred_binarize = lb.transform(y_pred) + y_pred_binarize = lb.transform(y_pred) for name in METRICS_WITH_AVERAGING: yield (_named_check(check_averaging, name), name, y_true, @@ -1013,8 +1012,19 @@ def check_sample_weight_invariance(name, metric, y1, y2): def generate_sample_weight_invariance(n_samples=50): - #create generator function to go through each relevant metric + #create generative function to iterate through each relevant metric random_state = check_random_state(0) + # regression + y_true = random_state.random_sample(size=(n_samples,)) + y_pred = random_state.random_sample(size=(n_samples,)) + for name in ALL_METRICS: + if name not in REGRESSION_METRICS: + continue + if name in METRICS_WITHOUT_SAMPLE_WEIGHT: + continue + metric = ALL_METRICS[name] + yield _named_check(check_sample_weight_invariance, name), name,\ + metric, y_true, y_pred # binary random_state = check_random_state(0) @@ -1022,6 +1032,8 @@ def generate_sample_weight_invariance(n_samples=50): y_pred = random_state.randint(0, 2, size=(n_samples, )) y_score = random_state.random_sample(size=(n_samples,)) for name in ALL_METRICS: + if name in REGRESSION_METRICS: + continue if (name in METRICS_WITHOUT_SAMPLE_WEIGHT or name in METRIC_UNDEFINED_BINARY): continue @@ -1039,6 +1051,8 @@ def generate_sample_weight_invariance(n_samples=50): y_pred = random_state.randint(0, 5, size=(n_samples, )) y_score = random_state.random_sample(size=(n_samples, 5)) for name in ALL_METRICS: + if name in REGRESSION_METRICS: + continue if (name in METRICS_WITHOUT_SAMPLE_WEIGHT or name in METRIC_UNDEFINED_BINARY_MULTICLASS): continue @@ -1075,7 +1089,6 @@ def generate_sample_weight_invariance(n_samples=50): metric, y_true, y_pred) def test_sample_weight_invariance(n_samples=50): - #iterate through each metric in generator function for metrics in generate_sample_weight_invariance(n_samples): pass From 451a30c1a396748a4107da6503bf56c4694de042 Mon Sep 17 00:00:00 2001 From: Kanchana Ranasinghe Date: Thu, 9 Mar 2017 14:59:33 +0530 Subject: [PATCH 3/4] wrapping improved --- sklearn/metrics/tests/test_common.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sklearn/metrics/tests/test_common.py b/sklearn/metrics/tests/test_common.py index 045abe8765589..9575e97f87089 100644 --- a/sklearn/metrics/tests/test_common.py +++ b/sklearn/metrics/tests/test_common.py @@ -1012,7 +1012,7 @@ def check_sample_weight_invariance(name, metric, y1, y2): def generate_sample_weight_invariance(n_samples=50): - #create generative function to iterate through each relevant metric + # create generative function to iterate through each relevant metric random_state = check_random_state(0) # regression y_true = random_state.random_sample(size=(n_samples,)) @@ -1089,6 +1089,8 @@ def generate_sample_weight_invariance(n_samples=50): metric, y_true, y_pred) def test_sample_weight_invariance(n_samples=50): + # iterate through each metric testing each case + for metrics in generate_sample_weight_invariance(n_samples): pass From 5874a0849c7d6595cae3e7f073f6e993ac76a5c6 Mon Sep 17 00:00:00 2001 From: Kanchana Ranasinghe Date: Thu, 9 Mar 2017 20:26:04 +0530 Subject: [PATCH 4/4] wrapping --- sklearn/metrics/tests/test_common.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/sklearn/metrics/tests/test_common.py b/sklearn/metrics/tests/test_common.py index 9575e97f87089..5b9f6d2b69027 100644 --- a/sklearn/metrics/tests/test_common.py +++ b/sklearn/metrics/tests/test_common.py @@ -369,11 +369,11 @@ # No Sample weight support METRICS_WITHOUT_SAMPLE_WEIGHT = [ - "confusion_matrix", # Left this one here because the tests in this file do - # not work for confusion_matrix, as its output is a - # matrix instead of a number. Testing of - # confusion_matrix with sample_weight is in - # test_classification.py + "confusion_matrix", # Left this one here because the tests in this file do + # not work for confusion_matrix, as its output is a + # matrix instead of a number. Testing of + # confusion_matrix with sample_weight is in + # test_classification.py "median_absolute_error", ] @@ -619,9 +619,9 @@ def test_invariance_string_vs_numbers_labels(): def test_inf_nan_input(): - invalids =[([0, 1], [np.inf, np.inf]), - ([0, 1], [np.nan, np.nan]), - ([0, 1], [np.nan, np.inf])] + invalids = [([0, 1], [np.inf, np.inf]), + ([0, 1], [np.nan, np.nan]), + ([0, 1], [np.nan, np.inf])] METRICS = dict() METRICS.update(THRESHOLDED_METRICS) @@ -1088,12 +1088,13 @@ def generate_sample_weight_invariance(n_samples=50): yield (_named_check(check_sample_weight_invariance, name), name, metric, y_true, y_pred) + def test_sample_weight_invariance(n_samples=50): # iterate through each metric testing each case - for metrics in generate_sample_weight_invariance(n_samples): pass + @ignore_warnings def test_no_averaging_labels(): # test labels argument when not using averaging