The Wayback Machine - https://web.archive.org/web/20240621190154/https://www.geeksforgeeks.org/difference-between-numpy-dot-and-operation-in-python/
Open In App

Difference between NumPy.dot() and ‘*’ operation in Python

Last Updated : 09 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In Python if we have two numpy arrays which are often referred as a vector. The ‘*’ operator and numpy.dot() work differently on them. It’s important to know especially when you are dealing with data science or competitive programming problem.
 

Working of ‘*’ operator

‘*’ operation caries out element-wise multiplication on array elements. The element at a[i][j] is multiplied with b[i][j] .This happens for all elements of array.
Example: 
 

Let the two 2D array are v1 and v2:-
v1 = [[1, 2], [3, 4]]
v2 = [[1, 2], [3, 4]]

Output:
[[1, 4]
[9, 16]]
From below picture it would be clear.

 

 

Working of numpy.dot()

It carries of normal matrix multiplication . Where the condition of number of columns of first array should be equal to number of rows of second array is checked than only numpy.dot() function take place else it shows an error. 
Example: 
 

Let the two 2D array are v1 and v2:-
v1=[[1, 2], [3, 4]]
v2=[[1, 2], [3, 4]]
Than numpy.dot(v1, v2)  gives output of :-
[[ 7 10]
 [15 22]]

Examples 1: 
 

Python3




import numpy as np
 
 
# vector v1 of dimension (2, 2)
v1 = np.array([[1, 2], [1, 2]])
 
# vector v2 of dimension (2, 2)
v2 = np.array([[1, 2], [1, 2]])
 
print("vector multiplication")
print(np.dot(v1, v2))
 
print("\nElementwise multiplication of two vector")
print(v1 * v2)


Output :
vector multiplication
[[3 6]
 [3 6]]

Elementwise multiplication of two vector
[[1 4]
 [1 4]]

Examples 2: 
 

Python3




import numpy as np
 
 
v1 = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
 
v2 = np.array([[[1, 2, 3], [1, 2, 3], [1, 2, 3]]])
 
print("vector multiplication")
print(np.dot(v1, v2))
 
print("\nElementwise multiplication of two vector")
print(v1 * v2)


Output :
vector multiplication
[[ 6 12 18]
 [ 6 12 18]
 [ 6 12 18]]

Elementwise multiplication of two vector
[[1 4 9]
 [1 4 9]
 [1 4 9]]


Similar Reads

Python | Numpy matrix.dot()
With the help of Numpy matrix.dot() method, we are able to find a product of two given matrix and gives output as new dimensional matrix. Syntax : matrix.dot() Return : Return product of two matrix Example #1 : In this example we can see that with the help of matrix.dot() method we are able to find the product of two given matrix. # import the impo
1 min read
Numpy recarray.dot() function | Python
In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record arrays allow the fields to be accessed as members of th
3 min read
Numpy MaskedArray.dot() function | Python
numpy.MaskedArray.dot() function is used to calculate the dot product of two mask arrays. Syntax : numpy.ma.dot(arr1, arr2, strict=False) Parameters: arr1, arr2:[ ndarray] Inputs arrays. strict : [bool, optional] Whether masked data are propagated (True) or set to 0 (False) for the computation. Default is False. Return : [ ndarray] The dot product
3 min read
Numpy ndarray.dot() function | Python
numpy.ndarray.dot() function return the dot product of two arrays. Syntax : numpy.ndarray.dot(arr, out=None) Parameters : arr : [array_like] Input array. out : [ndarray, optional] Output argument. Return : [ndarray] The dot product of two arrays. Code #1 : # Python program explaining # numpy.ndarray.dot() function # importing numpy as geek import n
1 min read
Python Program to Get dot product of multidimensional Vectors using NumPy
Given two multidimensional Vectors, the task is to write a Python program to get the dot product of two multidimensional Vectors using NumPy. Example: Lets take 2 vectors a = [2,5,3] and b = [6,3,1] Dot Product(ab) = (a[0] * b[0])+ (a[1] * b[1]) + (a[2] * b[2]) = (2*6)+ (5*3) + (3*1) = 30 Dot Product is the sum of the product of elements at each po
4 min read
numpy.dot() in Python
numpy.dot(vector_a, vector_b, out = None) returns the dot product of vectors a and b. It can handle 2D arrays but considers them as matrix and will perform matrix multiplication. For N dimensions it is a sum-product over the last axis of a and the second-to-last of b : dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m]) Parameters vector_a : [array_like]
2 min read
Difference between Dot matrix printer and Laser printer
Prerequisite - Impact and Non-Impact Printers 1. Laser Printer : Laser Printer is a type of Non-Impact Printer which prints characters or images without striking paper. It is Introduced by Digital Equipment Corporation in1970. It provides a high speed and high quality printing than Dot Matrix printer. 2. Dot Matrix Printer : Dot Matrix Printer is a
2 min read
White and black dot detection using OpenCV | Python
Image processing using Python is one of the hottest topics in today's world. But image processing is a bit complex and beginners get bored in their first approach. So in this article, we have a very basic image processing python program to count black dots in white surface and white dots in the black surface using OpenCV functions (cv2.imread, cv2.
4 min read
Draw Dot Patterns Using Turtle in Python
Prerequisite: Turtle Programming Basics Turtle is an inbuilt module in Python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle (pen). To move turtle, there are some functions i.e forward(), backward(), etc. 1) Draw Dot Square Following steps are used : Import turtleMake tu
2 min read
How to calculate dot product of two vectors in Python?
In mathematics, the dot product or also known as the scalar product is an algebraic operation that takes two equal-length sequences of numbers and returns a single number. Let us given two vectors A and B, and we have to find the dot product of two vectors. Given that, [Tex]A = a_1i + a_2j + a_3k [/Tex] and, [Tex]B = b_1i + b_2j + b_3k [/Tex] Where
3 min read
three90RightbarBannerImg