Reference: http://scikit-learn.org/stable/modules/preprocessing_targets.html
There's nothing good to translate, just give examples.
1. Label binarization
Labelbinarizer is a utility class to help create a label indicator matrix from a list of Multi-Class lab Els
>>>
>>> from Sklearn Import preprocessing>>>lb = preprocessing.Labelbinarizer()>>>lb.Fit([1, 2, 6, 4, 2])Labelbinarizer (neg_label=0, pos_label=1, Sparse_output=false) >>> lb. Classes_ Array ([1, 2, 4, 6]) >>>lb.Transform([1, 6])Array ([[1, 0, 0, 0],[0, 0, 0, 1]])
Binary targets transform to a column vector
>>>
>>>lb = preprocessing.Labelbinarizer()>>>lb.Fit_transform([' yes ', ' No ', ' No ', ' yes '])Array ([[1],[0],[0],[1]])
Passing a 2D matrix for Multilabel classification
>>>
>>>Import NumPy as NP>>>lb.Fit(NP.Array([[0, 1, 1], [1, 0, 0]]))Labelbinarizer (neg_label=0, pos_label=1, Sparse_output=false)>>>lb.Classes_Array ([0, 1, 2])>>>lb.Transform([0, 1, 2, 1])Array ([[1, 0, 0],[0, 1, 0],[0, 0, 1],[0, 1, 0]])
For multiple labels per instance, use Multilabelbinarizer:
>>>
>>>lb = preprocessing.Multilabelbinarizer()>>>lb.Fit_transform([(1, 2), (3,)])Array ([[1, 1, 0],[0, 0, 1]]) >>> lb. Classes_ Array ([1, 2, 3])
2, lable encoding
Labelencoder is a utility class to help normalize labels such this they contain only values between 0 and N_c Lasses-1. Labelencoder can used as follows:
>>>
>>> from Sklearn Import preprocessing>>>le = preprocessing.Labelencoder()>>>le.Fit([1, 2, 2, 6])Labelencoder ()>>>le.Classes_Array ([1, 2, 6]) >>> le. Transform ([1, 1, 2, 6]) Array ([0, 0, 1, 2]) >>>le.Inverse_transform([0, 0, 1, 2])Array ([1, 1, 2, 6])
It can also is used to transform non-numerical labels (as long as they is hashable and comparable) to numerical Labels
>>>
>>>le = preprocessing.Labelencoder()>>>le.Fit(["Paris", "Paris", "Tokyo", "Amsterdam"])Labelencoder ()>>>List(le.Classes_)[' Amsterdam ', ' Paris ', ' Tokyo ']>>>le.Transform(["Tokyo", "Tokyo", "Paris"])Array ([2, 2, 1])>>>List(le.Inverse_transform([2, 2, 1]))[' Tokyo ', ' Tokyo ', ' Paris ']
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
scikit-learn:4.8. Transforming the prediction target (y)