Skip to main content
 首页 » 编程设计

python之多级标签编码器

2025年04月02日20www_RR

我正在使用 python 标签编码器来转换我的数据。这是我的示例数据。

                         Database      Target    Market_Description    Brand  \ 
0            CN_Milk powder_Incl_Others    NaN  Shanghai Hyper total  O.Brand    
1            CN_Milk powder_Incl_Others    NaN  Shanghai Hyper total  O.Brand    
2            CN_Milk powder_Incl_Others    NaN  Shanghai Hyper total  O.Brand    
 
  Sub_Brand Category                   Class_Category   
0       NaN      NaN  Hi Cal Adult Milk Powders- C1   
1       NaN      NaN  Hi Cal Adult Milk Powders- C1   
2       NaN      NaN  Hi Cal Adult Milk Powders- C1  

我正在对所有列应用转换

df3 = CountryDF.apply(preprocessing.LabelEncoder().fit_transform)    

当我检查 Target 列的唯一值时,它说,

>>> print pd.unique(CountryDF.Target.ravel()) 
 
>>> [nan 'Elder' 'Others' 'Lady'] 

但是当我在转换后检查相同内容时,我得到了多个级别。

>>> print pd.unique(df3.Target.ravel()) 
>>> [ 40749 667723 667725 ...,  43347  43346  43345] 

我不确定这是怎么回事?我期望有四个唯一值,因为我认为转换实现通过获取唯一值并为每个值分配排序的 numpy 来工作,任何人都可以帮助我理解这一点。

编辑:- 这个数据集是一个大数据集的子集。跟这个有关系吗?

EDIT2 :- @Kevin 我尝试了你的建议,这很奇怪。看到这个。

请您参考如下方法:

我认为大型数据集不会影响您的结果。 LabelEncoder的目的|是转换预测目标(在您的情况下,我假设是 Target 列)。来自User Guide :

LabelEncoder is a utility class to help normalize labels such that they contain only values between 0 and n_classes-1.

这是一个例子,注意我在你的例子 CountryDF 中更改了 Target 的值,只是为了演示目的:

from sklearn.preprocessing import LabelEncoder 
import numpy as np 
import pandas as pd 
 
CountryDF = pd.DataFrame([['CN_Milk powder_Incl_Others',np.nan,'Shanghai Hyper total','O.Brand',np.nan,np.nan,'Hi Cal Adult Milk Powders- C1'], 
                              ['CN_Milk powder_Incl_Others','Elder','Shanghai Hyper total','O.Brand',np.nan,np.nan,'Hi Cal Adult Milk Powders- C1'], 
                              ['CN_Milk powder_Incl_Others','Others','Shanghai Hyper total','O.Brand',np.nan,np.nan,'Hi Cal Adult Milk Powders- C1'], 
                              ['CN_Milk powder_Incl_Others','Lady','Shanghai Hyper total','O.Brand',np.nan,np.nan,'Hi Cal Adult Milk Powders- C1'], 
                             ['CN_Milk powder_Incl_Others',np.nan,'Shanghai Hyper total','O.Brand','S_B1',np.nan,'Hi Cal Adult Milk Powders- C1'], 
                             ['CN_Milk powder_Incl_Others',np.nan,'Shanghai Hyper total','O.Brand','S_B2',np.nan,'Hi Cal Adult Milk Powders- C1']], 
                            columns=['Database','Target','Market_Description','Brand','Sub_Brand', 'Category','Class_Category']) 

首先,初始化 LabelEncoder,然后拟合和转换数据(同时将转换后的数据分配给新列)。

le = LabelEncoder() # initialze the LabelEncoder once 
 
#Create a new column with transformed values. 
CountryDF['EncodedTarget'] = le.fit_transform(CountryDF['Target']) 

注意,最后一列 EncodedTarget 是 Target 的转换副本。

CountryDF 
 
Database    Target  Market_Description  Brand   Sub_Brand   Category    Class_Category  EncodedTarget 
0   CN_Milk powder_Incl_Others  NaN     Shanghai Hyper total    O.Brand     NaN     NaN     Hi Cal Adult Milk Powders- C1   0 
1   CN_Milk powder_Incl_Others  Elder   Shanghai Hyper total    O.Brand     NaN     NaN     Hi Cal Adult Milk Powders- C1   1 
2   CN_Milk powder_Incl_Others  Others  Shanghai Hyper total    O.Brand     NaN     NaN     Hi Cal Adult Milk Powders- C1   3 
3   CN_Milk powder_Incl_Others  Lady    Shanghai Hyper total    O.Brand     NaN     NaN     Hi Cal Adult Milk Powders- C1   2 

我希望这有助于清理 LabelEncoder。如果这不能完全回答您的问题,它可能会引导您走上正确的道路来转变您的功能(这可能是您正在尝试做的?)——查看 OneHotEncoder

编辑 我向 CountryDF 添加了另外两行(见上文),它在 Sub_Brand 列中有两个唯一值,它们遵循一系列连续的 NaN .我很困惑为什么你会看到这种行为,它对我有用,pandas 0.17.0 和 scikit 0.17。

df3 = CountryDF.apply(LabelEncoder().fit_transform) 
df3 
Database    Target  Market_Description  Brand   Sub_Brand   Category    Class_Category 
0   0   0   0   0   0   0   0 
1   0   1   0   0   0   1   0 
2   0   3   0   0   0   2   0 
3   0   2   0   0   0   3   0 
4   0   0   0   0   1   4   0 
5   0   0   0   0   2   5   0 

我无法重现您的问题,您是否有指向您的数据的链接?

pd.unique(CountryDF.Target.ravel())     
array([nan, 'Elder', 'Others', 'Lady'], dtype=object) 
pd.unique(df3.Target.ravel()) 
array([0, 1, 3, 2])