Creating a Matrix Using NumPy Arrays

30 minutes
  • 4 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

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 pip to install the numpy package.

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 prices matrix 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.

Additional Resources

We're writing a small script that can calculate the revenue numbers for a chain of stores in our local area. Each store sells the same 4 products at the same price. We keep track of the number of sales of each product from each store, and we'd like to use those sales numbers to calculate the total revenue of the business for the month. Thankfully, we can use multi-dimensional arrays and matrices to calculate our values.

Here are the four products and the number of sales for each:

Store NamePenNotebookStaplerBackpack
North Store1412320
East Store952310
South Store60423690
West Store23289173

Here are the prices for each item that is sold in the stores:

Item NameItem Price
Pen1.50
Notebook4.25
Stapler6.00
Backpack25.99

Logging In

There are a couple of ways to get in and work with the code. One is to use the credentials provided in the hands-on lab overview page, log in with SSH, and use a text editor in the terminal.

The other is using VS Code in the browser. If you'd like to go this route, you will need to navigate the workstation server's public IP address (provided in the hands-on lab overview page) on port 8080 (example: http://PUBLIC_IP:8080). Your password will be the same password that you'd use to connect over SSH.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?