gbh*_*rea 1 scikit-learn one-hot-encoding
为了离散分类功能我正在使用LabelEncoder和OneHotEncoder.我知道LabelEncoder按字母顺序映射数据,但OneHotEncoder如何映射数据?
我有一个pandas数据框,dataFeat有5个不同的列和4个可能的标签,如上所述.
dataFeat = data[['Feat1', 'Feat2', 'Feat3', 'Feat4', 'Feat5']]
Feat1 Feat2 Feat3 Feat4 Feat5
A B A A A
B B C C C
D D A A B
C C A A A
Run Code Online (Sandbox Code Playgroud)
我申请labelencoder这样的,
le = preprocessing.LabelEncoder()
intIndexed = dataFeat.apply(le.fit_transform)
Run Code Online (Sandbox Code Playgroud)
这就是LabelEncoder对标签进行编码的方式
Label LabelEncoded
A 0
B 1
C 2
D 3
Run Code Online (Sandbox Code Playgroud)
然后我应用这样的OneHotEncoder
enc = OneHotEncoder(sparse = False)
encModel = enc.fit(intIndexed)
dataFeatY = encModel.transform(intIndexed)
Run Code Online (Sandbox Code Playgroud)
intIndexed.shape = 94,5和dataFeatY.shape=94,20.
我的形状有点困惑dataFeatY- 不应该也是95,5?
按照下面的MhFarahani回答,我这样做是为了看标签是如何映射的
import numpy as np
S = np.array(['A', 'B','C','D'])
le = LabelEncoder()
S = le.fit_transform(S)
print(S)
[0 1 2 3]
ohe = OneHotEncoder()
one_hot = ohe.fit_transform(S.reshape(-1,1)).toarray()
print(one_hot.T)
[[ 1. 0. 0. 0.]
[ 0. 1. 0. 0.]
[ 0. 0. 1. 0.]
[ 0. 0. 0. 1.]]
Run Code Online (Sandbox Code Playgroud)
这是否意味着标签是这样映射的,还是每列都不同?(这可以解释形状是94,20)
Label LabelEncoded OneHotEncoded
A 0 1. 0. 0. 0
B 1 0. 1. 0. 0.
C 2 0. 0. 1. 0.
D 3 0. 0. 0. 1.
Run Code Online (Sandbox Code Playgroud)
一个热编码意味着您创建一个和零的向量.所以顺序无关紧要.在sklearn,首先你需要编码分类数据的数字数据,然后将它们喂到OneHotEncoder,例如:
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
S = np.array(['b','a','c'])
le = LabelEncoder()
S = le.fit_transform(S)
print(S)
ohe = OneHotEncoder()
one_hot = ohe.fit_transform(S.reshape(-1,1)).toarray()
print(one_hot)
Run Code Online (Sandbox Code Playgroud)
这导致:
[1 0 2]
[[ 0. 1. 0.]
[ 1. 0. 0.]
[ 0. 0. 1.]]
Run Code Online (Sandbox Code Playgroud)
但是pandas直接转换分类数据:
import pandas as pd
S = pd.Series( {'A': ['b', 'a', 'c']})
print(S)
one_hot = pd.get_dummies(S['A'])
print(one_hot)
Run Code Online (Sandbox Code Playgroud)
哪个输出:
A [b, a, c]
dtype: object
a b c
0 0 1 0
1 1 0 0
2 0 0 1
Run Code Online (Sandbox Code Playgroud)
正如您在映射期间所看到的,对于每个分类要素,都会创建一个向量.向量的元素在分类特征的位置处是一个元素,在其他位置处是零.以下是系列中只有两个分类功能的示例:
S = pd.Series( {'A': ['a', 'a', 'c']})
print(S)
one_hot = pd.get_dummies(S['A'])
print(one_hot)
Run Code Online (Sandbox Code Playgroud)
结果是:
A [a, a, c]
dtype: object
a c
0 1 0
1 1 0
2 0 1
Run Code Online (Sandbox Code Playgroud)
编辑回答新问题
让我们从这个问题开始:为什么我们执行一个热门编码?如果您将['a','b','c']等分类数据编码为整数[1,2,3](例如使用LableEncoder),除了对分类数据进行编码外,您还可以给它们一些权重1 <2 <3.这种编码方式适用于某些机器学习技术,如RandomForest.但是许多机器学习技术会假设在这种情况下'a'<'b'<'c'如果你分别用1,2,3编码它们.为避免此问题,您可以为数据中的每个唯一分类变量创建一列.换句话说,您为每个分类变量创建一个新功能(这里一个列为'a'一个用于'b',一个用于'c').如果变量位于该索引中,则这些新列中的值设置为1,而其他位置则设置为零.
对于示例中的数组,一个热编码器将是:
features -> A B C D
[[ 1. 0. 0. 0.]
[ 0. 1. 0. 0.]
[ 0. 0. 1. 0.]
[ 0. 0. 0. 1.]]
Run Code Online (Sandbox Code Playgroud)
您有4个分类变量"A","B","C","D".因此,OneHotEncoder会将您的(4,)数组填充到(4,4),以便为每个分类变量(这将是您的新功能)提供一个向量(或列).由于"A"是数组的0元素,因此第一列的索引0设置为1,其余的设置为0.类似地,第二个矢量(列)属于特征"B",因为"B"是在数组的索引1中,"B"向量的索引1设置为1,其余设置为零.这同样适用于其他功能.
让我改变你的阵列.也许它可以帮助您更好地理解标签编码器的工作原理:
S = np.array(['D', 'B','C','A'])
S = le.fit_transform(S)
enc = OneHotEncoder()
encModel = enc.fit_transform(S.reshape(-1,1)).toarray()
print(encModel)
Run Code Online (Sandbox Code Playgroud)
现在结果如下.这里第一列是'A',因为它是数组的最后一个元素(index = 3),所以第一列的最后一个元素是1.
features -> A B C D
[[ 0. 0. 0. 1.]
[ 0. 1. 0. 0.]
[ 0. 0. 1. 0.]
[ 1. 0. 0. 0.]]
Run Code Online (Sandbox Code Playgroud)
关于你的熊猫数据框,dataFeat即使在第一步中你是如何LableEncoder工作的,你也是错的.当你应用LableEncoder它时,它适合每列,并编码; 然后,它转到下一列并重新拟合该列.这是你应该得到的:
from sklearn.preprocessing import LabelEncoder
df = pd.DataFrame({'Feat1': ['A','B','D','C'],'Feat2':['B','B','D','C'],'Feat3':['A','C','A','A'],
'Feat4':['A','C','A','A'],'Feat5':['A','C','B','A']})
print('my data frame:')
print(df)
le = LabelEncoder()
intIndexed = df.apply(le.fit_transform)
print('Encoded data frame')
print(intIndexed)
Run Code Online (Sandbox Code Playgroud)
结果:
my data frame:
Feat1 Feat2 Feat3 Feat4 Feat5
0 A B A A A
1 B B C C C
2 D D A A B
3 C C A A A
Encoded data frame
Feat1 Feat2 Feat3 Feat4 Feat5
0 0 0 0 0 0
1 1 0 1 1 2
2 3 2 0 0 1
3 2 1 0 0 0
Run Code Online (Sandbox Code Playgroud)
请注意,在第一列中,Feat1"A"被编码为0,但在第二列中Feat2,"B"元素为0.这种情况发生,因为LableEncoder适合每列并单独转换它.请注意,在('B','C','D')的第二列中,变量'B'在字母顺序上更优越.
最后,这是您正在寻找的sklearn:
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder()
label_encoder = LabelEncoder()
data_lable_encoded = df.apply(label_encoder.fit_transform).as_matrix()
data_feature_onehot = encoder.fit_transform(data_lable_encoded).toarray()
print(data_feature_onehot)
Run Code Online (Sandbox Code Playgroud)
这给你:
[[ 1. 0. 0. 0. 1. 0. 0. 1. 0. 1. 0. 1. 0. 0.]
[ 0. 1. 0. 0. 1. 0. 0. 0. 1. 0. 1. 0. 0. 1.]
[ 0. 0. 0. 1. 0. 0. 1. 1. 0. 1. 0. 0. 1. 0.]
[ 0. 0. 1. 0. 0. 1. 0. 1. 0. 1. 0. 1. 0. 0.]]
Run Code Online (Sandbox Code Playgroud)
如果你使用pandas,你可以比较结果,希望给你一个更好的直觉:
encoded = pd.get_dummies(df)
print(encoded)
Run Code Online (Sandbox Code Playgroud)
结果:
Feat1_A Feat1_B Feat1_C Feat1_D Feat2_B Feat2_C Feat2_D Feat3_A \
0 1 0 0 0 1 0 0 1
1 0 1 0 0 1 0 0 0
2 0 0 0 1 0 0 1 1
3 0 0 1 0 0 1 0 1
Feat3_C Feat4_A Feat4_C Feat5_A Feat5_B Feat5_C
0 0 1 0 1 0 0
1 1 0 1 0 0 1
2 0 1 0 0 1 0
3 0 1 0 1 0 0
Run Code Online (Sandbox Code Playgroud)
这是完全一样的!
| 归档时间: |
|
| 查看次数: |
2756 次 |
| 最近记录: |