[英]Index a 2D Numpy array with 2 lists of indices
我有一個奇怪的情況。
我有一個 2D Numpy 數組,x:
x = np.random.random_integers(0,5,(20,8))
我有 2 個索引器——一個帶有行索引,一個帶有列索引。 為了索引 X,我必須執行以下操作:
row_indices = [4,2,18,16,7,19,4]
col_indices = [1,2]
x_rows = x[row_indices,:]
x_indexed = x_rows[:,column_indices]
而不僅僅是:
x_new = x[row_indices,column_indices]
(失敗:錯誤,不能用(2,)廣播(20,))
我希望能夠使用廣播在一行中進行索引,因為這將使代碼保持干凈和可讀......此外,我對底層的 python 了解不多,但據我所知它,在一行中完成它應該更快(我將使用相當大的數組)。
測試用例:
x = np.random.random_integers(0,5,(20,8))
row_indices = [4,2,18,16,7,19,4]
col_indices = [1,2]
x_rows = x[row_indices,:]
x_indexed = x_rows[:,col_indices]
x_doesnt_work = x[row_indices,col_indices]
np.ix_
進行選擇或分配indexing-arrays
一個選擇
我們可以使用np.ix_
來獲取索引數組的元組,這些數組可以相互廣播,以產生更高維的索引組合。 因此,當該元組用於對輸入數組進行索引時,將為我們提供相同的高維數組。 因此,要根據兩個1D
引數組進行選擇,它將是 -
x_indexed = x[np.ix_(row_indices,col_indices)]
B. 分配
我們可以使用相同的符號將標量或可廣播數組分配到這些索引位置。 因此,以下適用於作業 -
x[np.ix_(row_indices,col_indices)] = # scalar or broadcastable array
masks
我們還可以將布爾數組/掩碼與np.ix_
一起np.ix_
,類似於索引數組的使用方式。 這可以再次用於從輸入數組中選擇一個塊,也可以用於分配給它。
一個選擇
因此,使用row_mask
和col_mask
布爾數組分別作為行和列選擇的掩碼,我們可以使用以下選擇 -
x[np.ix_(row_mask,col_mask)]
B. 分配
以下適用於作業 -
x[np.ix_(row_mask,col_mask)] = # scalar or broadcastable array
1. 使用np.ix_
和indexing-arrays
輸入數組和索引數組 -
In [221]: x
Out[221]:
array([[17, 39, 88, 14, 73, 58, 17, 78],
[88, 92, 46, 67, 44, 81, 17, 67],
[31, 70, 47, 90, 52, 15, 24, 22],
[19, 59, 98, 19, 52, 95, 88, 65],
[85, 76, 56, 72, 43, 79, 53, 37],
[74, 46, 95, 27, 81, 97, 93, 69],
[49, 46, 12, 83, 15, 63, 20, 79]])
In [222]: row_indices
Out[222]: [4, 2, 5, 4, 1]
In [223]: col_indices
Out[223]: [1, 2]
帶有np.ix_
的索引數組元組 -
In [224]: np.ix_(row_indices,col_indices) # Broadcasting of indices
Out[224]:
(array([[4],
[2],
[5],
[4],
[1]]), array([[1, 2]]))
做出選擇——
In [225]: x[np.ix_(row_indices,col_indices)]
Out[225]:
array([[76, 56],
[70, 47],
[46, 95],
[76, 56],
[92, 46]])
正如OP所建議的那樣,這實際上與使用row_indices
的二維數組版本執行老式廣播相同,該版本的元素/索引發送到axis=0
,從而在axis=1
處創建單例維度,從而允許使用col_indices
進行廣播. 因此,我們會有一個像這樣的替代解決方案 -
In [227]: x[np.asarray(row_indices)[:,None],col_indices]
Out[227]:
array([[76, 56],
[70, 47],
[46, 95],
[76, 56],
[92, 46]])
如前所述,對於分配,我們只是這樣做。
行、列索引數組 -
In [36]: row_indices = [1, 4]
In [37]: col_indices = [1, 3]
使用標量進行分配 -
In [38]: x[np.ix_(row_indices,col_indices)] = -1
In [39]: x
Out[39]:
array([[17, 39, 88, 14, 73, 58, 17, 78],
[88, -1, 46, -1, 44, 81, 17, 67],
[31, 70, 47, 90, 52, 15, 24, 22],
[19, 59, 98, 19, 52, 95, 88, 65],
[85, -1, 56, -1, 43, 79, 53, 37],
[74, 46, 95, 27, 81, 97, 93, 69],
[49, 46, 12, 83, 15, 63, 20, 79]])
使用 2D 塊(可廣播陣列)進行分配 -
In [40]: rand_arr = -np.arange(4).reshape(2,2)
In [41]: x[np.ix_(row_indices,col_indices)] = rand_arr
In [42]: x
Out[42]:
array([[17, 39, 88, 14, 73, 58, 17, 78],
[88, 0, 46, -1, 44, 81, 17, 67],
[31, 70, 47, 90, 52, 15, 24, 22],
[19, 59, 98, 19, 52, 95, 88, 65],
[85, -2, 56, -3, 43, 79, 53, 37],
[74, 46, 95, 27, 81, 97, 93, 69],
[49, 46, 12, 83, 15, 63, 20, 79]])
2. 使用帶有masks
np.ix_
輸入數組 -
In [19]: x
Out[19]:
array([[17, 39, 88, 14, 73, 58, 17, 78],
[88, 92, 46, 67, 44, 81, 17, 67],
[31, 70, 47, 90, 52, 15, 24, 22],
[19, 59, 98, 19, 52, 95, 88, 65],
[85, 76, 56, 72, 43, 79, 53, 37],
[74, 46, 95, 27, 81, 97, 93, 69],
[49, 46, 12, 83, 15, 63, 20, 79]])
輸入行,列掩碼 -
In [20]: row_mask = np.array([0,1,1,0,0,1,0],dtype=bool)
In [21]: col_mask = np.array([1,0,1,0,1,1,0,0],dtype=bool)
做出選擇——
In [22]: x[np.ix_(row_mask,col_mask)]
Out[22]:
array([[88, 46, 44, 81],
[31, 47, 52, 15],
[74, 95, 81, 97]])
使用標量進行分配 -
In [23]: x[np.ix_(row_mask,col_mask)] = -1
In [24]: x
Out[24]:
array([[17, 39, 88, 14, 73, 58, 17, 78],
[-1, 92, -1, 67, -1, -1, 17, 67],
[-1, 70, -1, 90, -1, -1, 24, 22],
[19, 59, 98, 19, 52, 95, 88, 65],
[85, 76, 56, 72, 43, 79, 53, 37],
[-1, 46, -1, 27, -1, -1, 93, 69],
[49, 46, 12, 83, 15, 63, 20, 79]])
使用 2D 塊(可廣播陣列)進行分配 -
In [25]: rand_arr = -np.arange(12).reshape(3,4)
In [26]: x[np.ix_(row_mask,col_mask)] = rand_arr
In [27]: x
Out[27]:
array([[ 17, 39, 88, 14, 73, 58, 17, 78],
[ 0, 92, -1, 67, -2, -3, 17, 67],
[ -4, 70, -5, 90, -6, -7, 24, 22],
[ 19, 59, 98, 19, 52, 95, 88, 65],
[ 85, 76, 56, 72, 43, 79, 53, 37],
[ -8, 46, -9, 27, -10, -11, 93, 69],
[ 49, 46, 12, 83, 15, 63, 20, 79]])
關於什么:
x[row_indices][:,col_indices]
例如,
x = np.random.random_integers(0,5,(5,5))
## array([[4, 3, 2, 5, 0],
## [0, 3, 1, 4, 2],
## [4, 2, 0, 0, 3],
## [4, 5, 5, 5, 0],
## [1, 1, 5, 0, 2]])
row_indices = [4,2]
col_indices = [1,2]
x[row_indices][:,col_indices]
## array([[1, 5],
## [2, 0]])
import numpy as np
x = np.random.random_integers(0,5,(4,4))
x
array([[5, 3, 3, 2],
[4, 3, 0, 0],
[1, 4, 5, 3],
[0, 4, 3, 4]])
# This indexes the elements 1,1 and 2,2 and 3,3
indexes = (np.array([1,2,3]),np.array([1,2,3]))
x[indexes]
# returns array([3, 5, 4])
請注意,根據您使用的索引類型,numpy 有非常不同的規則。 所以索引幾個元素應該由np.ndarray
tuple
(參見 索引手冊)。
所以你只需要將你的list
轉換為np.ndarray
並且它應該可以按預期工作。
我認為您正在嘗試執行以下(等效)操作之一:
x_does_work = x[row_indices,:][:,col_indices]
x_does_work = x[:,col_indices][row_indices,:]
這實際上將創建一個僅包含選定行的x
子集,然后從中選擇列,或者在第二種情況下反之亦然。 第一種情況可以認為是
x_does_work = (x[row_indices,:])[:,col_indices]
如果你用 np.newaxis 編寫它,你的第一次嘗試會奏效
x_new = x[row_indices[:, np.newaxis],column_indices]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.