@@ -795,9 +795,22 @@ def test_cohen_kappa():
795
795
)
796
796
797
797
798
- def test_matthews_corrcoef_nan ():
799
- assert matthews_corrcoef ([0 ], [1 ]) == 0.0
800
- assert matthews_corrcoef ([0 , 0 ], [0 , 1 ]) == 0.0
798
+ @pytest .mark .parametrize ("zero_division" , ["warn" , 0 , 1 , np .nan ])
799
+ @pytest .mark .parametrize ("y_true, y_pred" , [([0 ], [1 ]), ([0 , 0 ], [0 , 1 ])])
800
+ def test_matthews_corrcoef_zero_division (zero_division , y_true , y_pred ):
801
+ """Check the behaviour of `zero_division` in `matthews_corrcoef`."""
802
+ expected_result = 0.0 if zero_division == "warn" else zero_division
803
+
804
+ if zero_division == "warn" :
805
+ with pytest .warns (UndefinedMetricWarning ):
806
+ result = matthews_corrcoef (y_true , y_pred , zero_division = zero_division )
807
+ else :
808
+ result = matthews_corrcoef (y_true , y_pred , zero_division = zero_division )
809
+
810
+ if np .isnan (expected_result ):
811
+ assert np .isnan (result )
812
+ else :
813
+ assert result == expected_result
801
814
802
815
803
816
@pytest .mark .parametrize ("zero_division" , [0 , 1 , np .nan ])
@@ -924,15 +937,19 @@ def test_matthews_corrcoef():
924
937
925
938
# For the zero vector case, the corrcoef cannot be calculated and should
926
939
# output 0
927
- assert_almost_equal (matthews_corrcoef ([0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ]), 0.0 )
940
+ assert_almost_equal (
941
+ matthews_corrcoef ([0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ], zero_division = 0 ), 0.0
942
+ )
928
943
929
944
# And also for any other vector with 0 variance
930
- assert_almost_equal (matthews_corrcoef (y_true , ["a" ] * len (y_true )), 0.0 )
945
+ assert_almost_equal (
946
+ matthews_corrcoef (y_true , ["a" ] * len (y_true ), zero_division = 0 ), 0.0
947
+ )
931
948
932
949
# These two vectors have 0 correlation and hence mcc should be 0
933
950
y_1 = [1 , 0 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 ]
934
951
y_2 = [1 , 1 , 1 , 0 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 ]
935
- assert_almost_equal (matthews_corrcoef (y_1 , y_2 ), 0.0 )
952
+ assert_almost_equal (matthews_corrcoef (y_1 , y_2 , zero_division = 0 ), 0.0 )
936
953
937
954
# Check that sample weight is able to selectively exclude
938
955
mask = [1 ] * 10 + [0 ] * 10
@@ -965,17 +982,17 @@ def test_matthews_corrcoef_multiclass():
965
982
# Zero variance will result in an mcc of zero
966
983
y_true = [0 , 1 , 2 ]
967
984
y_pred = [3 , 3 , 3 ]
968
- assert_almost_equal (matthews_corrcoef (y_true , y_pred ), 0.0 )
985
+ assert_almost_equal (matthews_corrcoef (y_true , y_pred , zero_division = 0 ), 0.0 )
969
986
970
987
# Also for ground truth with zero variance
971
988
y_true = [3 , 3 , 3 ]
972
989
y_pred = [0 , 1 , 2 ]
973
- assert_almost_equal (matthews_corrcoef (y_true , y_pred ), 0.0 )
990
+ assert_almost_equal (matthews_corrcoef (y_true , y_pred , zero_division = 0 ), 0.0 )
974
991
975
992
# These two vectors have 0 correlation and hence mcc should be 0
976
993
y_1 = [0 , 1 , 2 , 0 , 1 , 2 , 0 , 1 , 2 ]
977
994
y_2 = [1 , 1 , 1 , 2 , 2 , 2 , 0 , 0 , 0 ]
978
- assert_almost_equal (matthews_corrcoef (y_1 , y_2 ), 0.0 )
995
+ assert_almost_equal (matthews_corrcoef (y_1 , y_2 , zero_division = 0 ), 0.0 )
979
996
980
997
# We can test that binary assumptions hold using the multiclass computation
981
998
# by masking the weight of samples not in the first two classes
@@ -994,7 +1011,10 @@ def test_matthews_corrcoef_multiclass():
994
1011
y_pred = [0 , 0 , 1 , 2 ]
995
1012
sample_weight = [1 , 1 , 0 , 0 ]
996
1013
assert_almost_equal (
997
- matthews_corrcoef (y_true , y_pred , sample_weight = sample_weight ), 0.0
1014
+ matthews_corrcoef (
1015
+ y_true , y_pred , sample_weight = sample_weight , zero_division = 0.0
1016
+ ),
1017
+ 0.0 ,
998
1018
)
999
1019
1000
1020
0 commit comments