简体   繁体   English

是否可以将 sklearn.preprocessing.LabelEncoder() 应用于 2D 列表?

[英]Is it possible to apply sklearn.preprocessing.LabelEncoder() on a 2D list?

Say I have a list as given below:假设我有一个如下列表:

l = [
       ['PER', 'O', 'O', 'GEO'],
       ['ORG', 'O', 'O', 'O'],
       ['O', 'O', 'O', 'GEO'],
       ['O', 'O', 'PER', 'O']
    ]

I want to encode the 2D list with LabelEncoder().我想用 LabelEncoder() 对二维列表进行编码。

It should look something like:它应该看起来像:

l = [
       [1, 0, 0, 2],
       [3, 0, 0, 0],
       [0, 0, 0, 2],
       [0, 0, 1, 0]
    ]

Is it possible?可能吗? If not, is there any workaround?如果没有,是否有任何解决方法?

Thanks in advance!提前致谢!

You can flatten the list, fit the encoder with all the potential values and then use the encoder to transform each sublist, as shown below:您可以展平列表,用所有潜在值拟合编码器,然后使用编码器转换每个子列表,如下所示:

from sklearn.preprocessing import LabelEncoder

l = [
       ['PER', 'O', 'O', 'GEO'],
       ['ORG', 'O', 'O', 'O'],
       ['O', 'O', 'O', 'GEO'],
       ['O', 'O', 'PER', 'O']
    ]

flattened_l = [e for sublist in l for e in sublist]

# flattened_l is ['PER', 'O', 'O', 'GEO', 'ORG', 'O', 'O', 'O', 'O', 'O', 'O', 'GEO', 'O', 'O', 'PER', 'O']

le = LabelEncoder().fit(flattened_l)

# See the mapping generated by the encoder:
list(enumerate(le.classes_))
# [(0, 'GEO'), (1, 'O'), (2, 'ORG'), (3, 'PER')]

# And, finally, transform each sublist:
res = [list(le.transform(sublist)) for sublist in l]
res

# Getting the result you want:
# [[3, 1, 1, 0], [2, 1, 1, 1], [1, 1, 1, 0], [1, 1, 3, 1]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据集上的sklearn.preprocessing.LabelEncoder TypeError - sklearn.preprocessing.LabelEncoder TypeError on data set 使用 sklearn.preprocessing.LabelEncoder() 使用 Python 编码多个分类数据在 2D 数组输入上需要太多处理时间 - Encoding Multiple Categorical Data with Python using sklearn.preprocessing.LabelEncoder() takes too much processing time on 2D array inputs 为什么 sklearn 预处理 LabelEncoder inverse_transform 仅适用于一列? - Why does sklearn preprocessing LabelEncoder inverse_transform apply from only one column? 如何将列表的Python列表转换为2D numpy数组以进行sklearn.preprocessing - How to convert a Python list of lists to a 2D numpy array for sklearn.preprocessing sklearn预处理培训清单 - Preprocessing training list with sklearn 将多个预处理步骤应用于 sklearn 管道中的列 - Apply multiple preprocessing steps to a column in sklearn pipeline 在Anaconda中更新软件包后,“从sklearn.preprocessing导入LabelEncoder,OneHotEncoder”失败 - “from sklearn.preprocessing import LabelEncoder, OneHotEncoder” fails after update of packages in Anaconda sklearn LabelEncoder中的标签不一致? - Inconsistent labeling in sklearn LabelEncoder? labelEncoder 在 sklearn 中的工作 - Working of labelEncoder in sklearn 如何在sklearn中一次在多个列上应用预处理方法 - how to apply preprocessing methods on several columns at one time in sklearn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM