利用sklearn的LabelEncoder對標籤進行數字化編碼

來源:互聯網
上載者:User

標籤:mixin   nbsp   mst   more   list   logs   imp   tween   標籤   

 

from sklearn.preprocessing import LabelEncoderdef gen_label_encoder():    labels = [‘BB‘, ‘CC‘]      le = LabelEncoder()    le.fit(labels)    print ‘le.classes_‘, le.classes_    for label in le.classes_:        print label, le.transform([label])[0]    joblib.dump(le, ‘data/label_encoder.h5‘)

 

LabelEncoder的說明:

 1 class LabelEncoder(BaseEstimator, TransformerMixin): 2     """Encode labels with value between 0 and n_classes-1. 3  4     Read more in the :ref:`User Guide <preprocessing_targets>`. 5  6     Attributes 7     ---------- 8     classes_ : array of shape (n_class,) 9         Holds the label for each class.10 11     Examples12     --------13     `LabelEncoder` can be used to normalize labels.14 15     >>> from sklearn import preprocessing16     >>> le = preprocessing.LabelEncoder()17     >>> le.fit([1, 2, 2, 6])18     LabelEncoder()19     >>> le.classes_20     array([1, 2, 6])21     >>> le.transform([1, 1, 2, 6]) #doctest: +ELLIPSIS22     array([0, 0, 1, 2]...)23     >>> le.inverse_transform([0, 0, 1, 2])24     array([1, 1, 2, 6])25 26     It can also be used to transform non-numerical labels (as long as they are27     hashable and comparable) to numerical labels.28 29     >>> le = preprocessing.LabelEncoder()30     >>> le.fit(["paris", "paris", "tokyo", "amsterdam"])31     LabelEncoder()32     >>> list(le.classes_)33     [‘amsterdam‘, ‘paris‘, ‘tokyo‘]34     >>> le.transform(["tokyo", "tokyo", "paris"]) #doctest: +ELLIPSIS35     array([2, 2, 1]...)36     >>> list(le.inverse_transform([2, 2, 1]))37     [‘tokyo‘, ‘tokyo‘, ‘paris‘]38 39     See also40     --------41     sklearn.preprocessing.OneHotEncoder : encode categorical integer features42         using a one-hot aka one-of-K scheme.43     """

 

利用sklearn的LabelEncoder對標籤進行數字化編碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.