numpy花式索引與ix_()
花式索引(Fancy indexing)是Numpy的一個術語,指的是利用整數陣列進行索引。(不僅是1維,也可以是多維)
用法與例子如下:
建立 arr 陣列
>>> arr1 = np.empty((8,4))# 建立一個8行4列的二維陣列
>>> for i in range(8):# 每一行賦值為0~7
arr1[i] = i
>>> arr1
array([[ 0., 0., 0., 0.],
[ 1., 1., 1., 1.],
[ 2., 2., 2., 2.],
[ 3., 3., 3., 3.],
[ 5., 5., 5., 5.],
[ 6., 6., 6., 6.],
[ 7., 7., 7., 7.]])
1、用1維陣列進行索引
>>> arr1[[4,3,0,6]]
# 選取第4行、第3行、第0行、第6行
array([[ 4., 4., 4., 4.],
[ 3., 3., 3., 3.],
[ 0., 0., 0., 0.],
[ 6., 6., 6., 6.]])
2、用有負數的1維陣列進行索引,就是從末尾開始選取行
>>> arr1[[-3,-5,-7]]
# 選取倒數第3行,倒數第5行,倒數第7行
array([[ 5., 5., 5., 5.],
[ 3., 3., 3., 3.],
[ 1., 1., 1., 1.]])
在這裡必須注意!
順序選取是從0開始數的,a[0]代表第一個;而逆序選取是從1開始數的,a[-1]是倒數第一個
新建一個數組 arr2
>>> arr2 = np.arange(32).reshape((8,4))
>>> arr2
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
[28, 29, 30, 31]])
3、按座標選取每一個數
>>> arr2[[1,5,7,2],[0,3,1,2]]
# 意思就是,取座標所對應的數(1,0)——4,(5,3)——23,(7,1)——29,(2,2)——10,然後返回一個數組
array([ 4, 23, 29, 10])
4、希望先按我們要求選取行,再按順序將列排序,獲得一個矩形
>>> arr2[[1,5,7,2]][:,[0,3,1,2]]
array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]])
先按先選取第1、5、2、7行,每一行再按第0個、第3個、第1個、第2個排序
5、np.ix_函式,能把兩個一維陣列 轉換為 一個用於選取方形區域的索引器
實際意思就是,直接往np.ix_()裡扔進兩個一維陣列[1,5,7,2],[0,3,1,2],就能先按我們要求選取行,再按順序將列排序,跟上面得到的結果一樣,而不用寫“[ : , [0,3,1,2] ]”
原理:np.ix_函式就是輸入兩個陣列,產生笛卡爾積的對映關係
>>> arr2[np.ix_([1,5,7,2],[0,3,1,2])]
array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]])
例如就這個例子,np.ix_函式,將陣列[1,5,7,2]和陣列[0,3,1,2]產生笛卡爾積,就是得到(1,0),(1,3),(1,1),(1,2);(5,0),(5,3),(5,1),(5,2);(7,0),(7,3),(7,1),(7,2);(2,0),(2,3),(2,1),(2,2);
就是按照座標(1,0),(1,3),(1,1),(1,2)取得 arr2 所對應的元素4,7,5,6
(5,0),(5,3),(5,1),(5,2)取得 arr2 所對應的元素20,23,21,22
如此類推。
原文:https://blog.csdn.net/weixin_40001181/article/details/79775792
下面是官方解釋:
numpy.ix_
numpy.
ix_
(*args)[source]-
Construct an open mesh from multiple sequences.
This function takes N 1-D sequences and returns N outputs with N dimensions each, such that the shape is 1 in all but one dimension and the dimension with the non-unit shape value cycles through all N dimensions.
Using
ix_
one can quickly construct index arrays that will index the cross product.a[np.ix_([1,3],[2,5])]
returns the array[[a[1,2]a[1,5]],[a[3,2]a[3,5]]]
.- Parameters
- args1-D sequences
-
Each sequence should be of integer or boolean type. Boolean sequences will be interpreted as boolean masks for the corresponding dimension (equivalent to passing in
np.nonzero(boolean_sequence)
).
- Returns
- outtuple of ndarrays
-
N arrays with N dimensions each, with N the number of input sequences. Together these arrays form an open mesh.
See also
Examples
>>> a = np.arange(10).reshape(2, 5) >>> a array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) >>> ixgrid = np.ix_([0, 1], [2, 4]) >>> ixgrid (array([[0], [1]]), array([[2, 4]])) >>> ixgrid[0].shape, ixgrid[1].shape ((2, 1), (1, 2)) >>> a[ixgrid] array([[2, 4], [7, 9]])
>>> ixgrid = np.ix_([True, True], [2, 4]) >>> a[ixgrid] array([[2, 4], [7, 9]]) >>> ixgrid = np.ix_([True, True], [False, False, True, False, True]) >>> a[ixgrid] array([[2, 4], [7, 9]])