Welcome! Today, we're diving into basic vector operations, which are foundational to your journey in machine learning. Understanding these operations will help you grasp more complex concepts later on. Our goals are to learn vector addition
and scalar multiplication
. We'll see why these operations are essential, learn how to implement them in Python, and understand their real-world applications.
Imagine you have two lists of numbers. You want to combine them by adding corresponding numbers together. That's vector addition
. Let's say we have vector and vector . Adding these vectors gives us another vector where each element is the sum of the corresponding elements. Note: The vectors must be of the same length to perform this addition.
So, = [1+4, 2+5, 3+6] = [5, 7, 9].
Consider two delivery trucks: Truck A and Truck B. Truck A delivers 1, 2, and 3 packages to three different locations respectively, while Truck B delivers 4, 5, and 6 packages to the same locations. By using vector addition, we can determine the total number of packages delivered to each location as follows: [1+4, 2+5, 3+6] = [5, 7, 9]. This means the total packages delivered to each location are 5, 7, and 9 respectively.
Here's how we can implement the vector addition in python:
Python1import numpy as np 2 3# Vector addition using numpy 4v1 = np.array([1, 2, 3]) 5v2 = np.array([4, 5, 6]) 6 7print("Vector Addition:", v1 + v2) # Vector Addition: [5 7 9]
Let's break down the code:
np.array([1, 2, 3])
and np.array([4, 5, 6])
create numpy arrays for v1
and v2
.v1 + v2
performs element-wise addition, resulting in [5, 7, 9]
.Now let's discuss scalar multiplication
. Imagine you have a list of numbers and you want to multiply each number by a constant value (scalar). For example, if and the scalar is 3, then multiplying each element by 3 gives us .
Imagine you are running a business and you have a list of product prices that are expected to increase by a fixed percentage (e.g., 20%). If the current prices are [10, 20, 30] dollars and you want to apply a 20% increase, you would multiply each price by 1.2. The new prices would be dollars.
Here's the Python code for the scalar multiplication:
Python1import numpy as np 2 3# Scalar multiplication using numpy 4v1 = np.array([1, 2, 3]) 5scalar = 2 6 7print("Scalar Multiplication:", scalar * v1) # Scalar Multiplication: [2 4 6]
Explanation:
2
is multiplied with each element in the numpy array v1
using the expression scalar * v1
.[2*1, 2*2, 2*3]
, or [2, 4, 6]
.Congratulations! In this lesson, we covered:
These operations are fundamental in machine learning and data science. By understanding and performing them in Python, you are building a solid foundation for more advanced topics.
Next, you'll get hands-on experience by practicing what you've learned. You'll perform vector addition
and scalar multiplication
using different examples. This practical approach will solidify your understanding and prepare you for more complex vector operations in future lessons. Happy coding!