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.
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.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.
marks < average does not give you one answer. It gives you one answer per mark:
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.
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
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.