• 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 / Numpy dot() function with examples

Numpy dot() function with examples

December 29, 2022 by cmdlinetips

In this tutorial, we will learn how to use Numpy’s dot() function with multiple examples. Numpy’s dot() function is for fining the dot product of two arrays.

In linear algebra, dot product basically takes two equal-length arrays (vectors) of numbers and returns a single number. For example, if we have two vectors a and b

a = (x1,y1,z1) 

and

b = (x2,y2, z2)

Then the dot product of two vectors is result from

a.b = sum(x1*x2 +y1*y2 +z1*z2) 

A dot product is also called the inner product.

Numpy dot(): Dot product of 1-D arrays with Numpy dot() function

Let us see samples of using Numpy’s dot() function under multiple scenarios. First let us load Numpy.

import numpy as np

In Numpy, dot() function can take two array like objects as input. When the two arguments a and b are 1-D arrays, we get inner product of vectors.

a = np.arange(3)
a

array([0, 1, 2])
b = np.arange(3,6)
b

array([3, 4, 5])

We can see that the dot product of two 1-D arrays gives us the sum of element-wise multiplication.

np.dot(a,b)

14

Numpy dot(): Dot product of 2-D arrays is Matrix Multiplication

When the two input arrays are 2-D arrays, Numpy’s dot() function performs matrix multiplication if they are compatible.

Let us consider two 2×2 arrays and use dot() function to multiply the two 2-D arrays. And then verify the results with other matrix multiplication functions in Numpy.

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

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

array([[4, 5],
       [6, 7]])

Let us perform dot product using Numpy’s dot() function. As these input vectors are compatible, dot() function will do matrix multiplication and give us 2×2 array as result .

np.dot(a,b)

array([[ 6,  7],
       [26, 31]])

And we can verify the above result is same matrix multiplication using two matrix multiplication methods, Numpy’s matmul() function

np.matmul(a,b)

array([[ 6,  7],
       [26, 31]])

And Python’s @ symbol for matrix multiplication.

a @ b

array([[ 6,  7],
       [26, 31]])

We can see that all the three give the same results. Numpy strongly recommends to use matmul() or @ for 2D array product instead np.dot() function.

Numpy dot(): Dot product of 2-D array with a scalar

If one of the arguments to dot() function is scalar, i.e. a number, then Numpy’s dot() function perform element wise multiplication.

Here is an example of that.

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

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

And let us use np.dot() on the 2-D matrix and a scalar.

np.dot(a,b)

array([[ 0,  3,  6],
       [ 9, 12, 15],
       [18, 21, 24]])

We can verify the result is same as element wise multiplication by Numppy’s multiply() function

np.multiply(a, b)

array([[ 0,  3,  6],
       [ 9, 12, 15],
       [18, 21, 24]])

and Python * operator for element wise multiplication

a * b

array([[ 0,  3,  6],
       [ 9, 12, 15],
       [18, 21, 24]])

And all the results are same as expected. When either a or b is 0-D (scalar), using numpy.multiply(a, b) or a * b is preferred.

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 ThumbnailHow to do matrix multiplication with Numpy Default Thumbnail9 Basic Linear Algebra Operations with NumPy Default ThumbnailNumpy allclose() function with examples Default ThumbnailNumpy isclose() function with examples

Filed Under: Numpy Tips Tagged With: Numpy dot product, Numpy dot() fucntion, Numpy inner product

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