gpt4 book ai didi

python - 通过列向量索引矩阵

转载 作者:太空宇宙 更新时间:2023-11-03 16:08:16 24 4
gpt4 key购买 nike

我有一个大小为 m x n 的矩阵 M,以及 m x 1 的列向量。对于 m 行中的每一行,我需要选取与列向量中的值减 1 相对应的索引。因此,给出答案 m x 1。我该怎么做?

zb=a1.a3[np.arange(a1.z3.shape[0]),a1.train_labels-1]

zb.shape
Out[72]: (4000, 4000)

a1.z3.shape
Out[73]: (4000, 26)

a1.train_labels.shape
Out[74]: (4000, 1)

a1.train_labels.head()
Out[75]:
22
1618 25
2330 1
1651 17
133 17
2360 5


#my column vector a1.train_labels is shuffled. I don't want to unshuffle it.

最佳答案

如果你的二维数组是M,索引是一维数组v,那么你可以使用

M[np.arange(len(v)), v - 1]

例如:

In [14]: M = np.array([[1, 2], [3, 4]])

In [15]: v = np.array([2, 1])

In [16]: M[np.arange(len(v)), v - 1]
Out[16]: array([2, 3])

关于python - 通过列向量索引矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39548914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com