Introduction to NumPy#

NumPy operations perform complex calculations on entire arrays without the need for Python for loops, which can be slow for large sequences. NumPy’s speed is explained by its C-based algorithms, which avoid the overhead of Python code. To give you an idea of the performance difference, we measure the difference between a NumPy array and a Python list with a hundred thousand integers:

[1]:
import numpy as np
[2]:
myarray = np.arange(100000)
mylist = list(range(100000))
[3]:
%time for _ in range(10): myarray2 = myarray ** 2
CPU times: user 2.67 ms, sys: 11.3 ms, total: 14 ms
Wall time: 1.49 ms
[4]:
%time for _ in range(10): mylist2 = [x ** 2 for x in mylist]
CPU times: user 73.9 ms, sys: 320 ms, total: 394 ms
Wall time: 35.5 ms