Menu
Coddy logo textTech

Dot Product

Lesson 17 of 18 in Coddy's Numpy Fundamentals course.

 

Dot product (also known as scalar product) is an operation from linear algebra that takes two equally sized vectors (vector is a sequence of numbers) and returns a single number

How it works

 Example 1

vector1 = [1, 2, 3]

vector2 = [4, 5, 6]

dot product of vector1 and vector2:

1 * 4 + 2 * 5 + 3 * 6 = 32

Example 2

When multiplying vectors, one must be vertical (shape: (X, 1)) and the other must be horizontal (shape: (1, X))

Vector1

5218

Vector2

6
8
2
1

The dot product is the sum of multiplying each corresponding index number of each vector.

Dot Product:

5*6 + 2*8 + 1*2 + 8*1 = 56

Dot product with Numpy

The syntax in Numpy is np.dot() 

Example 3

Dot product of two 1D Numpy arrays

ary1 = np.array([1, 4, 2])
ary2 = np.array([2, 5, 7])
np.dot(ary1, ary2) # 1*2 + 4*5 + 2*7
>>> 36

Example 4

Dot product of 1D Numpy array and scalar (a number)

ary1 = np.array([1, 4, 2])
num1 = 5
np.dot(ary1, num)
>>> [ 5, 20, 10]

It behaves as if we multiplied the array with scalar: ary1 * 5

If one of the arrays is a 2D array, the np.dot will perform a matrix multiplication which is covered in the next lesson.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create a function called calculate that receives two python lists, and performs:

  • res1 = Subtraction of the two lists
  • res2 = Multiplication of the two lists

and return the dot product of res1 and res2.

Try it yourself

import numpy as np
def calculate(lst1, lst2):

All lessons in Numpy Fundamentals