Fork me on GitHub

sklearn.preprocessing.label_binarize

sklearn.preprocessing.label_binarize(y, classes, multilabel=False, neg_label=0, pos_label=1)

Binarize labels in a one-vs-all fashion

Several regression and binary classification algorithms are available in the scikit. A simple way to extend these algorithms to the multi-class classification case is to use the so-called one-vs-all scheme.

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.

multilabel : boolean

Set to true if y is encoding a multilabel tasks (with a variable number of label assignements per sample) rather than a multiclass task where one sample has one and only one label assigned.

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.

Returns :

Y : numpy array of shape [n_samples, n_classes]

See also

label_binarize
function to perform the transform operation of LabelBinarizer with fixed classes.

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]])
>>> label_binarize([(1, 2), (6,), ()], multilabel=True,
...                classes=[1, 6, 4, 2])
array([[1, 0, 0, 1],
       [0, 1, 0, 0],
       [0, 0, 0, 0]])
Previous
Next