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 15 ms, sys: 76.9 ms, total: 91.9 ms
Wall time: 10.4 ms
[4]:
%time for _ in range(10): mylist2 = [x ** 2 for x in mylist]
CPU times: user 67 ms, sys: 181 ms, total: 248 ms
Wall time: 40.3 ms