If you’re in the position where your work requires you to work with n-dimensional arrays (matrices), then `numpy` will be a staple tool in your toolbelt. Knowing how to create matrices using arrays is a fundamental skill for working with `numpy`. In this hands-on lab, we go through the process of installing `numpy`, building up some arrays, and combining them to create a matrix.
To feel comfortable completing this lab, you’ll want to know how to do the following:
– Using NumPy arrays. Watch the “What are NumPy Arrays?” video from the [Using Python’s Math, Science, and Engineering Libraries](https://linuxacademy.com/cp/modules/view/id/62) course.
– Using NumPy matrices. Watch the “Reshaping a NumPy Array into a Matrix” video from the [Using Python’s Math, Science, and Engineering Libraries](https://linuxacademy.com/cp/modules/view/id/621) course.
Successfully complete this lab by achieving the following learning objectives:
- Install NumPy
Before we can start working with NumPy, we’ll need to make sure that we have it installed. Utilize
pipto install the
- Creating NumPy Arrays for Each Store’s Sales and Item Prices
Since we have the sales numbers for each item from each store, we can create an array for each store’s sales that can later be used to make calculations. We need to make sure that we’re putting the sales numbers for each item in the same position for each of our stores’ arrays. Create a 4 item array for each store, placing the number of sales for each item in the order of pens, notebooks, staplers, and backpacks.
- Create the Sales and Prices Matrix
Because we have 4 different items, each stores’ sales are stored as a 4 item, one-dimensional array, or a 1×4 matrix. We can use matrix multiplication to calculate the revenue for a store by multiplying that array with a 4×1 matrix where each row is the price for the corresponding column item in the sales arrays. Combine all of our sales variables into a single 4×4 matrix so that we can calculate the revenue for all of the stores at the same time, and create a 4×1
pricesmatrix that has the prices list from top to bottom for pen, notebook, stapler, and backpack.
- Calculate Store Revenues and Total Revenue
Calculate the dot product of these two matrices to get the revenue for each store and then utilize those values to calculate the total revenue.