它类似于围绕 SO 的一些问题,但我不太明白获得我想要的东西的技巧。
我有两个数组,
arr形状 (x, y, z)形状
索引(x, y) 保存 z 感兴趣的索引。
对于索引的每个值,我想在arr中获取实际值:
arr.x == indexes.x
arr.y == indexes.y
arr.z == indexes[x,y]
这将给出一个与索引形状相似的 shape(x,y) 数组。
例如:
arr = np.arange(99)
arr = arr.reshape(3,3,11)
indexes = np.asarray([
[0,2,2],
[1,2,3],
[3,2,10]])
# indexes.shape == (3,3)
# Example for the first element to be computed
first_element = arr[0,0,indexes[0,0]]
有了上述indexes
,预期的数组将如下所示:
expected_result = np.asarray([
[0,13,24],
[34,46,58],
[69,79,98]])
我试过elements = np.take(arr, indexes, axis=z)
了,但它给出了一个形状数组 (x, y, x, y)
我也尝试过类似elements = arr[indexes, indexes,:]
但我没有得到我想要的东西。
我看到了一些涉及转置索引并将其转换为元组的答案,但我不明白它会有什么帮助。
注意:我对 numpy 有点陌生,所以我还不完全了解索引。
你将如何解决这种 numpy 风格?