一键矢量转换如何工作? [英] How does this one-hot vector conversion work?

查看:78
本文介绍了一键矢量转换如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在机器学习项目中工作时,我正在寻找一行代码来将我的标签变成一个热门的矢量.我在Reddit上遇到了来自u/benanne的漂亮代码行.

When I was working on my machine learning project, I was looking for a line of code to turn my labels into one-hot vectors. I came across this nifty line of code from u/benanne on Reddit.

np.eye(n_labels)[target_vector]

例如,对于target_vector = np.array([1, 4, 2, 1, 0, 1, 3, 2]),它返回一键编码的值:

For example, for a target_vector = np.array([1, 4, 2, 1, 0, 1, 3, 2]), it returns the one-hot coded values:

np.eye(5)[target_vector]
Out: 
array([[ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.],
       [ 0.,  0.,  1.,  0.,  0.],
       ..., 
       [ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  0.],
       [ 0.,  0.,  1.,  0.,  0.]])

虽然它确实可以工作,但是我不确定它如何或为什么起作用.

While it definitely does work, I'm not sure how or why it works.

推荐答案

这很简单. np.eye(n_labels)创建一个大小为n_labels的单位矩阵,然后使用target_vector从该矩阵中选择与当前目标的值相对应的行.由于单位矩阵中的每一行仅包含一个1元素,其余的0,因此每一行将是有效的一个热编码".

It's rather simple. np.eye(n_labels) creates an identity matrix of size n_labels then you use your target_vector to select rows, corresponding to the value of the current target, from that matrix. Since each row in an identity matrix contains exactly one 1 element and the rest 0, each row will be a valid 'one hot coding'.

这篇关于一键矢量转换如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆