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
1by4to get4. - Multiply
2by5to get10. - Multiply
3by6to get18.
Add them: 4 + 10 + 18 = 32. That's the dot product! Mathematically:
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) by1.5($ per apple) to get3.0. - Multiply
3(bananas) by0.5($ per banana) to get1.5. - Multiply
1(cherry) by3.0($ per cherry) to get3.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:
Python Code for Dot Product
Here's how to calculate the dot product in Python using NumPy:
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!
