Python 用索引矩阵过滤nx维数组

Python 用索引矩阵过滤nx维数组,python,numpy,numpy-ndarray,Python,Numpy,Numpy Ndarray,我有一个nx维数组‘a’,形状是-(50000,32,32,3) 我想筛选属于以下索引的项-数组([5,10,15,…,49938,49952,49988]) 如何创建仅包含属于这些索引的项的新数组“B”。您可以使用 样本数据: >>> array = np.random.randn(10, 3, 3, 2) # 4-D array >>> filter_index_array = [2,4,8,9] # your filter index values &

我有一个nx维数组‘a’,形状是-(50000,32,32,3)

我想筛选属于以下索引的项-数组([5,10,15,…,49938,49952,49988])

如何创建仅包含属于这些索引的项的新数组“B”。

您可以使用

样本数据:

>>> array = np.random.randn(10, 3, 3, 2) # 4-D array
>>> filter_index_array = [2,4,8,9] # your filter index values
>>> filtered_array = np.array([x[i] for i in filter_index_array]) # This is how you can get your required index values.
>>> filtered_array.shape
(4, 3, 3, 2)
>>> len(filtered_array) == len(filter_index_array)
True

输出:

>>> array = np.random.randn(10, 3, 3, 2) # 4-D array
>>> filter_index_array = [2,4,8,9] # your filter index values
>>> filtered_array = np.array([x[i] for i in filter_index_array]) # This is how you can get your required index values.
>>> filtered_array.shape
(4, 3, 3, 2)
>>> len(filtered_array) == len(filter_index_array)
True
验证:

>>> array = np.random.randn(10, 3, 3, 2) # 4-D array
>>> filter_index_array = [2,4,8,9] # your filter index values
>>> filtered_array = np.array([x[i] for i in filter_index_array]) # This is how you can get your required index values.
>>> filtered_array.shape
(4, 3, 3, 2)
>>> len(filtered_array) == len(filter_index_array)
True

如果
idx
是您的数组
数组([5,10,15,…,49938,49952,49988])
,则:

result = A[idx]

将给出所需的筛选数组。

您可以添加具有预期输出的样本数据吗?不清楚模式是什么以及您确切期望的是什么?我的数组中有50000个项目,我只想根据我的索引按索引筛选项目array@shaikmoeed你试过了吗?它绝对可以和给定的形状一起工作。@shaikmoeed它会的。