scikit-learn:4.8. Transforming the prediction target (y)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.