8000 [MRG+1] Fix message formatting in exception (#8319) · scikit-learn/scikit-learn@a0db45d · GitHub
[go: up one dir, main page]

Skip to content

Commit a0db45d

Browse files
MMeketonlesteve
authored andcommitted
[MRG+1] Fix message formatting in exception (#8319)
when the missing class label is a string.
1 parent 4493d37 commit a0db45d

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

sklearn/utils/class_weight.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def compute_class_weight(class_weight, classes, y):
6666
for c in class_weight:
6767
i = np.searchsorted(classes, c)
6868
if i >= len(classes) or classes[i] != c:
69-
raise ValueError("Class label %d not present." % c)
69+
raise ValueError("Class label {} not present.".format(c))
7070
else:
7171
weight[i] = class_weight[c]
7272

sklearn/utils/tests/test_class_weight.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ def test_compute_class_weight_not_present():
3131
classes = np.arange(4)
3232
y = np.asarray([0, 0, 0, 1, 1, 2])
3333
assert_raises(ValueError, compute_class_weight, "balanced", classes, y)
34+
# Fix exception in error message formatting when missing label is a string
35+
# https://github.com/scikit-learn/scikit-learn/issues/8312
36+
assert_raise_message(ValueError,
37+
'Class label label_not_present not present',
38+
compute_class_weight,
39+
{'label_not_present': 1.}, classes, y)
3440
# Raise error when y has items not in classes
3541
classes = np.arange(2)
3642
assert_raises(ValueError, compute_class_weight, "balanced", classes, y)

0 commit comments

Comments
 (0)
0