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()
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. ])
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 ])
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).
a.dtype? What do you expect after the inplace division?atonp.float32. In-place division is not defined on integers due to the possibility of requiring type conversion to a floating-point representation.float64orint32(which will get converted tofloat64as a result of the operation I'm trying to do). Both array types exhibit this problem.np.array([1.0,2.0,3.0])works butnp.array([1,2,3])throws the error