关于one-hot编码的sklearn.preprocessing import OneHotEncoder的补充知识

今天学到one-hot编码

发现了一些以前one-hot编码的问题,现在补充一下:

关于one-hot编码的原理就不再说了,大家看其他的博文,
下面引入例子:
在机器学习算法中,我们经常会遇到分类特征,例如:人的性别有男女,祖国有中国,美国,法国等。
这些特征值并不是连续的,而是离散的,无序的。通常我们需要对其进行特征数字化。
那什么是特征数字化呢?例子如下:
性别特征:[“男”,“女”]
祖国特征:[“中国”,"美国,“法国”]
运动特征:[“足球”,“篮球”,“羽毛球”,“乒乓球”]
假如某个样本(某个人),他的特征是这样的[“男”,“中国”,“乒乓球”],我们可以用 [0,0,4] 来表示,但是这样的特征处理并不能直接放入机器学习算法中。因为类别之间是无序的(运动数据就是任意排序的)。

One-Hot实际案例

就拿上面的例子来说吧,性别特征:[“男”,“女”],按照N位状态寄存器来对N个状态进行编码的原理,咱们处理后应该是这样的(这里只有两个特征,所以N=2):
男 => 10
女 => 01
祖国特征:[“中国”,"美国,“法国”](这里N=3):
中国 => 100
美国 => 010
法国 => 001
运动特征:[“足球”,“篮球”,“羽毛球”,“乒乓球”](这里N=4):
足球 => 1000
篮球 => 0100
羽毛球 => 0010
乒乓球 => 0001
所以,当一个样本为[“男”,“中国”,“乒乓球”]的时候,完整的特征数字化的结果为:
[10,00,100,000,000,0000,0000,0000,0001]
简写为:
[1,0,1,0,0,0,0,0,1]
以上来源于原文:https://blog.csdn.net/wxystyle/article/details/80729741
之后举One-Hot在python中的使用例子

from sklearn import preprocessing  
   
enc = preprocessing.OneHotEncoder()  
enc.fit([[0,0,3],[1,1,0],[0,2,1],[1,0,2]])  #这里一共有4个数据,3种特征
   
array = enc.transform([[0,1,3]]).toarray()  #这里使用一个新的数据来测试
   
print array   # [[ 1  0  0  1  0  0  0  0  1]]

enc.n_values_ is: [2 3 4]
enc.feature_indices_ is: [0 2 5 9]
可是自己试验换一个数据fit一下出现的结果却不同了

import numpy as np
from sklearn.preprocessing import OneHotEncoder

enc = OneHotEncoder()
cc = [[0,0,3],[1,1,0],[0,2,1]]
enc.fit(cc)
print ("enc.n_values_ is:",enc.n_values_)
print ("enc.feature_indices_ is:",enc.feature_indices_)
enc.transform([[0,1,3]]).toarray()

结果为
enc.n_values_ is: [2 3 4]
enc.feature_indices_ is: [0 2 5 9]
array([[1., 0., 0., 1., 0., 0., 0., 1.]])
应用同样的编码,为什么例子中是9位数据 [[ 1 0 0 1 0 0 0 0 1]],而我的测试结果是8位数据[[1., 0., 0., 1., 0., 0., 0., 1.]],看了一下结果提示

c:\Users\ada\Anaconda3\lib\site-packages\sklearn\preprocessing\_encoders.py:371: FutureWarning: The handling of integer data will change in version 0.22. Currently, the categories are determined based on the range [0, max(values)], while in the future they will be determined based on the unique values.
If you want the future behaviour and silence this warning, you can specify "categories='auto'".
In case you used a LabelEncoder before this OneHotEncoder to convert the categories to integers, then you can now use the OneHotEncoder directly.
  warnings.warn(msg, FutureWarning)

说应用了新的分类形式,
所以
[[0,0,3],[1,1,0],[0,2,1]] --的特征编码为10 ,01 100,010,001, 100,010,001
(因为第三列为,3,0,1也是三个特征,但是按照4个特征来算range [0, max(values)],编码的时候却是三个)所以得到的结果是8位的数 [[1., 0., 0., 1., 0., 0., 0., 1.]]不是9位。
但是又会问,如果测试数据是
enc.transform([[0,1,3],[0,1,2]).toarray(),第三位没有2的表示,怎么办呢?系统中是用000表示没有的数据的
测试内容

import numpy as np
from sklearn.preprocessing import OneHotEncoder

enc = OneHotEncoder()
cc = [[0,0,3],[1,1,0],[0,2,1]]
#ccc = np.reshape(cc,[-1,1]) 
enc.fit(cc)
print ("enc.n_values_ is:",enc.n_values_)
print ("enc.feature_indices_ is:",enc.feature_indices_)
enc.transform([[0,1,3],[0,1,2]]).toarray()

结果

enc.n_values_ is: [2 3 4]
enc.feature_indices_ is: [0 2 5 9]
array([[1., 0., 0., 1., 0., 0., 0., 1.],
       [1., 0., 0., 1., 0., 0., 0., 0.]])

其中的2用000表示的。
上面的我对one-hot的补充,不知道对不对,请大家提出意见

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值