4.1. Numpy

NumPy jest podstawowym pakiet (dodatkowym) w Pythonie do obliczeń naukowych. Integruje on niskopoziomowe biblioteki takie jak BLAS i LAPACK lub ATLAS. Podstawowe właściwości NumPy to :

  • potężny N-wymiarowy obiekt tablicy danych

  • rozbudowane funkcje

  • narzędzia do integracji z kodem napisanym w C/C++ i Fortranie

  • narzędzia do algebry liniowej, transformaty Fouriera czy generator liczb losowych

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.

$ pip install numpy
import numpy as np

Data Structures:

  • Skalar - jednowymiarowa

  • Wektor - dwuwymiarowa

  • Tensor - trójwymiarowa

  • Tablica - czterowymiarowa

  • Macierz - n-wymiarowa

Performance:
Listing 670. 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.1. 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.