• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Numpy Tips / How to do matrix multiplication with Numpy

How to do matrix multiplication with Numpy

December 28, 2022 by cmdlinetips

In this post, we will learn how to perform matrix multiplication with Pythin in Numpy. In Pythn we have at least a few ways to do matrix multiplication. Here we will learn how to use the Numpy function matmul(), which short for Matrix Multiplication.

To use Numpy’s matmul() function, we simply pass in the two numpy arrays that we want to multiply as arguments. Note that we can not multiply all matrices. The input matrices or arrays must have compatible shapes. Two matrices are compatible for multiplication if the number of columns in the first matrix is the same as the number of rows in the second matrix. when the arguments are compatible, matmul() will return a new array/matrix as the result of the matrix multiplication.

Let us import Numpy.

import numpy as np

Example 1: Matrix multiplication with matmul()

Let us first consider the example of multiple two matrices of the same dimension. In this example, we will be creating two matrices of dimesion 2×2 and multiply them with matmul() function.

First, let us create the first matrix with dim 2×2.

a = np.arange(4).reshape(2,2)
a

array([[0, 1],
       [2, 3]])

Now the second matrix with the same dimension.

b = np.arange(2,6).reshape(2,2)
b

array([[2, 3],
       [4, 5]])

We can multiply these two matrices of dim 2×2 using Numpy’s matmul() function by providing them as arguments. We get

np.matmul(a,b)

array([[ 4,  5],
       [16, 21]])

Example 2: Matrix multiplication with matmul()

In the second example, we consider multiplication of two matrices of dimesion 3×3. As we can see multiplying two 3×3 matrices is possible as they compatible.

a = np.arange(9).reshape(3,3)
a

array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
b = np.arange(3,12).reshape(3,3)
b

array([[ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
np.matmul(a,b)
array([[ 24,  27,  30],
       [ 78,  90, 102],
       [132, 153, 174]])

Example 3: Matrix multiplication tow matrices of different dimensions with matmul()

So far, we have seen examples of multiplying matrices with same dimension. In this example, let us multiply two matrices of different, but with compatible shapes, using Numpy’s matmul().

To be compatible, the column size of first matrix should match with row size of the second matrix. For example, we can multiply a 4×3 matrix with 3×3 matrix.

Let us create a 4×3 matrix with Numpy’s arange() and reshape() function.

a = np.arange(12).reshape(4,3)
a

array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

Here we create a 3×3 matrix

b = np.arange(9).reshape(3,3)
b

array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

And the result of multipying a 4×3 matrix with 3×3 matrix is another 4×3 matrix.

np.matmul(a,b)

array([[ 15,  18,  21],
       [ 42,  54,  66],
       [ 69,  90, 111],
       [ 96, 126, 156]])

Also not that matrix multiplication is not commutative. This means that the order of the matrices while performing multiplication matters. Therefore, a x b is not the same as b *a.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailNumpy reshape function with examples Linear Regression fit with Matrix Multiplication in PythonLinear Regression Using Matrix Multiplication in Python Using NumPy Default Thumbnail9 Basic Linear Algebra Operations with NumPy Default ThumbnailHow to do Cholesky Matrix Decomposition with Numpy

Filed Under: Numpy Tips Tagged With: Numpy matmul()

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version