In this tutorial, I’ll show you how to use the Numpy dot function (np.dot), to compute dot products in Numpy. Show
I’ll explain exactly what the function does, how the syntax works, and I’ll show you clear examples of how to use np.dot. Table of Contents:
If you need something specific, you can click on any of the above links, and it will take you to the appropriate section of the tutorial. Having said that, if you’re new to Numpy, or need a quick refresher about mathematical dot products, you should probably read the whole tutorial. A Quick Introduction to Numpy DotFirst of all, let’s start with the basics. What does Numpy dot do? At a high level, Numpy dot computes the dot product of two Numpy arrays. If you’re a little new to Numpy though, or if you don’t completely understand dot products, that might not entirely make sense. So let’s quickly review some basics about Numpy and about dot products. Numpy dot operates on Numpy arraysLet’s start with Numpy. As you’re probably aware, Numpy is an add-on package for the Python programming language. We mostly use Numpy for data manipulation and scientific computing, but we use Numpy on specific types of data in specific data structures. Numpy is a Package for Working with Numeric Data in PythonIn particular, Numpy creates and operates on Numpy arrays. A Numpy array is a data structure that stores numerical data in a row and column structure. So for example, a 2-dimensional Numpy array looks something like this: Numpy arrays can come in a variety of shapes and sizes. For example, we can build 1-dimensional arrays, 2-dimensional arrays, and n-dimensional arrays. Additionally, we can create Numpy arrays where the Numbers have a variety of different properties. For example, we can create arrays that contain normally distributed numbers, numbers drawn from a uniform distribution, numbers that are all the same value, just to name a few. So Numpy has a variety of functions for creating Numpy arrays with different types of properties. Numpy has Functions for Performing Calculations with Numpy ArraysIn addition to having functions for creating Numpy arrays, the Numpy package also has functions for operating on and computing with Numpy arrays. So Numpy has many functions for performing mathematical computations, like computing the sum of an array, computing exponentials of array values, and many more. Additionally, Numpy has functions for doing more advanced operations, like operations from linear algebra. np.dot computes dot products in NumpySo what does the Numpy dot function do? The simple explanation is that To paraphrase the entry on Wikipedia, the dot product is an operation that takes two equal-length sequences of numbers and returns a single number. Having said that, the Numpy dot function works a little differently depending on the exact inputs. There are three broad cases that we’ll consider with
Let’s take a look at how Numpy dot operates for these different cases. If the input arrays are both 1-dimensional arrays, np.dot computes the vector dot productLet’s say we have two Numpy arrays, and , and each array has 3 values.Given two 1-dimensional arrays, The dot product can be computed as follows: Notice what’s going on here. These arrays have the same length, and each array has 3 values. When we compute the dot product, we multiply the first value of by the first value of . We multiply the second value of by the second value of . And we multiply the third value of by the third value of . Then, we take the resulting values, and sum them up.The output is a single scalar value … in this example, .In mathematical terms, we can generalize the example above. If we have two vectors and , and each vector has elements, then the dot product is given by the equation:(1) Essentially, when we take the dot product of two Numpy arrays, we’re computing the sum of the pairwise products of the two arrays. If one of the inputs is a scalar, np.dot performs scalar multiplicationThe second case is when one input is a scalar value , and one input is a Numpy array, which here we’ll call .If we use Numpy dot on these inputs with the code So when we use Numpy dot with one scalar and one Numpy array, it multiples every value of the array by the scalar and outputs a new Numpy array. If both inputs are 2D arrays, np.dot performs matrix multiplicationThe final case that we’ll cover is when both of the input arrays are 2-dimensional arrays. In this case, with two 2D arrays, the A full explanation of matrix multiplication is beyond the scope of this tutorial, but let’s look at a quick example. Let’s say that you have two 2D arrays, and .Next, let’s multiply those arrays together using matrix multiplication. During matrix multiplication, we multiply the values of the rows of with the values of the columns of and sum them up in the following way:And here’s the final computed output: Notice that the output array, , has the same number of rows as and the same number of columns as .Additionally, each value in the output array is calculated by summing the product of the ith row of and the jth row of .More generally, to compute the output array , each value of the output array is defined as:(2) If you’re not familiar with linear algebra generally, and matrix algebra specifically, I realize that these equations can be a little confusing. Maybe even a little intimidating. That said, if you want to learn more about these operations, I recommend that you read the book Linear Algebra and its Applications, by David Lay and colleagues. It’s a very approachable book about linear algebra that will help you understand some of the operations we’re performing with Numpy dot. Ok. So now that we’ve looked at what Numpy dot does, let’s take a closer look at the syntax. The Syntax of Numpy DotHere, I’ll explain the syntax of the Numpy dot function. A quick noteOne thing before we look at the syntax. In order to use Numpy functions, you need to import Numpy first. You can do that with the following code: import numpy as np This is important, because how you import Numpy will affect the syntax. It’s the common convention among Python data scientists to import Numpy with the alias ‘ np.dot syntaxThe syntax of Assuming that you’ve imported Numpy with the alias Then, inside the parenthesis, there are a few parameters that allow you to provide inputs to the function. Let’s take a look at those inputs. The parameters of np.dotThere are 2 core parameters for the
Let’s quickly take a look at those, one by one. a (required)The Technically, the argument to this parameter can be a scalar value, or any “array-like” object. Because it allows array-like objects, this can be a proper Numpy array, or it can be a Python list, a tuple, etc. A scalar value like an Keep in mind that you must provide an argument to this parameter. b (required)The Similar to the Keep in mind that you must provide an argument to this parameter. out (optional)Note that The output of np.dotThe output of There are a few cases:
So as you can see, the output really depends on how you use the function. With that in mind, let’s take a look at some examples so you can see how it works, and see the
different types of outputs that Examples: How to Compute Dot Products in NumpyOk. Let’s work through some step-by-step examples. If you need something specific, you can click on any of the following links, and it will take you to the approprate example. Examples:
Run this code firstBefore you run any of the examples, you’ll need to import Numpy first. You can do that with the following code: import numpy as np Once you’ve done that, you should be ready to go. EXAMPLE 1: Multiply two numbersOk. Let’s start with a simple example. Here, we’re going to use two numbers (i.e., scalar values) as the inputs to np.dot(2,3) OUT: 6Explanation This is maybe a little unexpected, but very simple. Here, we called When we call Obviously EXAMPLE 2: Multiply a Number and an ArrayNext, let’s provide an array and scalar as inputs. Here, we’ll actually provide a scalar (an integer) and a Python list. Instead of a Python list, we could also provide a Numpy array, but I’ve used a Python list instead because it makes the operation a little easier to understand here. Let’s take a look. np.dot(2,[5,6]) OUT: array([10, 12])Explanation Again, this is very simple. The first argument to the function is the scalar value 2. The second argument to the function is the Python list When we provide a scalar as one input and a list (or Numpy array) as the other input, Mathematically, we’d consider this to be scalar multiplication of a vector or matrix. EXAMPLE 3: Compute the Dot Product of Two 1D ArraysNext, let’s input two 1-dimensional lists. Here, we’ll use two Python lists, but we could also use 1D Numpy arrays. I’m using Python lists because it makes the operation a little easier to understand at a glance. Let’s take a look. np.dot([3,4,5],[7,8,9]) OUT: 98Explanation What’s going on here? Here, the These inputs are 1-dimensional Python lists. And as I said earlier, we could also use 1D Numpy arrays. Mathematically, 1D lists and 1D Numpy arrays are like vectors. When we’re working with vectors and take the dot product, the dot product is computed by equation 1 that we saw earlier.
So when Numpy dot has two 1D lists or arrays as inputs … … it takes the product of the pairwise elements, and then adds them together: And the output is a scalar value. In this case, the result is 98. EXAMPLE 4: Perform Matrix Multiplication on Two 2D ArraysFinally, let’s look at what happens when we use Numpy dot on two 2-dimensional arrays. Create ArraysFirst, let’s just create two 2-dimenstional Numpy arrays. To do this, we’ll use the A_array_2d = np.arange(start = 3, stop = 9).reshape((2,3)) B_array_2d = np.arange(start = 10, stop = 16).reshape((3,2)) And let’s print them out, just so you can see the contents. print(A_array_2d) OUT: [[3 4 5] [6 7 8]] print(B_array_2d) OUT: [[10 11] [12 13] [14 15]] Notice that both of these Numpy arrays are 2-dimensional. Having said that, the number of rows in Ok. Now let’s use the Numpy dot function on these two arrays. np.dot(A_array_2d, B_array_2d) OUT: array([[148, 160], [256, 277]])Explanation So what happened here? In this case, we used two 2-dimensional Numpy arrays as the inputs. When we use 2D arrays as inputs, When it does this, it
So under the hood, this is what Numpy is doing when we run the code Look carefully. In the above image, you can see that the computation multiplies the values of the rows of A by the values of the columns of B, sums them up, and puts them into the final output array, with the following values: This operation is known as the matrix product. So again: when we use Numpy dot on two 2-dimensional arrays, I understand that this might be slightly confusing if you don’t have a lot of experience with linear algebra. If that’s the case, I recommend that you do some reading about linear algebra generally, and matrix multiplication in particular. Frequently asked questions about Numpy DotNow that we’ve looked at some examples, let’s look at a few common questions about the Frequently asked questions:
Question 1: What’s the difference between np.dot() and np.matmul() ?Numpy dot and Numpy matmul are similar, but they behave differently for some types of inputs. The two big differences are for:
Let’s look at these one at a time. Multiplication by scalarsThe first difference between As we saw in example 2, when we use np.dot(2,[5,6]) OUT: array([10, 12]) However, if you try to do this with np.matmul(2,[5,6]) OUT: ValueError: matmul: Input operand 0 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)Multiplication of high-dimensional arrays The second area where According to the doccumentation … When you use
But when you use
Ultimately, Question 2: What’s the difference between np.dot() and ndarray.dot() ?
The difference is that So they effectively do the same thing, but you call them in a slightly different way. Let’s say we have two 2-dimensional arrays. A_array_2d = np.arange(start = 3, stop = 9).reshape((2,3)) B_array_2d = np.arange(start = 10, stop = 16).reshape((3,2)) We can call the np.dot(A_array_2d, B_array_2d) But we use so-called “dot syntax” to call the
A_array_2d.dot(B_array_2d) The output is the same, but the syntax is slightly different. If you’re still confused about this, make sure to read more about the difference between Python functions and Python methods. Leave your other questions in the comments belowDo you still have questions about the Numpy dot function? If so, leave your questions in the comments section below. Join our course to learn more about NumpyIn this
tutorial, I’ve explained how to use the This should help you understand If you’re serious about mastering Numpy, and serious about data science in Python, you should consider joining our premium course called Numpy Mastery. Numpy Mastery will teach you everything you need to know about Numpy, including:
Moreover, this course will show you a practice system that will help you master the syntax within a few weeks. We’ll show you a practice system that will enable you to memorize all of the Numpy syntax you learn. If you have trouble remembering Numpy syntax, this is the course you’ve been looking for. Find out more here: Learn More About Numpy Mastery What is the dot product of two arrays?Dot product of two arrays. Specifically, If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation). If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.
Can you multiply two arrays in Python?multiply() function is used when we want to compute the multiplication of two array. It returns the product of arr1 and arr2, element-wise.
Is there a dot product function in Python?Python provides a very efficient method to calculate the dot product of two vectors. By using numpy. dot() method which is available in the NumPy module one can do so.
How do you make a dot product in Python?In Python, one way to calulate the dot product would be taking the sum of a list comprehension performing element-wise multiplication. Alternatively, we can use the np. dot() function. Keeping to the convention of having x and y as column vectors, the dot product is equal to the matrix multiplication xTy x T y .
|