Chapter 2 exercises: NumPy Arrays and Selection
On this page
Attempt these tasks before opening the worked answers. Use the chapter experiment notebook, or import NumPy as np and Pandas as pd in your own notebook. Retain your first attempt and explain each correction.
B2-02-E01 · Select a rectangular block
Select rows 0–1 and columns 1–2. Preserve a two-dimensional result of shape (2,2).
a=np.arange(1,13).reshape(3,4)One hint after an attempt
Write the row and column slices independently.
B2-02-E02 · Observe shared storage
Change b[0] to 99. Predict the new a, then make an independent copy for a second modification.
a=np.array([2,4,6,8])
b=a[1:3]One hint after an attempt
Compare a view with .copy().
B2-02-E03 · Choose a boolean subset
Select values strictly above 7 and below 16; verify [8,12,15].
a=np.array([3,8,12,15,20])One hint after an attempt
Create each comparison mask first.
Reflect and get feedback
Explain one result without looking at the code. Identify one boundary case that would change your method. An optional tutor prompt is: “Here is my attempt at B2-02-E03. Give one hint about the violated assumption; do not give the solution yet.” Then use the worked answers.