def to_one_hot(y):
"""Transform multi-class labels to binary labels
The output of to_one_hot is sometimes referred to by some authors as the
1-of-K coding scheme.
Parameters
----------
y : numpy array or sparse matrix of shape (n_samples,) or
(n_samples, n_classes) Target values. The 2-d matrix should only
contain 0 and 1, represents multilabel classification. Sparse
matrix can be CSR, CSC, COO, DOK, or LIL.
Returns
-------
Y : numpy array or CSR matrix of shape [n_samples, n_classes]
Shape will be [n_samples, 1] for binary problems.
classes_ : class vector extraceted from y.
"""
lb = LabelBinarizer()
lb.fit(y)
Y = lb.transform(y)
return (Y.base, lb.classes_)
评论列表
文章目录