label_binarize 没有输出正确数量的 类

label_binarize not outputting the correct number of classes

当我使用 label_binarize 时,我没有得到正确的 类 数,即使我指定了它。这是我的简单代码:

import numpy as np
from sklearn.preprocessing import label_binarize

y = ['tap', 'not_tap', 'tap', 'tap', 'not_tap', 'tap', 'not_tap','not_tap']

y = label_binarize(y, classes=[0, 1])
n_classes = y.shape[1]

我得到 n_classes= 1。使用此代码时,我收到 警告消息:

FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  mask |= (ar1 == a)

你能告诉我如何像这个例子一样正确地得到 n_classes = 2 吗?

谢谢!

label_binarize 以 one-vs-all 方式对值进行二值化

考虑这个例子

from sklearn.preprocessing import label_binarize
print(label_binarize([1, 6], classes=[1, 2, 4, 6]))

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

列是 classes [1,2,4,6],1 表示该值是否匹配 class。

您现在调用它的方式 (label_binarize(y, classes=[0, 1])),none 个值 (tap,no_tap) 匹配任何 classes (0 ,1) 因此所有值都是 0.

您要找的是 LabelBinarizer

from sklearn.preprocessing import LabelBinarizer

y = ['tap', 'not_tap', 'tap', 'tap', 'not_tap', 'tap', 'not_tap','not_tap']
lb = LabelBinarizer()

label = lb.fit_transform(y)
[[1]
[0]
[1]
[1]
[0]
[1]
[0]
[0]]

n_classes = len(lb.classes_)
#2