Articles

NumPy Dot Product and Matrix Multiplication: Complete Guide

Matrix multiplication is at the core of many data science and machine learning tasks, and NumPy makes it both fast and intuitive. If you’ve ever wondered how and when to use np.matmul vs np.dot for matrix multiplication, you’re in the right place.

In this tutorial, we’ll discuss what matrix multiplication is and the different techniques for doing the same. Besides that, we’ll also cover the differences between np.dot and np.matmul.

Let’s start the discussion with a brief overview of matrix multiplication.

  • Learn about NumPy, a Python library used to store arrays of numbers, organize large amounts of data, and perform statistical calculations.
    • With Certificate
    • Intermediate.
      4 hours
  • Learn about linear algebra and how to perform operations with matrices and vectors.
    • Beginner Friendly.
      2 hours

What is matrix multiplication in NumPy?

Matrix multiplication is a foundational operation in linear algebra where rows of the first matrix are multiplied by columns of the second matrix to produce a new matrix. It’s commonly used in:

  • Solving systems of linear equations
  • Performing data transformations
  • Powering machine learning algorithms and neural networks
  • Working with 2D transformations in computer graphics

NumPy provides three primary methods to perform matrix multiplication, each suited for a specific use case:

  • np.multiply()
  • np.matmul()
  • np.dot()

Let’s try to understand each one in detail.

Element-wise multiplication with np.multiply()

The np.multiply() function in NumPy is used to perform element-wise multiplication of two arrays. Unlike matrix multiplication, where rows and columns are combined mathematically, element-wise multiplication multiplies the corresponding elements from two arrays or matrices of the same shape.

This operation is helpful in scenarios such as image processing, feature scaling, and applying weights to datasets.

Syntax of np.multiply() is:

numpy.multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])  

Parameters:

  • x1: First input array.
  • x2: Second input array. Must be broadcastable to the size of x1.
  • out (optional): A location into which the result is stored.
  • where (optional): A boolean array indicating where to perform the operation.
  • dtype, casting, order, subok: Optional advanced parameters controlling data type and memory usage.

Return value:

The np.multiply() returns a new array containing the result of the element-wise multiplication. The shape is the same as the broadcasted shape of the inputs.

Example:

import numpy as np
# Define two 2x2 NumPy arrays
A = np.array([[1, 2],
[3, 4]])
B = np.array([[5, 6],
[7, 8]])
# Element-wise multiplication
result = np.multiply(A, B)
print("Result of np.multiply():\n", result)

The output of this will be:

Result of np.multiply():
[[ 5 12]
[21 32]]

This output shows how element-wise multiplication works: each position in the first matrix (1,2,3,4) is multiplied by the corresponding position in the second matrix (5,6,7,8) to produce the result matrix (5,12,21,32).

While np.multiply() is limited to element-wise operations, it does not perform true matrix multiplication, and this is where np.matmul() comes in.

Using np.matmul() for matrix multiplication

The np.matmul() function in NumPy is explicitly designed for matrix multiplication, following the rules of linear algebra. Unlike np.multiply(), it doesn’t just multiply corresponding elements, it computes the dot product between rows and columns, making it the go-to method for multiplying 2D and higher-dimensional arrays.

This function is widely used in applications like linear transformations, machine learning, and solving matrix equations.

Syntax of np.matmul() is:

numpy.matmul(x1, x2, /, out=None, *, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])  

Parameters:

  • x1: First input array (must be 1-D or 2-D).
  • x2: Second input array. Inner dimensions must align for matrix multiplication.
  • out (optional): A location to store the output result.
  • casting, dtype, order: Other optional parameters that control data type and memory details.

Return value:

The np.matmul() returns the matrix product of the inputs. Shape depends on the broadcasting and dimensions of the inputs.

Example:

import numpy as np
# Define two 2x2 matrices
A = np.array([[1, 2],
[3, 4]])
B = np.array([[5, 6],
[7, 8]])
# Perform matrix multiplication
result = np.matmul(A, B)
print("Result of np.matmul():\n", result)

The output produced by this is:

Result of np.matmul():
[[19 22]
[43 50]]

This demonstrates matrix multiplication, where each element in the result is calculated by multiplying the dot product of the equivalent row from the first matrix by the column from the second matrix.

While np.matmul() is perfect for matrix products, np.dot() offers more flexibility by handling both vector dot products and matrix multiplication, depending on the input dimensions.

Using np.dot() for matrix multiplication

In NumPy, the np.dot() function calculates the dot product of two vectors. However, when both are 2D arrays, it treats them as matrices and calculates the dot product of them.

Syntax of np.dot() is:

numpy.dot(a, b, out=None)  

Parameters:

  • a: First input array.
  • b: Second input array.
  • out (optional): Optional output array to store the result.

Return value:

The np.dot() returns the dot product of the inputs. The shape and meaning depend on the input’s dimensions.

Example:

# Import the NumPy library
import numpy as np
# Define two NumPy matrices
mat1 = np.matrix([[1, 2], [3, 4]])
mat2 = np.matrix([[5, 6], [7, 8]])
# Using the `dot()` function
dot = np.dot(mat1, mat2)
# Print the matrix
print(dot)

Here is the output for this code:

[[19 22]
[43 50]]

This demonstrates the same matrix multiplication process as np.matmul(), where rows from the first matrix are combined with columns from the second matrix using dot products to create each element in the result matrix.

While np.dot() and np.matmul() often produce the same result with 2D arrays, they behave differently with other dimensions, which brings us to their key differences.

np.matmul() vs np.dot()

Here are the differences between np.matmul() and np.dot():

Feature np.dot() np.matmul()
1D arrays Dot product → scalar Treats as 1×n and n×1 → scalar
2D arrays Matrix multiplication Matrix multiplication
Higher dimensions Complex broadcasting rules Stacked (batch) matrix multiplication
Operator equivalent None @ operator (A @ B)
Specialization General dot product Strictly matrix multiplication

Conclusion

This guide covered how to multiply matrices in NumPy using np.multiply(), np.matmul(), and np.dot(). We also understood when to use np.matmul() vs np.dot() for matrix multiplication.

To keep learning and strengthening your foundation in data analysis, explore Codecademy’s Learn Statistics with NumPy course, where you’ll apply NumPy to real statistical problems using Python.

Frequently asked questions

1. What is np.dot() with example?

np.dot() in NumPy performs a vector dot product for 1D arrays (returning a scalar) and standard matrix multiplication for 2D arrays (returning a 2D array). For higher dimensions, it applies specific broadcasting rules to handle the multiplication.

Example of np.dot:

import numpy as np
# Dot product of two vectors
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dot(a, b)) # 1*4 + 2*5 + 3*6 = 32
# Matrix multiplication
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(np.dot(A, B))

Here is the output:

32
[[19 22]
[43 50]]

2. What is np.matmul() with example?

np.matmul() is specifically for matrix multiplication. Unlike np.dot(), it does not perform the vector dot product; for 1D arrays, it treats them as row/column vectors. It also supports stacked matrix multiplication for higher-dimensional arrays.

Example of np.matmul():

import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(np.matmul(A, B))
# With higher dimensions (batch multiplication)
X = np.arange(8).reshape(2, 2, 2)
Y = np.arange(8, 16).reshape(2, 2, 2)
print(np.matmul(X, Y))

Here is the output:

[[19 22]
[43 50]]
[[[ 10 11]
[ 46 51]]
[[118 127]
[170 183]]]

3. What is np.matmul() vs np.dot()?

  • np.dot() performs a dot product for 1D arrays, matrix multiplication for 2D arrays, and uses complex broadcasting for higher dimensions.
  • np.matmul() is strictly for matrix multiplication, supports batch operations, treats 1D arrays as 1xn and nx1, and has the @ operator equivalent.

4. Is np.dot() faster than np.matmul()?

For 2D matrices, both call the same BLAS (Basic Linear Algebra Subprograms) routines and run at essentially the same speed. np.dot may be slightly faster for 1D vectors, while np.matmul is better for higher-dimensional (batched) matrix multiplications.

5. What is the difference between np.dot() and np.multiply()?

  • np.dot() performs matrix multiplication or vector dot products, depending on the input dimensions.
  • np.multiply() carries out element-wise multiplication, multiplying corresponding elements of two arrays.
Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team

Learn more on Codecademy

  • Learn about NumPy, a Python library used to store arrays of numbers, organize large amounts of data, and perform statistical calculations.
    • With Certificate
    • Intermediate.
      4 hours
  • Learn about linear algebra and how to perform operations with matrices and vectors.
    • Beginner Friendly.
      2 hours
  • Learn how to organize data and automate repetitive tasks with arrays and loops.
    • Beginner Friendly.
      3 hours