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.
Using NumPy for Dot Product
The same thing can be done in NumPy that is easily extendible for more quantities and costs and offer better performance. The same calculation as before with NumPy would be
import numpy as np
quantity = np.array([2,12,3])
costs = np.array([12.5,.5,1.75])
np.sum(quantity*costs) # element-wise multiplication
The calculation is more simply done with NumPy’s np.dot
which can be implemented in three different ways.
quantity.dot(costs) # dot product way 1
np.dot(quantity,costs) # dot product way 2
quantity @ costs # dot product way 3
Multiplying Row and Column Arrays
Special care needs to be taken when dealing with multiplying a row and a column. Consider the 2D arrays called row_vec
and col_vec
:
row_vec =…