为什么numpy的as_步长会改变数组步长,而(默认)步长=无?

2024-04-25 07:48:30 发布

您现在位置:Python中文网/ 问答频道 /正文

根据docsnp.lib.stride_tricks.as_strided的步长参数默认为输入的步长:

[...] The strides of the new array. Defaults to x.strides.

但是,当我执行以下操作时,步幅会发生变化:

import numpy as np
from numpy.lib.stride_tricks import as_strided

print("The following should be identical:")
foo = np.arange(100).reshape(10, 10)
print(f"Foo strides: {foo.strides}")
bar = as_strided(foo, (5, 5))
print(f"Bar strides: {bar.strides}")

产生:

The following should be identical:
Foo strides: (40, 4)
Bar strides: (20, 4)

我在python 3.7.0中使用numpy 1.19。发生了什么事,我做错了什么


Tags: theimportnumpyfoolibasnpfollowing
1条回答
网友
1楼 · 发布于 2024-04-25 07:48:30

这是文档和代码之间的不匹配。{a1}的开头是这样的:

# first convert input to array, possibly keeping subclass
x = np.array(x, copy=False, subok=subok)
interface = dict(x.__array_interface__)
if shape is not None:
    interface['shape'] = tuple(shape)
if strides is not None:
    interface['strides'] = tuple(strides)

因此strides的实际默认值是'strides'键在x.__array_interface__中的任何值。对于C-连续数组,该值是None,而不是实际的步长元组,代码继续生成C-连续输出,而不是保持输入步长

相关问题 更多 >