Sep 03, 2021 - 5 min read

Erin Schaffer

NumPy is a popular Python library that offers a range of powerful mathematical functions. The library is widely used in quantitative fields, such as data science, machine learning, and deep learning. We can use NumPy to perform complex mathematical calculations, such as matrix multiplication.

Matrix multiplication can help give us quick approximations of very complicated calculations. It can help us with network theory, linear systems of equations, population modeling, and much more. In this tutorial, we’ll explore some basic computations with NumPy matrix multiplication.

Let’s get started!

**We’ll cover**:

Learn the fundamentals of Python data analysis with Educative’s 1-week free trial.

NumPy is an open-source Python library that we can use to perform **high-level mathematical operations** with arrays, matrices, linear algebra, Fourier analysis, and more. The NumPy library is very popular within scientific computing, data science, and machine learning. NumPy is compatible with popular data science libraries like pandas, matplotlib, and Scikit-learn. It’s **much faster than Python lists** because it integrates faster codes, such as C and C++, in Python. It also breaks down our tasks into multiple pieces and processes each piece concurrently.

Before we get started, let’s make sure we have NumPy installed. If you already have Python, you can install NumPy with one of the following commands:

conda install numpy

or

pip install numpy

To import NumPy into our Python code, we can use the following command:

import numpy as np

import numpy as np A = [[6, 7], [8, 9]] print(np.array(A) [0,0])

In the above code, we have matrix A `[[6, 7], [8, 9]]`

. We ask for the element given at `(0,0)`

, and our output returns `6`

. When we want to define the shape of our matrix, we use the number of rows by the number of columns. That means that matrix A has a shape of 2x2.

Now, let’s take a look at some different NumPy matrix multiplication methods.

There are **three main ways** to perform NumPy matrix multiplication:

`np.dot(array a, array b)`

: returns the scalar or dot product of two arrays`np.matmul(array a, array b)`

: returns the matrix product of two arrays`np.multiply(array a, array b)`

: returns the element-wise matrix multiplication of two arrays

Let’s take a closer look at each of the three methods:

Scalar multiplication is a simple form of matrix multiplication. A scalar is just a number, like `1`

, `2`

, or `3`

. In scalar multiplication, we **multiply a scalar by a matrix**. Each element in the matrix is multiplied by the scalar, which makes the output the same shape as the original matrix.

With scalar multiplication, the **order doesn’t matter**. We’ll get the same result whether we multiply the scalar by the matrix or the matrix by the scalar.

Let’s take a look at an example:

import numpy as np A = 5 B = [[6, 7], [8, 9]] print(np.dot(A,B))

Now, let’s multiply a 2-dimensional matrix by another 2-dimensional matrix. When multiplying two matrices, the **order matters**. That means that matrix A multiplied by matrix B is not the same as matrix B multiplied by matrix A.

Before we get started, let’s look at a visual for how the multiplication is done.

import numpy as np A = [[6, 7], [8, 9]] B = [[1, 3], [5, 7]] print(np.dot(A,B)) print("----------") print(np.dot(B,A))

Note: It’s important to note that we can only multiply two matrices if the number of columns in the first matrix is equal to the number of rows in the second matrix.

Get started with Python data analysis for free with our 1-week Educative Unlimited Trial. Educative’s text-based learning paths are easy to skim and feature live coding environments, making learning quick and efficient.

The `matmul()`

function gives us the matrix product of two 2-d arrays. With this method, we can’t use scalar values for our input. If one of our arguments is a 1-d array, the function converts it into a matrix by appending a 1 to its dimension. This is removed after the multiplication is done.

If one of our arguments is greater than 2-d, the function treats it as a stack of matrices in the last two indexes. The `matmul()`

method is great for times when we’re unsure of what the dimensions of our matrices will be.

Let’s look at some examples:

**Multiplying a 2-d array by another 2-d array**

import numpy as np A = [[2, 4], [6, 8]] B = [[1, 3], [5, 7]] print(np.matmul(A,B))

**Multiplying a 2-d array by a 1-d array**

import numpy as np A = [[5, 0], [0, 5]] B = [5, 2] print(np.matmul(A,B))

**One array with dimensions greater than 2-d**

import numpy as np A = np.arange(8).reshape(2, 2, 2) B = np.arange(4).reshape(2, 2) print(np.matmul(A,B))

The `numpy.multiply()`

method takes two matrices as inputs and performs element-wise multiplication on them. Element-wise multiplication, or Hadamard Product, multiples every element of the first matrix by the equivalent element in the second matrix. When using this method, both matrices should have the same dimensions.

Let’s look at an example:

import numpy as np A = np.array([[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]) B = np.array([[1, 2, 3, 4, 5], [5, 4, 3, 2, 1]]) print(np.multiply(A,B))

We can pass certain rows, columns, or submatrices to the `numpy.multiply()`

method. The sizes of the rows, columns, or submatrices that we pass as our operands should be the same. Let’s look at an example:

import numpy as np A = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) B = np.array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]) print(np.multiply(A[ 0,:], B[ 1,: ])) print("----------") print(np.multiply(A[ 1,:], B[ 0,:]))

Congrats on taking your first steps with NumPy matrix multiplication! It’s a complicated, yet important part of linear algebra. It helps us further our understanding of different aspects of data science, machine learning, deep learning, and other prevalent fields. There’s still a lot more to learn about NumPy and matrices. Some recommended topics to cover next are:

- NumPy matrix transpose
- NumPy arrays (ndarray)
- NumPy vectorization

To get started learning these concepts and more, check out Educative’s learning path **Python Data Analysis and Visualization**. This hands-on learning path will help you master the skills to extract insights from data using a powerful assortment of popular Python libraries.

*Happy learning!*

WRITTEN BYErin Schaffer

Join a community of more than 1.2 million readers. A free, bi-monthly email with a roundup of Educative's top articles and coding tips.