使用sklearn之LabelEncoder将Label标准化

来源:互联网 发布:日历制作软件 编辑:程序博客网 时间:2024/06/05 07:29

LabelEncoder可以将标签分配一个0—n_classes-1之间的编码
将各种标签分配一个可数的连续编号:

>>> 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]) # Transform Categories Into Integersarray([0, 0, 1, 2], dtype=int64)>>> le.inverse_transform([0, 0, 1, 2]) # Transform Integers Into Categoriesarray([1, 1, 2, 6])
>>> le = preprocessing.LabelEncoder()>>> le.fit(["paris", "paris", "tokyo", "amsterdam"])LabelEncoder()>>> list(le.classes_)['amsterdam', 'paris', 'tokyo']>>> le.transform(["tokyo", "tokyo", "paris"]) # Transform Categories Into Integersarray([2, 2, 1], dtype=int64)>>> list(le.inverse_transform([2, 2, 1])) #Transform Integers Into Categories['tokyo', 'tokyo', 'paris']

将DataFrame中的所有ID标签转换成连续编号:

from sklearn.preprocessing import LabelEncoderimport numpy as npimport pandas as pddf=pd.read_csv('testdata.csv',sep='|',header=None)
    0   1   2   3   4   50   37  52  55  50  38  541   17  32  20  9   6   482   28  10  56  51  45  163   27  49  41  30  53  194   44  29  8   1   46  135   11  26  21  14  7   336   0   39  22  33  35  437   18  15  47  5   25  348   23  2   4   9   3   319   12  57  36  40  42  24
le = LabelEncoder()le.fit(np.unique(df.values))df.apply(le.transform)
    0   1   2   3   4   50   37  52  55  50  38  541   17  32  20  9   6   482   28  10  56  51  45  163   27  49  41  30  53  194   44  29  8   1   46  135   11  26  21  14  7   336   0   39  22  33  35  437   18  15  47  5   25  348   23  2   4   9   3   319   12  57  36  40  42  24

将DataFrame中的每一行ID标签分别转换成连续编号:

import pandas as pdfrom sklearn.preprocessing import LabelEncoderfrom sklearn.pipeline import Pipelineclass MultiColumnLabelEncoder:    def __init__(self,columns = None):        self.columns = columns # array of column names to encode    def fit(self,X,y=None):        return self # not relevant here    def transform(self,X):        '''        Transforms columns of X specified in self.columns using        LabelEncoder(). If no columns specified, transforms all        columns in X.        '''        output = X.copy()        if self.columns is not None:            for col in self.columns:                output[col] = LabelEncoder().fit_transform(output[col])        else:            for colname,col in output.iteritems():                output[colname] = LabelEncoder().fit_transform(col)        return output    def fit_transform(self,X,y=None):        return self.fit(X,y).transform(X)
MultiColumnLabelEncoder(columns = [0, 1, 2, 3, 4, 5]).fit_transform(df)

或者

df.apply(LabelEncoder().fit_transform)
    0   1   2   3   4   50   8   8   8   7   5   91   3   5   2   2   1   82   7   1   9   8   7   13   6   7   6   4   9   24   9   4   1   0   8   05   1   3   3   3   2   56   0   6   4   5   4   77   4   2   7   1   3   68   5   0   0   2   0   49   2   9   5   6   6   3
# Create some toy data in a Pandas dataframefruit_data = pd.DataFrame({    'fruit':  ['apple','orange','pear','orange'],    'color':  ['red','orange','green','green'],    'weight': [5,6,3,4]})
    color   fruit   weight0   red     apple   51   orange  orange  62   green   pear    33   green   orange  4
MultiColumnLabelEncoder(columns = ['fruit','color']).fit_transform(fruit_data)

或者

fruit_data[['fruit','color']]=fruit_data[['fruit','color']].apply(LabelEncoder().fit_transform)
    color   fruit   weight0   2       0       51   1       1       62   0       2       33   0       1       4
原创粉丝点击