Numpy: Is there an array size size?

I learned how to use Numpy, and I wanted to see the difference in the speed of summing a list of numbers, so I made this code:

np_array = numpy.arange(1000000)
start = time.time()
sum_ = np_array.sum()
print time.time() - start, sum_

>>> 0.0 1783293664

python_list = range(1000000)
start = time.time()
sum_ = sum(python_list)
print time.time() - start, sum_

>>> 0.390000104904 499999500000

The sum of python_list is correct.

If I do the same code with a total of up to 1000, both print the correct answer. Is there an upper limit on the length of the Numpy array, or is it associated with the Numpy sum function?

thanks for the help

+3
source share
3 answers

Numpy creates an array of 32-bit unsigned ints. When he sums them, he sums them into a 32-bit value.

if 499999500000L % (2**32) == 1783293664L:
    print "Overflowed a 32-bit integer"

You can explicitly select the data type when creating the array:

a = numpy.arange(1000000, dtype=numpy.uint64)
a.sum() -> 499999500000
+9
source

, , 32- int.

numpy . - .

>>> 499999500000 % 2**32
1783293664L
+9

, 499999500000 % 2**32 1783293664... .. numpy 2 ** 32, numpy.array, .

np_array = numpy.arange(1000000, dtype=numpy.uint64), , (, , , ).

dtype=numpy.object, numpy, Python; , .

+6

Source: https://habr.com/ru/post/1715467/


All Articles