How do you gather a different class score for each sample?
take_along_axis gathers values using the indices associated with each slice. Different rows can select different columns.
numpy.take_along_axis in practice
Select column one in row zero and column zero in row one using a 2×1 index array. The label vocabulary must use the same column order as the score matrix.
import numpy as np
np.take_along_axis(np.array([[4,8],[7,3]]), np.array([[1],[0]]), axis=1)Result [[8], [7]]
中文
怎样读取每个样本自己选中的类别分数?
take_along_axis 沿指定轴按相应切片的索引取值,因此每行可以有自己的选择。
numpy.take_along_axis 算例
第一行取列 1、第二行取列 0,两个索引组成 2×1 的数组。类别顺序必须与分数矩阵列的顺序一致。
import numpy as np
np.take_along_axis(np.array([[4,8],[7,3]]), np.array([[1],[0]]), axis=1)结果 [[8], [7]]