Chapter 3 exercises: Computing with NumPy
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-03-E01 · Name the reduction axis
Find one total per column and one total per row; name the retained axis meaning.
visits=np.array([[10,14],[20,22],[30,36]])One hint after an attempt
Which dimension disappears?
B2-03-E02 · Broadcast an adjustment
Add one offset per row without a loop; preserve shape (3,2).
a=np.array([[1,2],[3,4],[5,6]])
offset=np.array([10,20,30])One hint after an attempt
Insert a column-shaped dimension.
B2-03-E03 · Handle a numerical domain
Calculate logarithms only for positive entries and retain an explicit missing result for zero.
a=np.array([0.,1.,4.,9.])One hint after an attempt
Prepare the output, then assign through a valid-domain mask.
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-03-E03. Give one hint about the violated assumption; do not give the solution yet.” Then use the worked answers.