本文简要介绍 python 语言中  numpy.ndarray.strides  的用法。
- 用法:- ndarray.strides
- 遍历数组时要在每个维度中步进的字节元组。 - 元素的字节偏移量 - (i[0], i[1], ..., i[n])在一个数组中a是:- offset = sum(np.array(i) * a.strides)- 有关步幅的更详细说明可以在 NumPy 参考指南中的 “ndarray.rst” 文件中找到。 - 注意:- 想象一个 32 位整数数组(每个 4 字节): - x = np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]], dtype=np.int32)- 该数组以 40 个字节的形式存储在内存中,一个接一个(称为连续的内存块)。数组的步幅告诉我们必须在内存中跳过多少字节才能沿着某个轴移动到下一个位置。例如,我们必须跳过 4 个字节(1 个值)才能移动到下一列,但必须跳过 20 个字节(5 个值)才能到达下一行中的相同位置。因此,数组的步幅x将会 - (20, 4).- 例子:- >>> y = np.reshape(np.arange(2*3*4), (2,3,4)) >>> y 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]]]) >>> y.strides (48, 16, 4) >>> y[1,1,1] 17 >>> offset=sum(y.strides * np.array((1,1,1))) >>> offset/y.itemsize 17- >>> x = np.reshape(np.arange(5*6*7*8), (5,6,7,8)).transpose(2,3,1,0) >>> x.strides (32, 4, 224, 1344) >>> i = np.array([3,5,2,2]) >>> offset = sum(i * x.strides) >>> x[3,5,2,2] 813 >>> offset / x.itemsize 813
相关用法
- Python numpy ndarray.setflags用法及代码示例
- Python numpy ndarray.setfield用法及代码示例
- Python numpy ndarray.sort用法及代码示例
- Python numpy ndarray.size用法及代码示例
- Python numpy ndarray.shape用法及代码示例
- Python numpy ndarray.astype用法及代码示例
- Python numpy ndarray.flat用法及代码示例
- Python numpy ndarray.real用法及代码示例
- Python numpy ndarray.itemset用法及代码示例
- Python numpy ndarray.__class_getitem__用法及代码示例
- Python numpy ndarray.partition用法及代码示例
- Python numpy ndarray.transpose用法及代码示例
- Python numpy ndarray.flatten用法及代码示例
- Python numpy ndarray.resize用法及代码示例
- Python numpy ndarray.dtype用法及代码示例
- Python numpy ndarray.imag用法及代码示例
- Python numpy ndarray.dot用法及代码示例
- Python numpy ndarray.fill用法及代码示例
- Python numpy ndarray.item用法及代码示例
- Python numpy ndarray.nbytes用法及代码示例
- Python numpy ndarray.tobytes用法及代码示例
- Python numpy ndarray.copy用法及代码示例
- Python numpy ndarray.ctypes用法及代码示例
- Python numpy ndarray.view用法及代码示例
- Python numpy ndarray.base用法及代码示例
注:本文由纯净天空筛选整理自numpy.org大神的英文原创作品 numpy.ndarray.strides。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
