JIT — Intro to Data Science · Python Lab · Problem 19 of 30

Array statistics and masks

One question asked of every mark at once — and the array of yes-or-no answers that turns into a filter.

Asking questions of a whole array

Problem 18 did arithmetic to every value at once. The same idea answers questions: what is the average, how spread out are they, and — the useful one — which values are below it.

The summary functions np.mean(marks), np.min(marks), np.max(marks) and np.std(marks) each take the whole array and hand back a single number. You met sum, min and max in problem 7; these are the array versions, and they work the same way.
New word — standard deviation np.std measures how far the marks sit from their average, on average. A small number means the class is bunched together; a large one means it is stretched out. Two classes can share an average of 73 and look nothing alike, and this is the number that says so.
NumPy divides by the number of marks. Some tools — including pandas, later in this course — divide by one less, which gives a slightly larger figure. Neither is wrong; they answer subtly different questions. It is worth knowing that the same column can produce two different spreads depending on what computed it.
New word — boolean mask marks < average does not give you one answer. It gives you one answer per mark:
[False True False False True]
A whole array of True and False, lined up with the original. That array is called a mask, and on its own it is only mildly interesting.
Putting the mask back into the square brackets marks[below] hands back only the marks where the mask says True.
In problem 6 the square brackets held a position. Here they hold a whole array of yes-or-no answers, and what comes out is a shorter array containing exactly the values that qualified. This is how filtering is done on real data, and it is the same idea you will use on a pandas column in problem 22.

Counting a mask True counts as 1 and False as 0, so below.sum() adds up the mask and tells you how many qualified. len(below) would tell you how many were tested, which is the whole class — the mask is always the same length as the array it came from.

What you are building

Marks: [78 65 80 92 54] Average: 73.8 Spread: 13.090454537562858 Lowest: 54 Highest: 92 Below average? [False True False False True] Their marks: [65 54] How many below: 2
Coming later A mask picks values out of one array. In pandas the same mask picks whole rows out of a table, so filtering on marks also brings along each student's name and city — problem 22.

Build the programme

Your plan

The steps still to place

The whole programme is laid out below. Five pieces are missing. Three of them hand back a perfectly good number that answers a different question from the one asked.

Leave nothing on "choose…". A wrong pick does not always cause an error — sometimes it just prints something you did not expect, which is the harder kind of mistake to spot.

Run and read

Here is the whole programme. The marks are yours to change. Try a set that is tightly bunched and one that is spread out, and watch the spread figure rather than the average.

Your programme

Output

Nothing has run yet.