我不認爲大數據集會影響您的結果。 LabelEncoder
的目的是改變預測目標(在你的情況下,我假設,Target
列)。從User Guide
:
LabelEncoder是一個實用工具類,以幫助恢復正常的標籤,使得它們包含0和n_classes-1之間僅值。
這裏有一個例子,注意到我改變的Target
值在你的榜樣CountryDF
,只是爲了演示的目的:
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
。我很難理解爲什麼你會看到這種行爲,它適用於我,熊貓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])
好的,所以我理解你的觀點。 LabelEncoder僅用於轉換預測目標。我們不能使用相同的功能來轉換功能嗎?我不知道爲什麼我的轉換創建了這個[40749 667723 667725 ...,43347 43346 43345] –
而我並不試圖預測目標列,我試圖預測「Class_Category」列。我沒有使用OneHotEncoder,主要是因爲我的功能每個都有超過190個唯一值,通過執行OneHotEncoder,這會增加很多列的數量。 –
看到編輯,我很好奇,如果最後兩行出來,如我所料,沒有數據集是很難。 – Kevin