label_binarize

 label_binarize(y, *, classes, neg_label=0, pos_label=1,
                   sparse_output=False):
    """Binarize labels in a one-vs-all fashion
    Several regression and binary classification algorithms are
    available in scikit-learn. A simple way to extend these algorithms
    to the multi-class classification case is to use the so-called
    one-vs-all scheme.
    以一对多的方式二值化标签
     scikit-learn 中提供了几种回归和二元分类算法。 将这些算法扩展到多类分类情况的一种简单方法是使用所谓的“一对一”方案。
    This function makes it possible to compute this transformation for a
    fixed set of class labels known ahead of time.
    此函数可以为提前已知的一组固定类标签计算此转换。
    Parameters参数
    ----------
    y : array-like
        Sequence of integer labels or multilabel data to encode.#要编码的整数标签或多标签数据的序列。
    classes : array-like of shape [n_classes]
        Uniquely holds the label for each class.#唯一地保存每个类的标签。
    neg_label : int (default: 0)
        Value with which negative labels must be encoded.#负标签必须编码的值。
    pos_label : int (default: 1)
        Value with which positive labels must be encoded.#必须对正标签进行编码的值。
    sparse_output : boolean (default: False),
        Set to true if output binary array is desired in CSR sparse format#如果需要 CSR 稀疏格式的输出二进制数组,则设置为 true
    Returns
    -------
    Y : numpy array or CSR matrix of shape [n_samples, n_classes]
        Shape will be [n_samples, 1] for binary problems.#对于二元问题,形状将为 [n_samples, 1]。
    Examples
    --------
    >>> from sklearn.preprocessing import label_binarize
    >>> label_binarize([1, 6], classes=[1, 2, 4, 6])
    array([[1, 0, 0, 0],
           [0, 0, 0, 1]])
    The class ordering is preserved:
    >>> label_binarize([1, 6], classes=[1, 6, 4, 2])
    array([[1, 0, 0, 0],
           [0, 1, 0, 0]])
    Binary targets transform to a column vector
    >>> label_binarize(['yes', 'no', 'no', 'yes'], classes=['no', 'yes'])
    array([[1],
           [0],
           [0],
           [1]])
    See also
    --------
    LabelBinarizer : class used to wrap the functionality of label_binarize and
        allow for fitting to classes independently of the transform operation
    """

记录一下遇到的问题,原来是三分类的问题使用这个函数没有问题,但现在要修改适配二分类的问题,就遇到很多bug,代码里要修改的地方比较多,能修改好的话就更新。

解决了,总结下。

label_binarize对于两个以上的分类,可以将1维转化为多维,对于二分类,就还是一维,为了适应下面的代码,需要将二分类的转化为二维才行。

np_utils.to_categorical(train, 2)将原来标签是一列的[1,0,0,0,1...]的转换为一行两列的独热码。

人傻了,想了老半天,代码看了好多遍,搜了好多东西,都理解透了,差点想重新写了,然后一行代码搞定,道行还是太浅!。

猜你喜欢

转载自blog.csdn.net/qq_40016005/article/details/121445503