← Data detective

Whole lists of numbers with numpy

Turn a list of song lengths into a numpy array, then do sums on every number at once: times 60, an average, and a test that keeps only the long songs.

To change every number in a list, you would write a loop. With numpy you write one line.

A Python list can hold numbers, but it cannot do sums on them all at once. numpy (said "num-pie") is the tool behind pandas, and it gives you the array: a list of numbers you can add up, multiply and test in one step.

import numpy as np loads it, and np.array([...]) turns a list into an array. Copy those for now: you will use them every time.

How this lesson works: Predict, Run, Investigate, Modify, Make
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

lesson.py
import numpy as np

lengths = np.array([3.5, 4.2, 2.8, 5.1, 3.9, 4.4])

print(lengths * 60)

lengths * 60 has an array on the left. What does it do to each number?

The list holds six song lengths in minutes. What does the last line print?

RunRun it and compare the result with your prediction.

Run

Run it (numpy takes a few seconds to load the first time) and read each line of output.

Line What it does The loop it replaces
lengths * 60 Times 60 on every number A loop that builds a new list
lengths.mean() The average Adding in a loop, then dividing
lengths.max() The biggest Keeping the largest so far
lengths > 4 True or False for every number A loop with an if
lengths[lengths > 4] Only the numbers where the test is True A loop that keeps some
lesson.py
import numpy as np

lengths = np.array([3.5, 4.2, 2.8, 5.1, 3.9, 4.4])

print("Lengths:", lengths)
print("In seconds:", lengths * 60)
print("Average:", round(lengths.mean(), 2), "minutes")
print("Longest:", lengths.max(), "minutes")

print(lengths > 4)
long_songs = lengths[lengths > 4]
print(long_songs)
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.py
import numpy as np

lengths = np.array([3.5, 4.2, 2.8, 5.1, 3.9, 4.4])

long_songs = lengths[lengths > 4]
print(len(long_songs))
print(long_songs)

len(array) is how many numbers are in it.

Inside out: the test first, then the keep.

Run the code. How many songs are longer than 4 minutes?

What does `lengths > 4` give on its own?

ModifyChange the code on purpose and check the result.

Modify

Change the program so it prints the average length in seconds as well as minutes.

Try it yourselfYour edits are saved in this browser.
Build up to it
Which line gives the average length in seconds?

Need a hint?

Before a hint: what kind of stuck is this? Try a strategy first

Times every length by 60 first, then take the mean.

Another hint

(lengths * 60).mean()

Show the average length in seconds.

    Run your program. This ticks when your code does all of the above.

    MakeBuild something new using what you have learned. Expect a few tries; that is normal.

    Make

    Find out how many songs are shorter than 4 minutes, and the average length of just those songs.

    Try it yourselfYour edits are saved in this browser.
    Need a hint?

    Before a hint: what kind of stuck is this? Try a strategy first

    Make a short array with a test on lengths, then use len and .mean().

    Another hint

    short = lengths[lengths < 4]

    Print how many songs are under 4 minutes and their average length.

      Run your program. This ticks when your code does all of the above.

      Now you can
      Finished the course?Go over it: Data detective recapEvery idea in one place, to check you've got it.You reached the endBack to Data detective
      For teachers

      An optional extension after pandas: students make a numpy array from a list, multiply or average it in one line, and use a test inside square brackets to keep some numbers. About 25 minutes.

      Open the teacher guide: answers, solutions, slides and a worksheet.

      Students will be able to

      Running the lesson

      1. Predict: the repeated-list option catches students who know that list * 60 repeats a list. An array does the sum instead.
      2. Run: show the table's loop column to remind them of the loops from earlier lessons.
      3. Investigate: read the filter from the inside out. The test makes True and False for every number.
      4. Make: the test uses a number, so no quotes round 4.

      The hard part (on purpose)

      Reading a filter in one line. Say it aloud: the lengths where the length is over 4.

      When students get stuck

      Stretch and support

      Stretch: Print the lengths in seconds as whole numbers with np.round(lengths * 60).

      Support: Give the whole program with the 4 blank for students to fill in.

      Where it leads

      Pandas uses numpy underneath, so the same square-bracket test you used on a table works on an array too.

      What each step is for