0
0
NumPydata~30 mins

Broadcasting with higher dimensions in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Broadcasting with higher dimensions
📖 Scenario: Imagine you work in a company that tracks sales data for different stores over several days. You have daily sales data for each store, and you want to apply a fixed tax rate to all sales to find the tax amount for each store on each day.
🎯 Goal: You will create a 3D NumPy array representing sales data for multiple stores over multiple days and apply a tax rate using broadcasting with higher dimensions. You will then output the tax amounts for each store and day.
📋 What You'll Learn
Create a 3D NumPy array called sales with shape (2, 3, 4) containing specific sales values.
Create a variable called tax_rate with the value 0.1 (10%).
Use broadcasting to multiply the sales array by tax_rate and store the result in tax_amount.
Print the tax_amount array.
💡 Why This Matters
🌍 Real World
Companies often need to apply fixed rates like taxes or discounts to large sets of sales data across multiple stores and days. Broadcasting helps apply these calculations efficiently without writing loops.
💼 Career
Understanding broadcasting with higher dimensions is essential for data scientists and analysts working with multi-dimensional data arrays in Python using NumPy.
Progress0 / 4 steps
1
Create the sales data array
Create a 3D NumPy array called sales with shape (2, 3, 4) containing these exact values: [[[10, 20, 30, 40], [15, 25, 35, 45], [20, 30, 40, 50]], [[12, 22, 32, 42], [17, 27, 37, 47], [22, 32, 42, 52]]].
NumPy
Need a hint?

Use np.array() and type the nested lists exactly as shown.

2
Set the tax rate
Create a variable called tax_rate and set it to 0.1 to represent a 10% tax.
NumPy
Need a hint?

Just assign 0.1 to the variable tax_rate.

3
Calculate tax amount using broadcasting
Use broadcasting to multiply the sales array by the tax_rate variable and store the result in a new variable called tax_amount.
NumPy
Need a hint?

Just multiply the array sales by tax_rate and assign it to tax_amount.

4
Print the tax amount array
Print the tax_amount array to see the tax for each store and day.
NumPy
Need a hint?

Use print(tax_amount) to display the result.