BANA409BANA409

NumPy In-Class Exercise

You run a small coffee chain with three stores. For one month you recorded, for every store, every week, and every product, how many units were sold. That is a 3-dimensional block of numbers, and it is exactly the kind of thing NumPy exists for.

Work in a notebook called numpy-exercise.ipynb. Two rules for the whole exercise:

  1. No for loops over the array. Every answer comes from slicing, arithmetic, or an aggregation function. If you find yourself writing a loop to add numbers up, you are solving it the pre-NumPy way.
  2. Predict before you run. Several tasks below ask you to write your predicted answer or shape in a markdown cell first, then run the code and compare. The prediction is the part that teaches you something. A wrong prediction that you can explain afterwards is worth more than a right answer you never thought about.

Setup

Put this in your first cell. The seed means everyone in the room gets the same numbers, so you can compare answers with your neighbour.

import numpy as np

np.random.seed(409)  # everyone gets identical data

# units sold: 3 stores x 4 weeks x 5 products
sales = np.random.randint(20, 80, size=(3, 4, 5))

stores = ['Downtown', 'Campus', 'Airport']
products = ['Latte', 'Espresso', 'Cold Brew', 'Sandwich', 'Cookie']
price = np.array([3.5, 3.0, 4.5, 6.5, 2.0])  # USD per unit, same order as products

Part 1 — Read the Shape

Print sales, then print its ndim, shape, size, and dtype.

In a markdown cell, write one sentence per axis saying what that axis counts. Then answer: what does sales[0, 3, 2] mean in business terms, and what is its value? Say the sentence out loud in the form "this is the number of ___ sold at ___ during ___."

Check: size is 60, and sales[0, 3, 2] is 40.

Part 2 — Slice It

Write one line of code for each. Before running each line, write down the shape you expect.

  • All four weekly numbers for Cold Brew at the Campus store
  • Week 1 only, for every store and every product
  • The Airport store, weeks 2 and 3, for the last two products only

Check: the first gives [61 58 56 59], the second has shape (3, 5), the third has shape (2, 2) and starts with 52.

Part 3 — Aggregate Along an Axis

This is the part that matters most. Compute:

  • The total number of units sold in the whole month
  • sales.sum(axis=0), sales.sum(axis=1), and sales.sum(axis=2)

The three sums have shapes (4, 5), (3, 5), and (3, 4). In a markdown cell, write one sentence for each explaining what a single number inside that result represents. Not "it sums the first axis" — say what a store manager would call it.

Now produce three clearly labelled results:

  • Total units per store
  • Total units per product
  • Total units per week

Each of those three collapses two axes at once, so axis= needs a tuple — see Aggregating Over Multiple Axes in the reading.

Check: per store you should get [934, 985, 1012], and per week [844, 686, 718, 683].

Week 4 is the weakest week at 683 units. Week 2 is 686. In a markdown cell, answer: would you tell the owner that week 4 was a bad week? Why or why not?

Part 4 — Broadcasting

Multiply units by price to get revenue:

revenue = sales * price

This is broadcasting, and the reading gives you the rule. Answer in a markdown cell: sales has shape (3, 4, 5) and price has shape (5,). Why does this multiplication work at all, and which price got applied to which numbers?

Then compute total revenue per store and total revenue per product.

Check: revenue per store is [3628.0, 3881.5, 3902.5].

Now look at your two "best product" answers. By units sold, Cookie wins. By revenue, Sandwich wins. Write three sentences explaining to the owner which product you would call the best and why the two rankings disagree.

Finally, break it on purpose. Each store has a different weekly operating cost multiplier:

cost = np.array([1.0, 1.2, 0.9])   # one number per store
sales * cost                        # this fails - read the error message

Read the error, then fix it with cost.reshape(3, 1, 1). In a markdown cell, explain why an array of shape (5,) combined with sales without complaint but an array of shape (3,) did not.

Checkpoint Questions

Answer these in a markdown cell at the end of your notebook, in your own words and without running code.

  1. sales has shape (3, 4, 5). How many numbers are in it, and why?
  2. What does axis=1 mean for this array, in business words rather than "the second axis"?
  3. What shape does sales.sum(axis=2) return, and what is one number in that result?
  4. What is the difference between sales[1] and sales[:, 1]?
  5. Why did multiplying by the shape (5,) price array work while the shape (3,) cost array failed?
  6. Cookie sells the most units and Sandwich earns the most revenue. Which is the better product, and what would change your answer?
  7. Point at any line in your notebook that AI wrote, and explain what each part of it does.

Submission

Upload your finished notebook to Canvas for grading.

On this page