Transcript
-
Using NumPy efficiently David Cournapeau @cournape github.com/cournape
-
PyData stack today
-
Why would you care about NumPy ? • Used a
fundamental piece in many higher level Machine Learning libraries (scikit learn/image, pandas, Tensorflow/Chainer/PyTorch) • Required to understand the source code of those libraries • Historically: key enabler of python for ML and Data Science • NumPy is a library for array computing • Long history in computing (APL, J, K, Matlab, etc…): see e.g. http://jsoftware.com/ • Both about efficiency and expressivity
-
A bit of history • Early work for array computing
in Python (matrix-sig mailing list): • 1995: Jim Fulton, Jim Hugunin. Became Numeric • 1995-2000ies: Paul Dubois, Konrad Hinsen, David Ascher, Travis Oliphant and other contributed later • 2001: Numarray: Perry Greenfield, Rick White and Todd Miller • 2005: “grand unification” into NumPy, led by Travis Oliphant
-
Why the difference ? • Why (c)python is slow for
computation: genericity • E.g. lists can contains arbitrary python values • You need to jump pointers to access values • Note: accessing an arbitrary value in RAM costs ~ 100 cycles (as much as computing the exponential of a double in C !) From Python Data Science Handbook by Jake Vanderplas
-
Broadcasting 3/4 • Broadcasting rules: • If arrays have different
number of dimensions, insert new axes on the left until arrays have same number of dimensions • For each axis i, if arrays dimension[i] do not match, “stretch” the arrays where dimension[i] = 1 to match other array(s) • (if no match and dimension[i] ! = 1 -> error) From Python Data Science Handbook by Jake Vanderplas
-
Broadcasting 4/4 • A few notes: • Broadcasting is done
“logically”, and the temporary arrays are not created in memory • Integrated in the ufunc and multi-dimensional indexing infrastructure in NumPy code (see later) • Indices are broadcasted as well in fancy indexing (see later) • You can use np.broadcast_arrays to explicitly build arrays as if they were broadcasted
-
Indexing: views • One can use slices any time one
needs to extract “regular” subarrays • If arrays are solely indexed through slices, the returned array is a view (no data copied) import numpy as np x = np.arange(6).reshape(2, 3) print(x) print(x[:, ::2]) print(x[::2, ::2])
-
Examples
-
Indexing: fancy indexing • As soon as you index an
array with an array, you are using fancy indexing • Fancy indexing always returns a copy (why ?) • 2 main cases of fancy indexing: • Use an array of boolean (aka mask) • Use an array of integers • Fancy indexing can get too fancy…
-
Does not sound that fancy ?
-
How to go further • From Python to NumPy by
Nicolas Rougier: http:// www.labri.fr/perso/nrougier/from-python-to-numpy • 100 NumPy exercises by Nicolas Rougier: https:// github.com/rougier/numpy-100/blob/master/ 100%20Numpy%20exercises.md • Guide to NumPy: http://web.mit.edu/dvp/Public/ numpybook.pdf • “New” ND index by Mark Wiebe, with notes about speeding up indexing, etc.: https://github.com/numpy/numpy/blob/ master/doc/neps/nep-0010-new-iterator-ufunc.rst
-
Thank you