【TensorFlow】onehot编码

def onehot(labels):
  '''one-hot 编码'''
  #数据有几行输出
  n_sample = len(labels)
  #数据分为几类。因为编码从0开始所以要加1
  n_class = max(labels) + 1
  #建立一个batch所需要的数组,全部赋0.
  onehot_labels = np.zeros((n_sample, n_class))
  #对每一行的,对应分类赋1
  onehot_labels[np.arange(n_sample), labels] = 1
  return onehot_labels

onehot_labels[np.arange(n_sample), labels] = 1   #对应行和列

运行结果:

label=np.array([0,1,2])
onehot(label)

Out[8]: 

array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
label=np.array([0,4,7,1,1,1,4,1])
onehot(label)

Out[10]:

array([[ 1., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 1., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 1.],
[ 0., 1., 0., 0., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 1., 0., 0., 0.],
[ 0., 1., 0., 0., 0., 0., 0., 0.]])

 为什么使用one-hot编码来处理离散型特征,具体原因,分下面几点来阐述: 
1、Why do we binarize categorical features?
We binarize the categorical input so that they can be thought of as a vector from the Euclidean space (we call this as embedding the vector in the Euclidean space).使用one-hot编码,将离散特征的取值扩展到了欧式空间,离散特征的某个取值就对应欧式空间的某个点。
 
2、Why do we embed the feature vectors in the Euclidean space?
Because many algorithms for classification/regression/clustering etc. requires computing distances between features or similarities between features. And many definitions of distances and similarities are defined over features in Euclidean space. So, we would like our features to lie in the Euclidean space as well.将离散特征通过one-hot编码映射到欧式空间,是因为,在回归,分类,聚类等机器学习算法中,特征之间距离的计算或相似度的计算是非常重要的,而我们常用的距离或相似度的计算都是在欧式空间的相似度计算,计算余弦相似性,基于的就是欧式空间

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值