Skip to content
Save the video (5 MB)
Open the course

Lessons ·

NumPy, the calculation engine

Create arrays, calculate without loops and filter with a mask, with Awa's prices and temperatures.

Transcript

Awa's sales have grown a lot, and she now has thousands of prices to process. With a list, Python has to look at them one by one, which becomes slow. So she needs a more powerful tool, and this tool is called NumPy.

NumPy is a library, in other words a toolbox that we add to Python. To use it, we write import numpy, and we add as np to give it a shorter nickname. Everybody uses this nickname.

Awa then creates an array with np dot array, giving it a list of prices. It looks like a list, but there is one big difference. Which one? A NumPy array contains only values of the same type, for example only numbers. That is what allows it to calculate very fast.

It can also do statistics by itself. For example, the mean method calculates the average, and the average price here is 2175.

If she wants to double all her prices, she needs a loop then? Actually no, and that is the whole strength of NumPy. She only has to write prices times 2, and the operation applies to all the elements in one go. Look: every label is multiplied by 2.

This is called vectorization. Imagine an employee who changes the labels one by one, then a giant stamp that changes them all at once. NumPy is the giant stamp. The code is shorter, and on large arrays, it runs much faster.

An array can also have rows and columns, like a gradebook. Here, each row is a student, and each column a subject. Read the code carefully: it is a list of rows, and each row is itself a list of three grades.

How do we take all the grades of a subject? We write grades, then between square brackets two pieces of information separated by a comma: first the rows, then the columns. The colon means all the rows, and the 1 refers to the second column, since we count from zero. We get 15, 11 and 13.

And if we want a total per subject? We use the axis parameter, which often surprises people at first, so let's take our time. With axis equals 0, Python goes down along the rows, and so it adds up each column.

For the first subject, 12 plus 16 plus 8 gives 36.

With axis equals 1, it is the opposite: Python moves along each row, and gives a total per student.

So simply remember this: axis equals 0 gives one result per column, and axis equals 1 one result per row.

And to keep only certain values, like the days when it was over 30 degrees? Let's take these six temperatures, each on its thermometer, and draw a line at 30 degrees.

Awa writes temperatures, then the greater than sign, then 30. NumPy then compares each temperature with 30, and it answers true or false for each one. This array of booleans is called a mask, because it hides the values where it is false.

Then all we have to do is put this mask between square brackets, and NumPy keeps only the values where it is true: 31, 35 and 33. No loop was needed.

Now it is your turn to do these calculations in the lesson, without watching the video again. Take your time, because it is by searching for yourself that you will remember them.

Voices: Google Chirp 3 HD (Despina, Enceladus).

Photos : One Idea LLC, StockSnap, CC0