Lesson 4
Dot Product and Matrix Multiplication
Lesson Introduction

Welcome to our lesson on "Dot Product and Matrix Multiplication"! These topics are key in machine learning as they help computers process data efficiently. By the end of this lesson, you'll know what dot products and matrix multiplication are, how to calculate them, and how to implement these operations in Python.

These concepts are used in tasks like recognizing faces in photos, predicting the weather, and much more. Let's dive in and see how they work!

Dot Product

First, let's talk about the dot product. It's a way to combine two vectors into a single number. Think of playing a game where you multiply pairs of numbers and add the results. With numbers [1, 2, 3] and [4, 5, 6]:

  • Multiply 1 by 4 to get 4.
  • Multiply 2 by 5 to get 10.
  • Multiply 3 by 6 to get 18.

Add them: 4 + 10 + 18 = 32. That's the dot product! Mathematically:

v1v2=i=1nv1iv2iv_1 \cdot v_2 = \sum_{i=1}^{n} v_{1i} \cdot v_{2i}

Real-Life Example of Dot Product

Consider the scenario of calculating the total price of groceries.

  • Quantities of items: [2, 3, 1] (for instance, 2 apples, 3 bananas, and 1 cherry)
  • Prices per item: [1.5, 0.5, 3.0]

To find the total cost, multiply the quantities by the respective prices.

  • Multiply 2 (apples) by 1.5 ($ per apple) to get 3.0.
  • Multiply 3 (bananas) by 0.5 ($ per banana) to get 1.5.
  • Multiply 1 (cherry) by 3.0 ($ per cherry) to get 3.0.

Add them: 3.0 + 1.5 + 3.0 = 7.5. So, the total cost of groceries is $7.5.

Mathematically, this can be expressed as the dot product of the quantity and price vectors:

Total Cost=quantityprice=i=1nquantityipricei\text{Total Cost} = \text{quantity} \cdot \text{price} = \sum_{i=1}^{n} \text{quantity}_{i} \cdot \text{price}_{i}

Python Code for Dot Product

Here's how to calculate the dot product in Python using NumPy:

Python
1import numpy as np 2 3# Vectors 4v1 = np.array([1, 2, 3]) 5v2 = np.array([4, 5, 6]) 6 7# Calculate and print the dot product 8print("Dot Product:", np.dot(v1, v2)) # Dot Product: 32

This code uses NumPy's dot function, which directly calculates the dot product of vectors v1 and v2.

You've successfully calculated the dot product!

Matrix Multiplication

Next, let's talk about matrix multiplication. This helps combine two matrices into a new matrix. Imagine matrices as grids of numbers. Multiplying matrices combines rows and columns in a specific way. Given two matrices A and B:

A=[a11a12a21a22]\textbf{A} = \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix} B=[b11b12b21b22]\textbf{B} = \begin{bmatrix} b_{11} & b_{12} \\ b_{21} & b_{22} \end{bmatrix}

To find element c_{ij} in the resulting matrix C, take the dot product of the i-th row of A and the j-th column of B:

C=[c11c12c21c22]\textbf{C} = \begin{bmatrix} c_{11} & c_{12} \\ c_{21} & c_{22} \end{bmatrix}

For example, c11c_{11} here is a dot product of the first row of A \textbf{A} and the first column of B \textbf{B}. The c12c_{12} is a dot product of the first row of A \textbf{A} and the second column of B \textbf{B}, and so on.

More specifically:

c11=a11b11+a12b21c_{11} = a_{11} \cdot b_{11} + a_{12} \cdot b_{21}

c12=a11b12+a12b22c_{12} = a_{11} \cdot b_{12} + a_{12} \cdot b_{22}

c21=a21b11+a22b21c_{21} = a_{21} \cdot b_{11} + a_{22} \cdot b_{21}

c22=a21b12+a22b22c_{22} = a_{21} \cdot b_{12} + a_{22} \cdot b_{22}

The resulting matrix C will have as many rows as A and as many columns as B. Think of it as "we multiply n rows by m columns, so the resulting matrix is of n×mn \times m shape.

When Matrices Can Be Multiplied: Example 1

Matrix multiplication is only possible when the number of columns in matrix A is equal to the number of rows in matrix B.

Let's look at some examples:

Matrix A (2x3):

A=[123456]\textbf{A} = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}

Matrix B (3x2):

B=[789101112]\textbf{B} = \begin{bmatrix} 7 & 8 \\ 9 & 10 \\ 11 & 12 \end{bmatrix}

Matrix multiplication is possible here because the number of columns in A (3) is equal to the number of rows in B (3). The resulting matrix, C, will have dimensions 2x2, as we multiply 2 rows by 2 columns.

When Matrices Can Be Multiplied: Example 2

Matrix A (2x3):

A=[123456]\textbf{A} = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}

Matrix B (2x2):

B=[78910]\textbf{B} = \begin{bmatrix} 7 & 8 \\ 9 & 10 \end{bmatrix}

Matrix multiplication is not possible here because the number of columns in A (3) does not equal the number of rows in B (2).

When Matrices Can Be Multiplied: Example 3

Matrix A (2x3):

A=[123456]\textbf{A} = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}

Vector v (3x1):

v=[789]\textbf{v} = \begin{bmatrix} 7 \\ 8 \\ 9 \end{bmatrix}

Matrix multiplication is possible here because the number of columns in A (3) is equal to the number of rows in vector v (3). The resulting vector will have the dimensions 2x1, as we multiply 2 rows by 1 column.

Practical Example

Consider a scenario where you're tracking the sales of two salespersons across three different products over two months. You have the sales data and the price per product:

Sales Matrix (A):

A=[1015812185]\textbf{A} = \begin{bmatrix} 10 & 15 & 8 \\ 12 & 18 & 5 \end{bmatrix}

Price Matrix (B):

B=[203050]\textbf{B} = \begin{bmatrix} 20 \\ 30 \\ 50 \end{bmatrix}

To find the total revenue generated by each salesperson, you'll perform matrix multiplication:

c11=(1020)+(1530)+(850)=200+450+400=1050c_{11} = (10 \cdot 20) + (15 \cdot 30) + (8 \cdot 50) = 200 + 450 + 400 = 1050

c21=(1220)+(1830)+(550)=240+540+250=1030c_{21} = (12 \cdot 20) + (18 \cdot 30) + (5 \cdot 50) = 240 + 540 + 250 = 1030

So, the resulting revenue matrix C is:

C=[10501030]\textbf{C} = \begin{bmatrix} 1050 \\ 1030 \end{bmatrix}
Python Code for Matrix Multiplication

Here's how to do it in Python using NumPy:

Python
1import numpy as np 2 3# Matrices 4m1 = np.array([[1, 2], [3, 4]]) 5m2 = np.array([[5, 6], [7, 8]]) 6 7# Calculate and print result 8print("Matrix Multiplication:\n", np.dot(m1, m2)) 9# Matrix Multiplication: 10# [[19 22] 11# [43 50]]

This code uses NumPy's dot function to perform matrix multiplication on matrices m1 and m2, resulting in matrix C.

Lesson Summary

Congratulations! You've learned about dot products and matrix multiplication. We covered their importance, how they work, and how to implement them in Python using NumPy. These operations are foundational in machine learning for data processing.

Next, it's time to practice. In the practice session, you'll use what you've learned to solve tasks and reinforce your understanding. Keep practicing, and soon these concepts will become second nature to you!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.