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:

v1⋅v2=∑i=1nv1i⋅v2iv_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=quantity⋅price=∑i=1nquantityi⋅pricei\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
import numpy as np

# Vectors
v1 = np.array([1, 2, 3])
v2 = np.array([4, 5, 6])

# Calculate and print the dot product
print("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!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal