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]:
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!
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:
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=[c11​c21​​c12​c22​​]
For example, c11​ here is a dot product of the first row of A and the first column of B. The c12​ is a dot product of the first row of A and the second column of B, and so on.
More specifically:
c11​=a11​⋅b11​+a12​⋅b21​
c12​=a11​⋅b12​+a12​⋅b22​
c21​=a21​⋅b11​+a22​⋅b21​
c22​=a21​⋅b12​+a22​⋅b22​
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×m shape.
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=[14​25​36​]
Matrix B (3x2):
B=​7911​81012​​
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.
Matrix A (2x3):
A=[14​25​36​]
Matrix B (2x2):
B=[79​810​]
Matrix multiplication is not possible here because the number of columns in A (3) does not equal the number of rows in B (2).
Matrix A (2x3):
A=[14​25​36​]
Vector v (3x1):
v=​789​​
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.
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=[1012​1518​85​]
Price Matrix (B):
B=​203050​​
To find the total revenue generated by each salesperson, you'll perform matrix multiplication: