In Python if we have two numpy arrays which are often referd 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 conditon 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:**

`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) ` |

*chevron_right*

*filter_none*

Output :vector multiplication [[3 6] [3 6]] Elementwise multiplication of two vector [[1 4] [1 4]]

**Examples 2:**

`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) ` |

*chevron_right*

*filter_none*

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]]

## Recommended Posts:

- Python - AND operation between Tuples
- Python | Tuple XOR operation
- Python | Operation to each element in list
- Python | Perform operation on each key dictionary
- Commit & RollBack Operation in Python
- Python | Union Operation in two Strings
- Python - Alternate Elements operation on Tuple
- Python - Binary operation on specific keys in Dictionary List
- Python set operations (union, intersection, difference and symmetric difference)
- Join operation Vs Nested query in DBMS
- Conditional operation on Pandas DataFrame columns
- Difference between C and Python
- Difference between Python and C++
- Difference between 'and' and '&' in Python
- Difference Between x = x + y and x += y in Python
- Python Set | difference()
- Difference between Python and C#
- Difference between == and is operator in Python
- Difference between dir() and vars() in Python
- Difference Between '+' and 'append' in Python

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.