Simplest solution

Use numpy.dot or a.dot(b). See the documentation here.

Use with AI>>> a = np.array([[ 5, 1 ,3], 
                  [ 1, 1 ,1], 
                  [ 1, 2 ,1]])
>>> b = np.array([1, 2, 3])
>>> print a.dot(b)
array([16, 6, 8])

This occurs because numpy arrays are not matrices, and the standard operations *, +, -, / work element-wise on arrays.

Note that while you can use numpy.matrix (as of early 2021) where * will be treated like standard matrix multiplication, numpy.matrix is deprecated and may be removed in future releases.. See the note in its documentation (reproduced below):

It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.

Thanks @HopeKing.


Other Solutions

Also know there are other options:

  • As noted below, if using python3.5+ and numpy v1.10+, the @ operator works as you'd expect:

    Use with AI>>> print(a @ b)
    array([16, 6, 8])
    
  • If you want overkill, you can use numpy.einsum. The documentation will give you a flavor for how it works, but honestly, I didn't fully understand how to use it until reading this answer and just playing around with it on my own.

    Use with AI>>> np.einsum('ji,i->j', a, b)
    array([16, 6, 8])
    
  • As of mid 2016 (numpy 1.10.1), you can try the experimental numpy.matmul, which works like numpy.dot with two major exceptions: no scalar multiplication but it works with stacks of matrices.

    Use with AI>>> np.matmul(a, b)
    array([16, 6, 8])
    
  • numpy.inner functions the same way as numpy.dot for matrix-vector multiplication but behaves differently for matrix-matrix and tensor multiplication (see Wikipedia regarding the differences between the inner product and dot product in general or see this SO answer regarding numpy's implementations).

    Use with AI>>> np.inner(a, b)
    array([16, 6, 8])
    
    # Beware using for matrix-matrix multiplication though!
    >>> b = a.T
    >>> np.dot(a, b)
    array([[35,  9, 10],
           [ 9,  3,  4],
           [10,  4,  6]])
    >>> np.inner(a, b) 
    array([[29, 12, 19],
           [ 7,  4,  5],
           [ 8,  5,  6]])
    
  • If you have multiple 2D arrays to dot together, you may consider the np.linalg.multi_dot function, which simplifies the syntax of many nested np.dots. Note that this only works with 2D arrays (i.e. not for matrix-vector multiplication).

    Use with AI  >>> np.dot(np.dot(a, a.T), a).dot(a.T)
      array([[1406,  382,  446],
             [ 382,  106,  126],
             [ 446,  126,  152]])
      >>> np.linalg.multi_dot((a, a.T, a, a.T))
      array([[1406,  382,  446],
             [ 382,  106,  126],
             [ 446,  126,  152]])
    

Rarer options for edge cases

  • If you have tensors (arrays of dimension greater than or equal to one), you can use numpy.tensordot with the optional argument axes=1:

    Use with AI>>> np.tensordot(a, b, axes=1)
    array([16,  6,  8])
    
  • Don't use numpy.vdot if you have a matrix of complex numbers, as the matrix will be flattened to a 1D array, then it will try to find the complex conjugate dot product between your flattened matrix and vector (which will fail due to a size mismatch n*m vs n).

Answer from wflynny on Stack Overflow
🌐
DataCamp
datacamp.com › doc › numpy › matrix-multiplication
NumPy Matrix Multiplication
In these syntaxes, matrix_a and matrix_b are arrays or matrices to be multiplied, producing a new matrix as the result. For higher-dimensional arrays, np.matmul() and @ handle the last two dimensions as matrices and broadcast the remaining dimensions, unlike np.dot(), which performs a more ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › matrix-multiplication-in-numpy
Matrix Multiplication in NumPy - GeeksforGeeks
In Python, NumPy provides a way to compute matrix multiplication using numpy.dot() function.
Published: September 22, 2025
Discussions

python - numpy matrix vector multiplication - Stack Overflow
Find centralized, trusted content ... you use most. Learn more about Collectives ... Save this question. Show activity on this post. ... Closed 12 years ago. When I multiply two numpy arrays of sizes (n x n)*(n x 1), I get a matrix of size (n x n). Following normal matrix multiplication rules, an (n x 1) vector is expected, but I simply cannot find any information about how this is done in Python's Numpy ... More on stackoverflow.com
🌐 stackoverflow.com
Help with matrices (without using numpy)
If A is a matrix (2D list), len(A) will be the # of rows, and len(a row) will be the # of columns. Use that to get (and compare) dimensions. AB(i,j) = (row i of A) dot (col j of B) Suppose my two matrices (A and B) are [[1,2], [[1,2,3], [3,4] and [4,5,6]] and you want the (0-based indexing) entry in row 1, col 2 that comes from [3,4] dot [3,6]. What do the indices of A look like in that row? What do the indices of B look like in that column? More on reddit.com
🌐 r/learnpython
6
8
February 19, 2021
matrix multiplication in Numpy
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
3
0
October 30, 2023
Numpy Matrix Multiplication
Not at a computer but something like arr1 [:, None,:] * arr2[:,:, None] Maybe? Or maybe its arr1[None,:,:].None is np.newaxis and it allows you to do broadcasting. I'm probably way off. More on reddit.com
🌐 r/learnpython
2
3
April 21, 2016
People also ask

Why do I get shapes not aligned in NumPy matrix multiplication?
Because the inner dimensions do not match. For A @ B, the number of columns in A must equal the number of rows in B. Print A.shape and B.shape to check the inner pair; often a transpose like A @ B.T lines them up.
🌐
runxbuild.com
runxbuild.com › home › blog › matrix multiplication in python: use numpy and the @ operator, not nested loops
Matrix Multiplication in Python: Use NumPy and the @ Operator, ...
Should I write my own matrix multiplication loop in Python?
No, except to learn. Nested-loop matrix multiplication in pure Python is correct but often hundreds of times slower than NumPy, which runs in optimized C with vectorized instructions. For any real numeric work, use A @ B and let NumPy do the computation.
🌐
runxbuild.com
runxbuild.com › home › blog › matrix multiplication in python: use numpy and the @ operator, not nested loops
Matrix Multiplication in Python: Use NumPy and the @ Operator, ...
How do I multiply matrices in Python?
Use NumPy and the @ operator: A @ B, where A and B are NumPy arrays. It performs real matrix multiplication using the rows-times-columns rule. np.matmul(A, B) is equivalent. Avoid writing nested loops, which are far slower.
🌐
runxbuild.com
runxbuild.com › home › blog › matrix multiplication in python: use numpy and the @ operator, not nested loops
Matrix Multiplication in Python: Use NumPy and the @ Operator, ...
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.matmul.html
numpy.matmul — NumPy v2.5 Manual
If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed. (For stacks of vectors, use matvec.)
Top answer
1 of 1
475

Simplest solution

Use numpy.dot or a.dot(b). See the documentation here.

Use with AI>>> a = np.array([[ 5, 1 ,3], 
                  [ 1, 1 ,1], 
                  [ 1, 2 ,1]])
>>> b = np.array([1, 2, 3])
>>> print a.dot(b)
array([16, 6, 8])

This occurs because numpy arrays are not matrices, and the standard operations *, +, -, / work element-wise on arrays.

Note that while you can use numpy.matrix (as of early 2021) where * will be treated like standard matrix multiplication, numpy.matrix is deprecated and may be removed in future releases.. See the note in its documentation (reproduced below):

It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.

Thanks @HopeKing.


Other Solutions

Also know there are other options:

  • As noted below, if using python3.5+ and numpy v1.10+, the @ operator works as you'd expect:

    Use with AI>>> print(a @ b)
    array([16, 6, 8])
    
  • If you want overkill, you can use numpy.einsum. The documentation will give you a flavor for how it works, but honestly, I didn't fully understand how to use it until reading this answer and just playing around with it on my own.

    Use with AI>>> np.einsum('ji,i->j', a, b)
    array([16, 6, 8])
    
  • As of mid 2016 (numpy 1.10.1), you can try the experimental numpy.matmul, which works like numpy.dot with two major exceptions: no scalar multiplication but it works with stacks of matrices.

    Use with AI>>> np.matmul(a, b)
    array([16, 6, 8])
    
  • numpy.inner functions the same way as numpy.dot for matrix-vector multiplication but behaves differently for matrix-matrix and tensor multiplication (see Wikipedia regarding the differences between the inner product and dot product in general or see this SO answer regarding numpy's implementations).

    Use with AI>>> np.inner(a, b)
    array([16, 6, 8])
    
    # Beware using for matrix-matrix multiplication though!
    >>> b = a.T
    >>> np.dot(a, b)
    array([[35,  9, 10],
           [ 9,  3,  4],
           [10,  4,  6]])
    >>> np.inner(a, b) 
    array([[29, 12, 19],
           [ 7,  4,  5],
           [ 8,  5,  6]])
    
  • If you have multiple 2D arrays to dot together, you may consider the np.linalg.multi_dot function, which simplifies the syntax of many nested np.dots. Note that this only works with 2D arrays (i.e. not for matrix-vector multiplication).

    Use with AI  >>> np.dot(np.dot(a, a.T), a).dot(a.T)
      array([[1406,  382,  446],
             [ 382,  106,  126],
             [ 446,  126,  152]])
      >>> np.linalg.multi_dot((a, a.T, a, a.T))
      array([[1406,  382,  446],
             [ 382,  106,  126],
             [ 446,  126,  152]])
    

Rarer options for edge cases

  • If you have tensors (arrays of dimension greater than or equal to one), you can use numpy.tensordot with the optional argument axes=1:

    Use with AI>>> np.tensordot(a, b, axes=1)
    array([16,  6,  8])
    
  • Don't use numpy.vdot if you have a matrix of complex numbers, as the matrix will be flattened to a 1D array, then it will try to find the complex conjugate dot product between your flattened matrix and vector (which will fail due to a size mismatch n*m vs n).

🌐
Educative
educative.io › blog › numpy-matrix-multiplication
NumPy matrix multiplication: Get started in 5 minutes
April 30, 2026 - Use the @ operator for clean, readable code—it’s the most Pythonic option. Use np.matmul() when you want explicit matrix multiplication, especially for higher-dimensional arrays. Don’t choose between these methods based on performance alone—clarity matters more. Note: Benchmark results can vary depending on your hardware, NumPy version, and underlying libraries, so your exact numbers may differ.
🌐
RunxBuild
runxbuild.com › home › blog › matrix multiplication in python: use numpy and the @ operator, not nested loops
Matrix Multiplication in Python: Use NumPy and the @ Operator, Not Nested Loops
July 18, 2026 - To multiply matrices in Python, use NumPy and the @ operator: A @ B. That is real matrix multiplication - rows times columns, the dot-product rule from linear algebra. Do not write nested loops for it, and do not use *: in NumPy, * is element-wise ...
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › numpy-matrix-multiplication
NumPy Matrix Multiplication: Methods and Examples | DigitalOcean
August 4, 2022 - Perform matrix multiplication in NumPy using dot(), matmul(), and @ operator. Complete guide with examples for 2D, 3D arrays and performance tips.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.matmul.html
numpy.matmul — NumPy v2.1 Manual
If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed. ... Multiplication by scalars is not allowed, use * instead.
🌐
Geekflare
geekflare.com › development › 3 ways to multiply matrices in python
3 Ways to Multiply Matrices in Python
December 28, 2024 - In Python, @ is a binary operator used for matrix multiplication. It operates on two matrices, and in general, N-dimensional NumPy arrays, and returns the product matrix.
🌐
CodeSignal
codesignal.com › learn › courses › vector-and-matrix-operations-with-numpy › lessons › matrix-multiplication-with-numpy
Matrix Multiplication with NumPy | CodeSignal Learn
In this lesson, we explored matrix multiplication using NumPy, a critical skill for carrying out more complex matrix operations effortlessly. You’ve learned to define matrices, use the np.matmul function, and print the result matrix to verify its correctness.
🌐
CASRAI
casrai.org › guides › research tools & software › statistical software for research › matrix multiplication in numpy: matmul, @ and np.dot
Matrix Multiplication in NumPy: matmul, @ and np.dot
2 weeks ago - Multiply matrices in NumPy with np.matmul and @, tell them from element-wise multiply, and invert a matrix with np.linalg.inv.
🌐
CoddyKit
coddykit.com › home › courses › pandas & numpy academy › matrix multiplication with np.matmul and @
Matrix Multiplication with np.matmul and @ — Pandas & NumPy Academy | CoddyKit
1 week ago - NumPy has three multiplication functions that are easy to confuse. A * B is element-wise multiplication (Hadamard product). np.dot(A, B) behaves like np.matmul for 2-D arrays but has different semantics for higher dimensions. A @ B is strictly matrix multiplication and follows the mathematical definition precisely — prefer it over np.dot for matrix work.
🌐
Programiz
programiz.com › python-programming › numpy › methods › matmul
NumPy matmul() (With Examples)
November 20, 2023 - For example, For A = (M x N) and B = (N x K) when we multiply, C = A * B the resulting matrix is of size C = (M x K). import numpy as np # create two matrices matrix1 = np.array([[1, 2], [3, 4]]) matrix2 = np.array([[5, 6], [7, 8]]) # create an output array result = np.zeros((2, 2), dtype=int) # perform matrix multiplication using matmul() and store the output in the result array np.matmul(matrix1, matrix2, out=result) print(result)
🌐
Codecademy
codecademy.com › article › numpy-matrix-multiplication-a-beginners-guide
NumPy Dot Product and Matrix Multiplication: Complete Guide | Codecademy
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 ...
🌐
APXML
apxml.com › courses › linear-algebra-fundamentals-machine-learning › chapter-3-matrix-operations › numpy-matrix-operations-practice
NumPy Matrix Operations Tutorial
Note: A common mistake for beginners is to use the * operator for matrix multiplication. In NumPy, * performs element-wise multiplication. For two matrices to be multiplied element-wise, they must have the same shape. The @ operator was introduced in Python 3.5 specifically for matrix ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-multiply-two-matrices
Python Program to Multiply Two Matrices - GeeksforGeeks
NumPy handles matrix multiplication internally using optimized C-based operations. It takes the rows of matrix A and the columns of matrix B, performs vectorized dot-products, and produces the result efficiently without manual loops.
Published: November 27, 2025
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
Matrix operations with NumPy in Python | note.nkmk.me
January 21, 2024 - NumPy provides the np.ndarray class for general-purpose multidimensional arrays and the np.matrix class specialized for matrices (two-dimensional arrays). However, matrix operations are also possible with np.ndarray, and as of NumPy version 1.26, it is no longer recommended to use np.matrix. It is no longer recommended to use this class, even for linear algebra.
🌐
Medium
medium.com › @zackbunch › matrices-with-numpy-and-scipy-a260a65551c6
Python Matrices with NumPy and SciPy | Medium
January 11, 2022 - Two matrices are multiplied by computing the dot product of each row i of the first matrix and the corresponding column j of the second matrix. This value then becomes the result’s (i,j)th value.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › multiplication-two-matrices-single-line-using-numpy-python
Multiplication of two Matrices in Single line using Numpy in Python - GeeksforGeeks
August 6, 2024 - Numpy is a build in a package in python for array-processing and manipulation.For larger matrix operations we use numpy python package which is 1000 times faster than iterative one method.
🌐
Medium
medium.com › @tushargoel4613 › matrix-multiplication-using-numpy-15298f152f4d
Matrix Multiplication Using Numpy | by Tushargoel | Medium
January 31, 2023 - ... If both a and b are 2-D arrays, it is matrix multiplication, but matmul or a@b is preferred. ... If either a or b is 0-D (scalar), it is equivalent to multiplying, and using numpy.multiply(a,b) or a*b is preferred.