簡體   English   中英

切片 3D arrays numpy

[英]Slicing 3D arrays numpy

考慮以下:

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))

我想根據下面的循環在特定位置替換 af 數組:

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

有了這個,我想知道是否有任何方法可以在沒有循環的情況下進行這種替換。

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 
     ...:                                                                                            

pos reshape 有什么不同嗎?

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

不,所以讓我們忘記它。

正如我評論的那樣x[np.arange(n), idx]是分配值的常用方法,例如循環。 索引 arrays 需要相互廣播以定義所需的元素。

如果我們嘗試:

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,) 

因此,讓我們制作第一個索引 (7,1) 形狀:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM