本文介绍了为什么NumPy为x创建视图[[slice(None),1,2]]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在:

当您的索引类似 x [[1,slice(None),2]] ,你得到一个视图,因为切片整个轴允许一定的偏移,步幅和计数来表示原始数组的切片。

When you have an indexing like x[[1, slice(None), 2]], you get a view because slicing an entire axis allows for a certain offset, stride and count to represent the slice with the original array.

例如, x = np.arange(27).reshape(3,3,3).copy(),我们有:

In [79]: x_view = x[1, :, 2]  # or equivalently x[[1, slice(None), 2]]

In [80]: x_view
Out[80]: array([11, 14, 17])

In [81]: x_view.base
Out[81]:
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]]])

然后我们可以使用(不是公共API,YMMV的一部分)来说明偏移以从我们的原始数组中获取切片。

Then we can use numpy.byte_bounds (not part of the public API, YMMV) to illustrate the offset to get our slice from our original array.

In [82]: np.byte_bounds(x_view)[0] - np.byte_bounds(x_view.base)[0]
Out[82]: 88

这是有道理的,因为有11个8字节在切片中第一个值之前的整数,11。NumPy使用公式计算此偏移量,使用原始数组的步幅。

This makes sense, since there are 11 8-byte integers before the first value in the slice, 11. NumPy calculates this offset with a formula you can see here, using the strides of the original array.

In [93]: (x.strides * np.array([1, 0, 2])).sum()
Out[93]: 88

我们切片中的步幅简直就是大步前进是沿着我们正在切片的轴(或轴)上的 x 。即 x.strides [1] == x_view.strides [0] 。现在,偏移量,新步幅和计数一起是NumPy从原始数组中查看切片的足够信息。

The strides in our slice simply become whatever the strides were for x along the axis (or axes) on which we are slicing. i.e. x.strides[1] == x_view.strides[0]. Now together the offset, new strides and count are enough information for NumPy to view our slice from our original array.

In [94]: x_view.strides
Out[94]: (24,)

In [95]: x_view.size
Out[95]: 3

最后,你为什么用 x [[0,1,2]] 触发花式索引的原因是因为在没有完整的轴切片的情况下,它不是通常可以制定一些新的偏移量,字节顺序,步幅和计数,以便我们可以查看具有相同基础数据的切片。

Finally, the reason why you trigger fancy indexing with x[[0, 1, 2]] for instance is because in the absence of a full axis slice, it isn't generally possible to formulate some new offset, byte order, strides and count such that we can view the slice with the same underlying data.

这篇关于为什么NumPy为x创建视图[[slice(None),1,2]]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 17:18