Navigation Menu

Skip to content

Commit

Permalink
BUG handle two-class multilabel case in LabelBinarizer
Browse files Browse the repository at this point in the history
Would throw an exception due to special handling of binary case.
  • Loading branch information
larsmans committed Oct 26, 2011
1 parent b5429ab commit f161fab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions sklearn/preprocessing/__init__.py
Expand Up @@ -514,10 +514,10 @@ def transform(self, y):
Y : numpy array of shape [n_samples, n_classes]
"""

if len(self.classes_) == 2:
Y = np.zeros((len(y), 1))
else:
if self.multilabel or len(self.classes_) > 2:
Y = np.zeros((len(y), len(self.classes_)))
else:
Y = np.zeros((len(y), 1))

y_is_multilabel = _is_multilabel(y)

Expand Down
11 changes: 11 additions & 0 deletions sklearn/preprocessing/tests/test_preprocessing.py
Expand Up @@ -308,6 +308,17 @@ def test_label_binarizer_multilabel():
assert_array_equal(expected, got)
assert_equal(lb.inverse_transform(got), inp)

# regression test for the two-class multilabel case
lb = LabelBinarizer()

inp = [[1, 0], [0], [1], [0, 1]]
expected = np.array([[1, 1],
[1, 0],
[0, 1],
[1, 1]])
got = lb.fit_transform(inp)
assert_array_equal(expected, got)


def test_label_binarizer_errors():
"""Check that invalid arguments yield ValueError"""
Expand Down

0 comments on commit f161fab

Please sign in to comment.