GitHub PullRequestReviewCommentEvent at 2014-02-27 09:48:09
Repository: scikit-learn
Actor: arjoly
Payload: {"comment":{"url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/comments/10117222","id":10117222,"diff_hunk":"@@ -253,3 +254,143 @@ def test_label_binarize_with_multilabel_indicator():\n \n     output = lb.fit(y).transform(y)\n     assert_array_equal(output, expected)\n+\n+\n+def test_mutlilabel_binarizer():\n+    # test input as iterable of iterables\n+    inputs = [\n+        lambda: [(2, 3), (1,), (1, 2)],\n+        lambda: (set([2, 3]), set([1]), set([1, 2])),\n+        lambda: iter([iter((2, 3)), iter((1,)), set([1, 2])]),\n+    ]\n+    indicator_mat = np.array([[0, 1, 1],\n+                              [1, 0, 0],\n+                              [1, 1, 0]])\n+    inverse = inputs[0]()\n+    for inp in inputs:\n+        # With fit_tranform\n+        mlb = MultiLabelBinarizer()\n+        got = mlb.fit_transform(inp())\n+        assert_array_equal(indicator_mat, got)\n+        assert_array_equal([1, 2, 3], mlb.classes_)\n+        assert_equal(mlb.inverse_transform(got), inverse)\n+\n+        # With fit\n+        mlb = MultiLabelBinarizer()\n+        got = mlb.fit(inp()).transform(inp())\n+        assert_array_equal(indicator_mat, got)\n+        assert_array_equal([1, 2, 3], mlb.classes_)\n+        assert_equal(mlb.inverse_transform(got), inverse)\n+\n+\n+def test_mutlilabel_binarizer_empty_sample():\n+    mlb = MultiLabelBinarizer()\n+    y = [[1, 2], [1], []]\n+    Y = np.array([[1, 1],\n+                  [1, 0],\n+                  [0, 0]])\n+    assert_array_equal(mlb.fit_transform(y), Y)\n+\n+\n+def test_mutlilabel_binarizer_unknown_class():\n+    mlb = MultiLabelBinarizer()\n+    y = [[1, 2]]\n+    assert_raises(KeyError, mlb.fit(y).transform, [[0]])\n+\n+    mlb = MultiLabelBinarizer(classes=[1, 2])\n+    assert_raises(KeyError, mlb.fit_transform, [[0]])\n+\n+\n+def test_mutlilabel_binarizer_given_classes():\n+    inp = [(2, 3), (1,), (1, 2)]\n+    indicator_mat = np.array([[0, 1, 1],\n+                              [1, 0, 0],\n+                              [1, 0, 1]])\n+    # fit_transform()\n+    mlb = MultiLabelBinarizer(classes=[1, 3, 2])\n+    assert_array_equal(mlb.fit_transform(inp), indicator_mat)\n+    assert_array_equal(mlb.classes_, [1, 3, 2])\n+\n+    # fit().transform()\n+    mlb = MultiLabelBinarizer(classes=[1, 3, 2])\n+    assert_array_equal(mlb.fit(inp).transform(inp), indicator_mat)\n+    assert_array_equal(mlb.classes_, [1, 3, 2])\n+\n+    # ensure works with extra class\n+    mlb = MultiLabelBinarizer(classes=[4, 1, 3, 2])\n+    assert_array_equal(mlb.fit_transform(inp),\n+                       np.hstack(([[0], [0], [0]], indicator_mat)))\n+    assert_array_equal(mlb.classes_, [4, 1, 3, 2])\n+\n+    # ensure fit is no-op as iterable is not consumed\n+    inp = iter(inp)\n+    mlb = MultiLabelBinarizer(classes=[1, 3, 2])\n+    assert_array_equal(mlb.fit(inp).transform(inp), indicator_mat)\n+\n+\n+def test_mutlilabel_binarizer_same_length_sequence():\n+    \"\"\"Ensure sequences of the same length are not interpreted as a 2-d array\n+    \"\"\"\n+    inp = [[1], [0], [2]]\n+    indicator_mat = np.array([[0, 1, 0],\n+                              [1, 0, 0],\n+                              [0, 0, 1]])\n+    # fit_transform()\n+    mlb = MultiLabelBinarizer()\n+    assert_array_equal(mlb.fit_transform(inp), indicator_mat)\n+    assert_array_equal(mlb.inverse_transform(indicator_mat), inp)\n+\n+    # fit().transform()\n+    mlb = MultiLabelBinarizer()\n+    assert_array_equal(mlb.fit(inp).transform(inp), indicator_mat)\n+    assert_array_equal(mlb.inverse_transform(indicator_mat), inp)\n+\n+\n+def test_mutlilabel_binarizer_non_integer_labels():\n+    tuple_classes = np.empty(3, dtype=object)\n+    tuple_classes[:] = [(1,), (2,), (3,)]\n+    inputs = [\n+        ([('2', '3'), ('1',), ('1', '2')], ['1', '2', '3']),\n+        ([((2,), (3,)), ((1,),), ((1,), (2,))], tuple_classes),\n+    ]","path":"sklearn/preprocessing/tests/test_label.py","position":110,"original_position":110,"commit_id":"230f7e0a98b0a97e9bfd0e05f70266581bcf90d7","original_commit_id":"230f7e0a98b0a97e9bfd0e05f70266581bcf90d7","user":{"login":"arjoly","id":1274722,"avatar_url":"https://avatars.githubusercontent.com/u/1274722","gravatar_id":"9fb38ab677ea6f73bb07faa43b0677f9","url":"https://api.github.com/users/arjoly","html_url":"https://github.com/arjoly","followers_url":"https://api.github.com/users/arjoly/followers","following_url":"https://api.github.com/users/arjoly/following{/other_user}","gists_url":"https://api.github.com/users/arjoly/gists{/gist_id}","starred_url":"https://api.github.com/users/arjoly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arjoly/subscriptions","organizations_url":"https://api.github.com/users/arjoly/orgs","repos_url":"https://api.github.com/users/arjoly/repos","events_url":"https://api.github.com/users/arjoly/events{/privacy}","received_events_url":"https://api.github.com/users/arjoly/received_events","type":"User","site_admin":false},"body":"Yes, it's what I mean.","created_at":"2014-02-27T09:48:09Z","updated_at":"2014-02-27T09:48:09Z","html_url":"https://github.com/scikit-learn/scikit-learn/pull/2657#discussion_r10117222","pull_request_url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/2657","_links":{"self":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/comments/10117222"},"html":{"href":"https://github.com/scikit-learn/scikit-learn/pull/2657#discussion_r10117222"},"pull_request":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/2657"}}}}