花式索引(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.],
[ 4., 4., 4., 4.],
[ 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],
[ 8, 9, 10, 11],
[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
-
- args 1-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
-
- out tuple of ndarrays
-
N arrays with N dimensions each, with N the number of input sequences. Together these arrays form an open mesh.
See also
ogrid
,mgrid
,meshgrid
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]])