Python Practice › NumPy Programs › Program 38

Dot product of two vectors

EasyNumPy Programs · 38 of 100

Write a NumPy program to calculate the dot product of two vectors.

Advertisement
Input
[5, 3] and [7, 4]
Output
47 47

Approach

  1. np.dot(a, b) or a @ b.

Solution

Python
import numpy as npa, b = np.array([5, 3]), np.array([7, 4])print(np.dot(a, b), a @ b)
Output
47 47

Line by line

Line 3

5 × 7 + 3 × 4 = 47.

Note

The original sample answer (42) was not the dot product of these vectors.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs