Numpy数组切片以返回切片数组和相应的数组索引
我正在尝试从一个生成两个Numpy阵列。一个是原始数组的切片,另一个代表可用于查找产生的值的索引。我可以通过示例解释的最好方法是:
import numpy as np
original = np.array([
[5, 3, 7, 3, 2],
[8, 4, 22, 6, 4],
])
sliced_array = original[:,::3]
indices_of_slice = None # here, what command to use?
for val, idx in zip(np.nditer(sliced_array), np.nditer(indices_of_slice)):
# I want indices_of_slice to behave the following way:
assert val == original[idx], "Error. This implementation is not correct. "
最终我的目标是一个数组,我可以使用nditer
和一个相应的数组indices_of_slices
,该数组返回原始查找索引(i,j,...)
。然后,切片阵列的值应等于索引中原始数组的值(i,j,...)。
主要问题是:我可以同时返回切片阵列时的新切片阵列以及值的索引吗?希望一切都很清楚!
编辑:这是两个阵列的预期打印输出:
# print(sliced_array)
# >>> [[5 3]
# >>> [8 6]]
# expected result of
# print(indices_of_slice)
# >>> [[(0 0) (0 3)]
# >>> [(1 0) (1 3)]]
I'm trying to generate two numpy arrays from one. One which is a slice slice of an original array, and another which represents the indexes which can be used to look up the values produced. The best way I can explain this is by example:
import numpy as np
original = np.array([
[5, 3, 7, 3, 2],
[8, 4, 22, 6, 4],
])
sliced_array = original[:,::3]
indices_of_slice = None # here, what command to use?
for val, idx in zip(np.nditer(sliced_array), np.nditer(indices_of_slice)):
# I want indices_of_slice to behave the following way:
assert val == original[idx], "Error. This implementation is not correct. "
Ultimately what I'm aiming for is an array which I can iterate through with nditer
, and a corresponding array indices_of_slices
, which returns the original lookup indices (i,j,...)
. Then, the value of the sliced array should be equal to value of the original array in index (i,j,...).
Main question is: Can I return both the new sliced array as well as the indices of the values when slicing a numpy array? Hopefully all is clear!
Edit: Here are the expected printouts of the two arrays:
# print(sliced_array)
# >>> [[5 3]
# >>> [8 6]]
# expected result of
# print(indices_of_slice)
# >>> [[(0 0) (0 3)]
# >>> [(1 0) (1 3)]]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用numpy的slice > 带有一点点的体操,以获取您要寻找的索引:
请注意,这与具有相反方向的切片作用:
You can use numpy's slice
np.s_[]
with a tiny bit of gymnastics to get the indices you are looking for:Note that this works with slices that have some reverse direction:
制作一个带相同切片的布尔数组:
mask
选择相同的值 - 但以ravel形的形式:我们可以从掩码中获取索引:
迭代地应用它们:
用高级索引进行测试:
这不是与高级索引的各种变化一起工作;例如,索引相反的订单或重复行与不匹配。
mask
选择相同的值,但按不同的顺序选择。slice
是视图
原始
的。也就是说,它使用相同的数据
。但是它从不同的点开始,并使用不同的步伐。通常,
numpy
索引是单向街道。它创建一个具有所需值的新数组,无论是视图还是复制,但不会创建或返回映射或反向映射。除某些特殊情况外,我们无法确定在Original
中找到sliced_arreay
值的位置。编辑
其他答案建议以
np.s _
:产生切片对象的元组。他仍然必须使用
arange
来生成索引,因为切片本身没有oinder.shape
信息。ogrid
可用于创建高级索引数组:meshgrid
sparse> sparse = true
提供类似的东西。或带有完全填充的数组:有多种将数组转换为(n,2)索引的方法,这些索引可以单独使用(例如我以前的代码中的
arg where
):ix_ <
ix_ < /code>是创建高级索引数组的另一种方便工具,相当于切片:
请记住,索引切片,如
on
ointer [:,, :: 3]
产生view < /代码>。用数组索引较慢,因为它可以复制。迭代索引甚至更慢。
nonZero
产生一个可直接用于索引数组的数组元组:arg where
给出相同的值迭代,或者有些尴尬:Make a boolean array with the same slicing:
mask
selects the same values - but in raveled form:We can get indices from the mask:
And iteratively apply them:
Testing this with advanced indexing:
This doesn't work with all variations of advanced indexing; for example, indices the reverse the order or duplicate rows won't match.
mask
selects the same values, but in a different order.The
slice
is aview
oforiginal
. That is it uses the samedata
. But it starts at a different point, and uses different strides.In general,
numpy
indexing is a one-way street. It creates a new array, whether view or copy, that has the desired values, but it does not create, or return, a mapping, or a reverse mapping. Except for some special cases, we can't identify where inoriginal
thesliced_array
values are found.edit
The other answer suggests starting with
np.s_
:That produces a tuple of slice objects. He still has to use
arange
to generate the indices, since the slices themselves don't haveoriginal.shape
information.ogrid
can be used to create advanced indexing arrays:meshgrid
withsparse=True
gives something similar. Or with fully populated arrays:There are various ways of transforming that array into a (n,2) set of indices that could be used individually (like
argwhere
in my previous code):ix_
is another handy tool for creating advanced indexing arrays, the equivalent of slices:Keep in mind that indexing with slices, as in
original[:,::3]
produces aview
. Indexing with array is slower, since it makes a copy. And iterative indexing is even slower.nonzero
produces a tuple of arrays that can be used directly to index the array:argwhere
gives the same values, but in a (n,2) form that has to be used iteratively, or in a somewhat awkward: