3.4.3. Homework#
Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.
Sign in from the account menu before you start working on the assignment.
Write and run your code directly in the coding cells.
Multiple submissions are allowed. The highest score is kept.
1. Array Shape
A store manager tracks sales for three departments across four weeks. Treating those values as a 3 by 4 array is a reasonable way to describe the data shape.
2. Elementwise Operations
A pricing analyst wants to add the same fee to every item price. This is the kind of repeated numeric work that vectorized array operations are designed to express.
3. Index Positions
In Python-style array indexing, position 1 always refers to the first value in a sequence.
4. Boolean Masks
A Boolean mask can represent which stores met a sales target before selecting only those stores.
5. Random Seeds
Setting a random seed guarantees that every possible future simulation will produce different values each time it runs.
### 6. Revenue Array Totals
# Each row is one week and each column is one product.
# 1. Convert sales to a NumPy array.
# 2. Total each week with .sum(axis=1) and each product with .sum(axis=0).
# Print exactly:
# Week totals: [67 71]
# Product totals: [41 48 49]
import numpy as np
sales = [[20, 22, 25], [21, 26, 24]]
### Your code starts here.
### Your code ends here.
### 7. Elementwise Discounts
# Subtract each discount from the matching price without a loop.
# 1. Convert prices and discounts to NumPy arrays.
# 2. Subtract the arrays and store the result in final_prices.
# Print exactly:
# Final prices: [110 135 180]
import numpy as np
prices = [120, 150, 200]
discounts = [10, 15, 20]
### Your code starts here.
### Your code ends here.
### 8. Slice the Demand Series
# Daily demand for one product is stored in an array.
# 1. Select the last three values with a negative slice.
# 2. Select every other value, starting with the first, with a step of 2.
# Print exactly:
# Recent demand: [32 35 37]
# Every other day: [28 32 37]
import numpy as np
demand = np.array([28, 30, 32, 35, 37])
### Your code starts here.
### Your code ends here.
### 9. Compare to Target
# Find which stores met the sales target.
# 1. Build a Boolean mask that is True where sales are at or above target.
# 2. Use the mask to select the matching store names, and count them.
# Print exactly:
# Stores meeting target: ['B' 'C']
# Count: 2
import numpy as np
stores = np.array(["A", "B", "C", "D"])
sales = np.array([80, 105, 110, 95])
target = 100
### Your code starts here.
### Your code ends here.
### 10. Simulated Demand
# Simulate 1,000 days of demand with the seeded generator below.
# 1. Draw 1,000 whole numbers from 40 up to (not including) 61 with
# rng.integers(40, 61, size=1000) and store them in demand.
# 2. Print the average demand rounded to 1 decimal.
# 3. Print the share of days with demand above 55, rounded to 3 decimals.
# Print exactly:
# Average simulated demand: 49.9
# Share above 55: 0.242
import numpy as np
rng = np.random.default_rng(3420)
### Your code starts here.
### Your code ends here.