4.1. Numpy

NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

4.1.1. Rationale

NumPy is the fundamental package for scientific computing with Python. It contains among other things:

  • a powerful N-dimensional array object

  • sophisticated (broadcasting) functions

  • tools for integrating C/C++ and Fortran code

  • useful linear algebra, Fourier transform, and random number capabilities

Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.

4.1.2. Installation

Listing 4.119. Installation
$ pip install numpy
Listing 4.120. Upgrade
$ pip install --upgrade numpy
Listing 4.121. Check version
import numpy as np

np.__version__
# '1.19.1'

4.1.3. Performance

Listing 4.122. Results with Jupyter and %%timeit -n 1_000_000 -r 10
import numpy as np


np.arange(0, 100, step=2, dtype=float)
# 756 ns ± 10.3 ns per loop (mean ± std. dev. of 10 runs, 1000000 loops each)

np.array(range(0, 100, 2), dtype=float)
# 8.28 µs ± 364 ns per loop (mean ± std. dev. of 10 runs, 1000000 loops each)

np.array([x for x in range(0, 100) if x % 2 == 0], dtype=float)
# 9.76 µs ± 324 ns per loop (mean ± std. dev. of 10 runs, 1000000 loops each)

np.array([float(x) for x in range(0, 100) if x % 2 == 0])
# 12.7 µs ± 195 ns per loop (mean ± std. dev. of 10 runs, 1000000 loops each)

np.array([float(x) for x in range(0, 100, 2)])
# 8.35 µs ± 196 ns per loop (mean ± std. dev. of 10 runs, 1000000 loops each)

np.array([x for x in range(0, 100, 2)], dtype=float)
# 5.89 µs ± 77 ns per loop (mean ± std. dev. of 10 runs, 1000000 loops each)

4.1.4. References

NP1

Alex Chabot-Leclerc. Introduction to numerical computing with numpy: broadcasting rules. 2019. URL: https://youtu.be/ZB7BZMhfPgk?t=5142.

NP2

Alex Chabot-Leclerc. Introduction to numerical computing with numpy: visualizing multi-dimensional arrays. 2019. URL: https://youtu.be/ZB7BZMhfPgk?t=5142.

NP3

Wikipedia. Normal distribution. 2019. URL: https://en.wikipedia.org/wiki/Normal_distribution.

NP4

Wikipedia. Pearson correlation coefficient. 2019. URL: https://en.wikipedia.org/wiki/Pearson_correlation_coefficient.

NP5

Wikipedia. Poisson distribution. 2019. URL: https://en.wikipedia.org/wiki/Poisson_distribution.

NP6

Wikipedia. Polynomial. 2019. URL: https://en.wikipedia.org/wiki/Polynomial.

NP7

Wikipedia. Continuous uniform distribution. 2020. URL: https://en.wikipedia.org/wiki/Uniform_distribution_(continuous).