Index a 2D Numpy array with 2 lists of indices
我有一个奇怪的情况。
我有一个2D Numpy数组,x:
1 | x = np.random.random_integers(0,5,(20,8)) |
我有2个索引器-一个索引为行,一个索引为列。 为了索引X,我必须执行以下操作:
1 2 3 4 | row_indices = [4,2,18,16,7,19,4] col_indices = [1,2] x_rows = x[row_indices,:] x_indexed = x_rows[:,column_indices] |
不仅仅是:
1 | x_new = x[row_indices,column_indices] |
(失败:错误,无法通过(2,)广播(20,))
我希望能够使用广播在一行中建立索引,因为那样可以使代码保持干净和可读性...而且,我对幕后的python并不太了解,但是据我了解 它,应该可以更快地在一行中完成它(我将使用相当大的数组)。
测试用例:
1 2 3 4 5 6 7 8 | 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] |
使用
1.使用
一个选择
我们可以使用
1 | x_indexed = x[np.ix_(row_indices,col_indices)] |
B.分配
我们可以使用相同的符号将标量或可广播数组分配给那些索引位置。因此,以下工作适用于作业-
1 | x[np.ix_(row_indices,col_indices)] = # scalar or broadcastable array |
2.使用
我们也可以将布尔数组/掩码与
一个选择
因此,使用
1 | x[np.ix_(row_mask,col_mask)] |
B.分配
以下是作业的作品-
1 | x[np.ix_(row_mask,col_mask)] = # scalar or broadcastable array |
样品运行
1.将
输入数组和索引数组-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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] |
具有
1 2 3 4 5 6 7 | In [224]: np.ix_(row_indices,col_indices) # Broadcasting of indices Out[224]: (array([[4], [2], [5], [4], [1]]), array([[1, 2]])) |
进行选择-
1 2 3 4 5 6 7 | In [225]: x[np.ix_(row_indices,col_indices)] Out[225]: array([[76, 56], [70, 47], [46, 95], [76, 56], [92, 46]]) |
正如OP所建议的,这实际上与使用
1 2 3 4 5 6 7 | In [227]: x[np.asarray(row_indices)[:,None],col_indices] Out[227]: array([[76, 56], [70, 47], [46, 95], [76, 56], [92, 46]]) |
如前所述,对于作业,我们只是这样做。
行,列索引数组-
1 2 3 | In [36]: row_indices = [1, 4] In [37]: col_indices = [1, 3] |
使用标量进行分配-
1 2 3 4 5 6 7 8 9 10 11 | 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块(可广播数组)进行分配-
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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.将
输入数组-
1 2 3 4 5 6 7 8 9 | 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]]) |
输入行,列掩码-
1 2 3 | 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) |
进行选择-
1 2 3 4 5 | In [22]: x[np.ix_(row_mask,col_mask)] Out[22]: array([[88, 46, 44, 81], [31, 47, 52, 15], [74, 95, 81, 97]]) |
使用标量进行分配-
1 2 3 4 5 6 7 8 9 10 11 | 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块(可广播数组)进行分配-
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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]]) |
关于什么:
1 | x[row_indices][:,col_indices] |
例如,
1 2 3 4 5 6 7 8 9 10 11 12 | 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]]) |
1 2 3 4 5 6 7 8 9 10 11 12 | 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具有非常不同的规则。因此,应使用
因此,您只需要将您的
我认为您正在尝试执行以下(等效)操作之一:
1 2 | x_does_work = x[row_indices,:][:,col_indices] x_does_work = x[:,col_indices][row_indices,:] |
实际上,这将仅使用选定的行创建
1 | x_does_work = (x[row_indices,:])[:,col_indices] |