索引具有2个索引列表的2D Numpy数组
我有一个奇怪的情况。
我有一个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]
小怪兽爱吃肉
相关分类