一键矢量转换如何工作?
当我在机器学习项目中工作时,我正在寻找一行代码来将我的标签变成一个热门的矢量.我在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'.