Chapter 8: NumPy recap and exercises

NumPy recap

face Josiah Wang

And that’s all the NumPy I will cover. Hopefully that is useful!

Here is a quick recap.

What is a NumPy array?

We started by looking at what a np.array is. Remember that np.array is just a shorthand for np.ndarray (N-dimensional array)! A 1-dimensional array is a vector in Mathematical terms, while higher dimensional arrays are matrices.

You have looked at how to construct an array, either by passing a list to the constructor, or using some convenience functions for some standard matrices like np.zeros(), np.ones(), np.full(), np.eye(), np.identity(), np.zeros_like(), np.arange(), np.linspace() etc.

You have also looked at various attributes of the np.ndarray class: .shape, .ndim, .size, and .dtype.

NumPy array operations

Then you looked at how you can perform vectorised operations on np.arrays. You looked at how operators like +, -, *, /, etc. operate on the whole array, without the need for loops. You can also use the equivalent functions like np.add() and np.subtract(). A new operator is @, which performs matrix multiplication (you can also use np.matmul()).

Note that while you would expect operators to require the correct matrix sizes, sometimes NumPy automatically performs array broadcasting to automatically expand compatible-sized arrays.

Manipulating NumPy arrays

We looked at how to access elements in a np.array just like in Python, except that you use a comma for higher dimensional arrays, e.g. x[1, 2]. Array slicing also works. You can also filter elements by using boolean expressions, e.g. x[x > 3].

You have also attempted to use different functions/methods to manipulate the shapes of np.arrays. Some useful ones include .transpose(), .flatten(), .ravel(), .reshape() and .squeeze(). np.newaxis is also useful for adding a new axis to an array, something you may need to obtain a compatible matrix for different operations.

You have also combined and split existing arrays, using np.stack(), np.concatenate(), np.split(), np.repeat() and np.tile().

You then looked at more vectorised functions provided by NumPy. There are many mathematical functions like np.sum(), np.median(), np.prod().np.cumsum(), and np.argmin(), to name a few. You can also use np.any() and np.all(), and also use np.nonzero() to find out which elements in an array are non-zero. You can use np.unique() to get a set of unique elements in an array (along with their frequency). You can also sort an array with np.sort() and np.argsort().

Other NumPy stuff

We also briefly looked at how to load and save np.arrays from/to the hard drive, generating random numbers, and getting Python to print out more than 1000 values in a np.array.

Final remarks

We have definitely covered a lot, but we have barely scratched the surface of what NumPy can do. We recommend referring to the official documentation/tutorial if you want to delve deeper into NumPy at your own time. More importantly, the best way to learn NumPy is like typical Python programming - hands-on practice, and applying your knowledge to various problems and tasks!

I also recommend this NumPy cheat sheet if you need a quick reference to recall how to do something. The more you use it, the more they will stick!

We will end the NumPy section bit with a few more exercises for you to practise your NumPy skills (these are optional). The last chapter of this lesson will be a brief introduction to Matplotlib, a Scientific visualisation package.