from sklearn.preprocessing import label_binarize
label_binarize(y, *, classes, neg_label=0, pos_label=1, sparse_output=False)
- Parameters(参数)
y : array-like
要编码的整数数据或多标签数据序列
classes : array-like of shape (n_classes,)
np.arange(c)
neg_label : int, default=0
负标签必须编码的值
pos_label : int, default=1
正标签必须编码的值
sparse_output : bool, default=False,
如果需要以CSR稀疏格式输出二进制数组,则设置为true。
- Returns(返回)
Y : {ndarray, sparse matrix} of shape (n_samples, n_classes)
对于二进制问题,形状将为(n_samples,1)
官方案例
>>> from sklearn.preprocessing import label_binarize
>>> label_binarize([1, 6], classes=[1, 2, 4, 6])
array([[1, 0, 0, 0],
[0, 0, 0, 1]])
>>> label_binarize([1, 6], classes=[1, 6, 4, 2])
array([[1, 0, 0, 0],
[0, 1, 0, 0]])
>>> label_binarize(['yes', 'no', 'no', 'yes'], classes=['no', 'yes'])
array([[1],
[0],
[0],
[1]])