避免成功广播标量

发布于 2025-02-03 13:37:22 字数 3382 浏览 3 评论 0 原文

减去乘法 arrays,numpy 广播 它们的形状相同:

>>> np.array(((2,3),(4,5))) - np.array((1,2,3,4))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (2,2) (4,) 
>>> np.array((2,3,4,5)) - np.array((1,2,3,4))
array([1, 1, 1, 1])
>>> np.array((2,3,4,5)) - np.array(1)
array([1, 2, 3, 4])
>>> np.array((2,3,4,5)) * np.array(2)
array([ 4,  6,  8, 10])

虽然最后一个操作是有意义的 数学上 (一个 can can multiply a vector 标量),倒数第二个不是不是(一个不能 添加或减去标量)。

我想知道是否可以告诉Numpy 将标量广播到 在加法上下文中的向量或矩阵,升高 value eRror:操作数不能合并在一起

理由: 乘以数组的每个元素乘以标量,并将标量添加到数组的每个元素中

一个向量是a 向量 space ,即, abelian 小组其元素可以 另外乘以 scalar ,即,一个数字。 IOW,乘以 vector(或矩阵)以一个数字是自然的操作 具有明确定义的属性,例如 distribunitivity &amp; c。

但是, vector 中添加标量 均未定义

为什么?!因为您不能在没有指定基础的情况下定义它

工程师称为 vector (即,数字序列),一个 数学家称为(摘要)向量的坐标表示 具体基础。 当您定义时,

a + [b,c,d,...] := [a+b,a+c,a+d,...]

我看到的是您选择了一个任意向量(巨大的规范!) y = [1,1,1,...] 并定义

a + X := a*Y + X

,但是为什么选择此特定向量?它比任何其他人都要好吗? 我们都喜欢规范的基础,但是在不同的基础上,这个向量可能会看起来 不同的!

当前的行为可能导致实际错误,例如,如果某人想添加随机错误并写入

my_vector + np.random.normal()     # WRONG! each component gets the SAME error!

正确的错误,而不是正确的错误

my_vector + np.random.normal(size=len(my_vector))

,则在处理矩阵时, >是 a ring 因此,它们 do 具有规范的非0元素 -

10 + array([[1,2],[3,4]])

应该是 array([[[11,2],[3,14]])不是 array([[[11,12],[13,14) ]]))! (出于同样的原因:矩阵只是线性操作员的表示, 它们在不同的基础上看起来不同)。

请注意,单位矩阵在任何基础上都是相同的。

请参阅您可以在矩阵中添加标量吗?以获取2D中的类似说明。

When subtracting or multiplying arrays, numpy broadcasts them to the same shape:

>>> np.array(((2,3),(4,5))) - np.array((1,2,3,4))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (2,2) (4,) 
>>> np.array((2,3,4,5)) - np.array((1,2,3,4))
array([1, 1, 1, 1])
>>> np.array((2,3,4,5)) - np.array(1)
array([1, 2, 3, 4])
>>> np.array((2,3,4,5)) * np.array(2)
array([ 4,  6,  8, 10])

while the last operation makes sense mathematically (one can multiply a vector by a scalar), the penultimate one does not (one cannot add or subtract a scalar to a vector).

I wonder if it is possible to tell numpy not to broadcast scalars to
vectors or matrices in additive context, raising ValueError: operands could not be broadcast together instead.

Rationale: the difference between multiplying each element of an array by a scalar, and adding the scalar to each element of the array

A vector is an element of a Vector
space
, i.e., an Abelian
group
whose elements can
additionally be multiplied by a
scalar, i.e., a number.
IOW, multiplying a vector (or a matrix) by a number is a natural operation
with well defined properties like distributivity &c.

However, adding a scalar to a vector is not defined at all.

Why?! Because you cannot define it without specifying a basis.

What an engineer calls a vector (i.e., a sequence of numbers), a
mathematician calls a coordinate representation of an (abstract) vector in a
specific basis.
IOW, when you define

a + [b,c,d,...] := [a+b,a+c,a+d,...]

what I see is that you have selected an arbitrary vector (of huge norm!)
Y=[1,1,1,...] and defined

a + X := a*Y + X

but why select this specific vector? how is it better than any other one?
We all love the canonical basis, but in a different basis this vector may look
different!

The current behavior can lead to actual errors, e.g., if someone wants to add a random error and writes

my_vector + np.random.normal()     # WRONG! each component gets the SAME error!

instead of the correct one

my_vector + np.random.normal(size=len(my_vector))

Also, when dealing with matrices, which are a
ring,
so they do have a canonical non-0 element - the
unit,
the numpy behavior is completely counter-intuitive!

10 + array([[1,2],[3,4]])

should be array([[11,2],[3,14]]), not array([[11,12],[13,14]])!
(for the same reason: matrices are just representations of linear operators, and
they look different in different bases).

Note that the unit matrix is the same in any basis.

Please see Can you add a scalar to a matrix? for a similar explanation in 2d.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文