简体   繁体   中英

Slicing 3D arrays numpy

Consider the following:

In[1]: pos
Out[1]:
array([[0, 1, 2, 3],
       [2, 3, 4, 5],
       [0, 1, 6, 7],
       [2, 3, 6, 7],
       [2, 3, 8, 9],
       [4, 5, 8, 9],
       [6, 7, 8, 9]])

In[2]: pos = pos.reshape((7,1,4))
Out[2]:
array([[[0, 1, 2, 3]],
       [[2, 3, 4, 5]],
       [[0, 1, 6, 7]],
       [[2, 3, 6, 7]],
       [[2, 3, 8, 9]],
       [[4, 5, 8, 9]],
       [[6, 7, 8, 9]]])

In[3]: af = np.zeros((7,10,4))

I would like to replace in the af array at specific positions as per the loop below:

for i in range(7):
    af[i,pos[i],pos[0]] = 1

With that, I would like to know if there is any way to do this substitution without a loop.

In [391]: pos = np.array([[0, 1, 2, 3], 
     ...:        [2, 3, 4, 5], 
     ...:        [0, 1, 6, 7], 
     ...:        [2, 3, 6, 7], 
     ...:        [2, 3, 8, 9], 
     ...:        [4, 5, 8, 9], 
     ...:        [6, 7, 8, 9]])                                                                      
In [392]: af = np.zeros((7,10,4),int)                                                                
In [393]: for i in range(7): 
     ...:     af[i,pos[i],pos[0]] = 1 
     ...:                                                                                            

Does the pos reshape make any difference?

In [395]: pos1 = pos.reshape((7,1,4))                                                                
In [398]: af1 = np.zeros((7,10,4),int)                                                                                                                                                           
In [399]: for i in range(7): 
     ...:     af1[i,pos1[i],pos1[0]] = 1 
     ...:      
     ...:                                                                                            
In [400]: np.allclose(af,af1)                                                                        
Out[400]: True

No, so let's forget about it.

As I commented x[np.arange(n), idx] is a common way of assigning values like your loop. The index arrays need to broadcast with each other to define the desired elements.

If we try:

In [403]: af[np.arange(7),pos,pos[0]]                                                                
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-403-6488d02c6898> in <module>
----> 1 af[np.arange(7),pos,pos[0]]

IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (7,) (7,4) (4,) 

So lets make the first index (7,1) shape:

In [404]: af[np.arange(7)[:,None],pos,pos[0]]                                                        
Out[404]: 
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])
In [405]: af2 = np.zeros((7,10,4),int)                                                               
In [406]: af2[np.arange(7)[:,None], pos,pos[0]] = 1                                                  
In [407]: np.allclose(af,af2)                                                                        
Out[407]: True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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