简体   繁体   English

用元组数组进行Numpy 2d索引,可能还进行Nd索引

[英]Numpy 2d and possibly N-d indexing by array of tuples

With 1d arrays it's possible to index it by Nd array of integers like this: 使用1d数组,可以通过Nd个整数数组进行索引,如下所示:

>>> rand = np.random.rand(9).astype(np.float32)
>>> rand
array([ 0.69786191,  0.09376735,  0.60141236,  0.35305005,  0.68340319,
        0.0746202 ,  0.11620298,  0.46607161,  0.90864712], dtype=float32)
>>> u = np.random.randint(0, 9, (2,2))
>>> u
array([[0, 6],
       [5, 6]])
>>> rand[u]
array([[ 0.69786191,  0.11620298],
       [ 0.0746202 ,  0.11620298]], dtype=float32)

But I can't do the same with 2d arrays: 但是我不能对2d数组做同样的事情:

>>> rand2d = np.random.rand(9).astype(np.float32).reshape(3,3)
>>> rand2d
array([[ 0.83248657,  0.75025952,  0.87252802],
       [ 0.78049046,  0.92902303,  0.42035589],
       [ 0.80461669,  0.49386421,  0.56518084]], dtype=float32)
>>> u = np.random.randint(0, 3, (2,2,2))
>>> u
array([[[2, 2],
        [2, 2]],

       [[0, 2],
        [0, 1]]])
>>> rand2d[u]
array([[[[ 0.80461669,  0.49386421,  0.56518084],
         [ 0.80461669,  0.49386421,  0.56518084]],

        [[ 0.80461669,  0.49386421,  0.56518084],
         [ 0.80461669,  0.49386421,  0.56518084]]],

       [[[ 0.83248657,  0.75025952,  0.87252802],
         [ 0.80461669,  0.49386421,  0.56518084]],

        [[ 0.83248657,  0.75025952,  0.87252802],
         [ 0.78049046,  0.92902303,  0.42035589]]]], dtype=float32)

While the result I expected is: 虽然我期望的结果是:

[[rand2d[2, 2], rand2d[2, 2]],
[rand2d[0, 2], rand2d[0, 1]]] ==
[[0.56518084, 0.56518084],
[0.87252802, 0.75025952]]

How can I achieve this without iterating? 我如何不进行迭代就实现这一目标?

Straight from the example in the docs : 直接来自docs中示例

>>> 
>>> x
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
>>> 
>>> rows = np.array([[0,0],[3,3]])
>>> columns = np.array([[0,2],[0,2]])
>>> x[rows,columns]
array([[ 0,  2],
       [ 9, 11]])
>>> 

You can see that it is selecting items at (0,0), (0,2) and (3,0),(3,2). 您可以看到它正在选择(0,0),(0,2)和(3,0),(3,2)的项目。

You can have a look at unravel_index . 您可以看一下unravel_index Not sure if this is exactly what you are after, but it might be useful: 不知道这是否正是您所追求的,但这可能很有用:

import numpy as np

rand2d = np.array([[ 0.83248657,  0.75025952,  0.87252802],
       [ 0.78049046,  0.92902303,  0.42035589],
       [ 0.80461669,  0.49386421,  0.56518084]], dtype=np.float32)


u = np.random.randint(0, 3, (2,2,2))

print(rand2d[np.unravel_index(u, rand2d.shape)])

Example output is: 示例输出为:

[[[ 0.87252802  0.75025952]
  [ 0.83248657  0.75025952]]

 [[ 0.87252802  0.87252802]
  [ 0.75025952  0.87252802]]]

Thanx wwii. 谢谢第二次世界大战。 My point is indexing an array with matrix of each element coordinates (as example - for per-pixel shift). 我的观点是用每个元素坐标的矩阵对数组进行索引(例如,针对每个像素的移动)。 For my case solution is: 就我而言,解决方案是:

>>> u
array([[[2, 2],
        [2, 2]],

       [[0, 2],
        [0, 1]]])
>>> ux = u.transpose(2,0,1)
>>> ux
array([[[2, 2],
        [0, 0]],

       [[2, 2],
        [2, 1]]])
>>> rand2d[ux[0], ux[1]]
array([[ 0.56518084,  0.56518084],
       [ 0.87252802,  0.75025952]], dtype=float32)

Also, here is my solution to get this array of coordinates, operate with it and use it back for indexing: 另外,这是我的解决方案,用于获取此坐标数组,对其进行操作并将其用于索引编制:

>>> ux = np.indices(rand2d.shape)
>>> ux
array([[[0, 0, 0],
        [1, 1, 1],
        [2, 2, 2]],

       [[0, 1, 2],
        [0, 1, 2],
        [0, 1, 2]]])
>>> u = ux.transpose(1,2,0)
>>> u
array([[[0, 0],
        [0, 1],
        [0, 2]],

       [[1, 0],
        [1, 1],
        [1, 2]],

       [[2, 0],
        [2, 1],
        [2, 2]]])
>>> u[1,1]-=1
>>> u
array([[[0, 0],
        [0, 1],
        [0, 2]],

       [[1, 0],
        [0, 0],
        [1, 2]],

       [[2, 0],
        [2, 1],
        [2, 2]]])
>>> ux = u.transpose(2,0,1) #Transpose back
>>> ux
array([[[0, 0, 0],
        [1, 0, 1],
        [2, 2, 2]],

       [[0, 1, 2],
        [0, 0, 2],
        [0, 1, 2]]])
>>> rand2d[ux[0], ux[1]]
array([[ 0.83248657,  0.75025952,  0.87252802],
       [ 0.78049046,  0.83248657,  0.42035589],
       [ 0.80461669,  0.49386421,  0.56518084]], dtype=float32)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM