Dot Products with NumPy

Shen Ge
CodeX
Published in
4 min readMar 7, 2022

--

Photo by Scott Webb on Unsplash

Introduction

According to mathematicians, a dot product or scalar product is an operation that takes two equal-length sequence of numbers (aka vector) and returns a single number (aka scalar). The dot product of vector A with vector B is written symbolically as A • B.

Algebraically, it is the sum of the products of each entry of the sequence (aka elements of vectors). If you have not encountered dot products, this may sound confusing so let’s start with a simple example which you may encounter in daily life. Consider your grocery store bill as such:

Item    | Quantity | Unit Cost
Wine | 2 | 12.50
Orange | 12 | 0.50
Muffin | 3 | 1.75

If you can do your grocery bills, you’ll find the total cost of each item and then sum them all up. To find the total cost of each item, you will multiply the Quantity of the item multiplied by its Unit Cost. In other words, (2*12.50) + (12*0.5) + (3*1.75) = 36.25 . This is known as a weighted sum. At our level, we can also represent a weighted sum as a dot product if we represent Quantity and the Unit Cost as two vectors.

quantity = [2,12,3]
costs = [12.5,.5,1.75]
sum(q*c for q,c in zip(quantity,costs))

A dot product here is the same as adding up all the element-wise multiplied quantity and costs.

--

--

Shen Ge
CodeX

Engineer and console operator who helped America land back on the moon 2/22/2024. Enjoys code + poetry. Become a member: https://medium.com/@shenge86/membership