0
$\begingroup$

I have multilabel labels. Elements in a label mean voting. Here is how labels look:

array([[ 4,  0,  0,  1,  3,  2,  0,  0],
       [ 6,  0,  1,  1,  0,  0,  0,  0],
       [ 5,  0,  0,  3,  1,  0,  0,  0],
       [ 4,  0,  0,  4,  1,  0,  0,  0],
       [ 9,  0,  0,  1,  0,  0,  0,  0],
       [ 6,  0,  0,  1,  0,  0,  1,  1],
       [ 2,  0,  0,  8,  0,  0,  0,  0],
       [ 0, 10,  0,  0,  0,  0,  0,  0],
       [ 0, 10,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  6,  0,  0,  0,  4,  0]])

And here is what I tried:

from sklearn.preprocessing import MultiLabelBinarizer
mlb = MultiLabelBinarizer()
nn = mlb.fit_transform(labels_train)
nn[:10]

Output:

array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
       [1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0],
       [1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
       [1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
       [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
       [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
       [1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0]])

And when I tried inverse_transform():

zet = mlb.inverse_transform(nn)
zet[:10]

out:

[(0, 1, 2, 3, 4),
 (0, 1, 6),
 (0, 1, 3, 5),
 (0, 1, 4),
 (0, 1, 9),
 (0, 1, 6),
 (0, 2, 8),
 (0, 10),
 (0, 10),
 (0, 4, 6)]

What am I doing wrong? Why is it showing unique values in ascending order?

$\endgroup$
  • $\begingroup$ Please don't cross-post exact duplicates. When I suggested you ask here, I meant the modeling question "how should I encode/model for this voting target." This question, about MultiLabelBinarizer, was suitable for SO, but the answer was simply "that's what MultiLabelBinarizer is supposed to do." $\endgroup$ – Ben Reiniger Sep 13 '20 at 14:17
2
$\begingroup$

This seems intuitive.
When we have a single-label response (Multi-Class setting), our response prior to encoding looks like:

[1, 4, 2, 3, 5]


After encoding,it becomes

[[1,0,0,0,0], [0,0,0,1,0], [0,1,0,0,0], [0,0,1,0,0], [0,0,0,0,1]]

Indicating the presence of ith Class. But only one. Similarly, in a multi-label setting-
Input should be - [[1,2,5], [4,1], [2,3] [3], [5,4]]
Encoded value - [[1,1,0,0,1], [1,0,0,1,0], [0,1,1,0,0], [0,0,1,0,0], [0,0,0,1,1]]
Indicating the presence of (i,j,k)th Classes. Can be many
There is no need to keep duplicate values in the data before encoding. The inverse function will work in the same way.

The set of labels for each sample such that y[i] consists of classes_[j] for each yt[i, j] == 1.

But if you try to transform again the o/p of inverse, you will get the same encoding.

mlb.transform(zet)

array([
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 1, 0, 0, 0],
[1, 1, 0, 1, 0, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 1, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[1, 1, 0, 0, 0, 0, 1, 0, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 0, 1, 0, 0, 0]])

$\endgroup$
  • $\begingroup$ thanks for the answer. I upvoted $\endgroup$ – Sherzod Sep 16 '20 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.