numpy 数组和数组标量之间的不同行为

问题描述 投票:0回答:1

这是这个问题的后续。

当我们使用具有特定类型的 numpy array 时,它会在数字运算后保留其类型。
例如,如果需要,向

uint32
数组添加 1 会将值包装为 0(当数组包含最大
uint32
值时)并保留
uint32
类型的数组:

import numpy
a = numpy.array([4294967295], dtype='uint32')
a += 1   # will wrap to 0
print(a)
print(a.dtype)

输出:

uint32
[0]
uint32

此行为不适用于具有相同类型的数组标量

import numpy
a = numpy.uint32(4294967295)
print(a.dtype)
a += 1   # will NOT wrap to 0, and change the scalar type
print(a)
print(a.dtype)

输出:

uint32
4294967296
int64

但是根据数组标量文档

使用数组标量的主要优点是它们保留数组类型

...

因此,使用数组标量可以确保数组和标量之间具有相同的行为,无论值是否位于数组内部。

(重点是我的)

我的问题:
为什么我会观察到数组和标量之间的上述不同行为,尽管明确的文档表明它们应该表现相同?

python numpy numpy-ndarray numpy-scalar
1个回答
0
投票

正如评论中提到的:是的,这个文档充其量是不精确的。我认为它指的是同一类型标量之间的行为:

import numpy
a = numpy.uint32(4294967295)
print(a.dtype)  # uint32
a += np.uint32(1)   # WILL wrap to 0 with warning
print(a)  # 0
print(a.dtype)  # uint32

但是,您的示例的行为将由于 NEP 50 而发生变化。因此,尽管旧的行为令人沮丧,但除了等待之外,没有什么可做的,除非您想提交有关向后移植文档更改的问题。如迁移指南中所述。

最大的向后兼容性变化是,这意味着标量的精度现在可以一致地保留...... 之前返回

np.float32(3) + 3.
时,
float32
现在返回
float64

我已经确认在您的示例中,类型按预期保留。

import numpy
a = numpy.uint32(4294967295)
print(a.dtype)  # uint32
a += 1  # will wrap to 0 
print(a)  # 0
print(a.dtype)  # uint32
numpy.__version__  # '2.1.0.dev0+git20240318.6059db1'

第二个 NumPy 2.0 候选版本已发布,如果您想尝试一下: https://mail.python.org/archives/list/[电子邮件受保护]/thread/EGXPH26NYW3YSOFHKPIW2WUH5IK2DC6J/

© www.soinside.com 2019 - 2024. All rights reserved.