Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy Type (In)consistency?

Tags:

python

numpy

In Numpy I tried the following. I suspect that this is not a bug. In case it is a feature, I do not understand it. Can somebody explain this? Thanks.

>>> np.array([173], dtype = np.uint8) * [360]
array([62280])
>>> np.array([173], dtype = np.uint8) * 360
array([-3256], dtype=int16)
>>> 
like image 566
Georg Michel Avatar asked Nov 10 '22 00:11

Georg Michel


1 Answers

The difference between these outputs is probably is caused by a bug in your numpy version.

The code

np.array([173], dtype = np.uint8) * [360]

is shorthand for:

np.array([173], dtype = np.uint8) * np.array([360])
# output array([62280])

And thus [360] is transformed to a numpy array with dtype=int. Multiplication takes the highest precision and thus it returns an array with int precision.

like image 94
Pieter Avatar answered Nov 15 '22 07:11

Pieter