numpy.array具有/ =运算符的怪异行为? [英] numpy.array's have bizarre behavior with /= operator?

查看:41
本文介绍了numpy.array具有/ =运算符的怪异行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数字数组标准化为范围(0,1],以便可以将它们用作加权的random.choice()的权重,所以我输入了这一行:

I'm trying to normalize an array of numbers to the range (0, 1] so that I can use them as weights for a weighted random.choice(), so I entered this line:

# weights is a nonzero numpy.array
weights /= weights.max()

但是,Pycharm表示 max()函数(参数 initial未填充)。我在REPL中使用/ =运算符并使用 regular除法( a = a / b )并得到与Pycharm认为不同的结果和错误:

However, Pycharm said there's an unfilled parameter to the max() function (Parameter 'initial' unfilled). I tried this in the REPL with the /= operator and with "regular" division (a = a / b) and got different results for both and a different error than Pycharm thought:

>>> a = numpy.array([1,2,3])
>>> a.max()
3
>>> a /= a.max()
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    a /= a.max()
TypeError: No loop matching the specified signature and casting was found for ufunc true_divide
>>> a = a/a.max()
>>> a
array([0.33333333, 0.66666667, 1.        ])

我还意识到,对于加权随机数, sum 所需的权重而不是对其进行归一化。但是用/ =操作将其除以总和即可得到完全相同的 TypeError (但Pycharm认为这样做还可以):

I also realized that for a weighted random, the weights needed to sum to one rather than be normalized to it. But dividing it by the sum yielded the exact same TypeError using the /= operation (but Pycharm thought this was okay):

>>> a = numpy.array([1,2,3])
>>> sum(a)
6
>>> a
array([1, 2, 3])
>>> a /= sum(a)
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    a /= sum(a)
TypeError: No loop matching the specified signature and casting was found for ufunc true_divide
>>> a = a / sum(a)
>>> a
array([0.16666667, 0.33333333, 0.5       ])

我在这里遇到了什么?是Numpy中的某个怪异错误,还是/ =运算符有其他用途或其他用途?我知道他们使用 __ truediv __ __ itruediv __ ,但是我看不出为什么一个有问题而另一个没有。我已经通过pip的最新版本的Numpy(在Windows x64上为1.19.2)确认了此行为。

What have I come across here? Is this some bizarre bug in Numpy or does the /= operator have a different use or something? I know they use __truediv__ and __itruediv__ but I can't see why one has a problem and the other doesn't. I have confirmed this behavior with the latest version of Numpy from pip (1.19.2 on Windows x64).

推荐答案

这是因为numpy关心类型。应用除法时,会将 int 更改为 float 。但是Numpy不会让你这样做!这就是为什么您的值应该已经在 float 中的原因。尝试以下操作:

It's because numpy cares the type. When you apply the division, you're changing int to float. But numpy won't let you do that! That's why your values should be already in float. Try this:

>>> a = np.array([1.0,2.0,3.0])
>>> a /= sum(a)
>>> a
array([0.16666667, 0.33333333, 0.5       ])

但是另一个为什么起作用?这是因为这不是就地解决方案,操作。因此,正在创建一个新的存储位置。新变量,新类型,因此此处numpy无关紧要。

But why did the other one work? It's because that's not an "in-place" operation. Hence a new memory location is being created. New variable, new type, hence numpy doesn't care here.

这篇关于numpy.array具有/ =运算符的怪异行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆