12

So I am trying to turn my float numpy array to integers. But when I do so:

array.astype(np.uint64)

it changes this array:

[ 550.  514.  451.  494.  490.  500.  ...]

to this one:

[549 513 450 493 489 499 ... ]

So, I was wondering if any of you had an idea of why this is? I have to get np.uint64 as output for the code I am using next.

Thank you in advance.

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
Alheana
  • 139
  • 1
  • 4
  • 5
    I am not able to reproduce the output you have mentioned , can you paste the code you have tried till now? – Satyadev May 11 '17 at 08:41
  • 9
    Numpy does not round when you cast from float to integer using `astype` but just cuts all digits after the decimal point. All your floats apparently are slightly smaller than the rounded output that you are seeing when printing it. Therefore, you get integers that are one smaller than what you are seeing before. Try e.g.: `np.array([549.999999999]).astype(np.uint64)`. – jotasi May 11 '17 at 08:45
  • 3
    So you may want round it (e.g. with `np.around`) before passing to `astype`. – Syrtis Major May 11 '17 at 11:39

1 Answers1

21

As was said in comments, casting to integers does not round, it simply truncates the non-integer part. A float that appears as 550. in console output may actually be 549.999999999 which will be truncated to 549. Use rounding before typecasting. An example:

>>> a = np.array([5, 6, 7], dtype=float) - 1e-12
>>> a
array([ 5.,  6.,  7.])
>>> a.astype(np.uint64)
array([4, 5, 6], dtype=uint64)
>>> np.around(a).astype(np.uint64)
array([5, 6, 7], dtype=uint64)