From the course: Machine Learning Foundations: Linear Algebra
Vector arithmetic
From the course: Machine Learning Foundations: Linear Algebra
Vector arithmetic
- [Instructor] Arithmetic operations in linear algebra are simple and intuitive, so let's jump right into it. We'll start with addition. To add two vectors, simply add each corresponding element. Addition is possible only for two vectors that have the same dimension. We cannot add a three-dimensional vector with a four-dimensional vector. For vector subtraction, the same rule applies. You can subtract the two vectors element-wise only if they have the same dimension. Let's head onto Jupyter Notebooks and try this out. We have already imported numpy as np. We'll define three vectors, a, b, and c, and then add vectors a and b, and after, try to add vectors a and c. As you can see, when we try to add a and c, we get an error because their dimensions don't match. Next, let's subtract a and b, and then try to subtract a and c. We also get an error in case of a and c. We can also multiply two vectors when they have the same length. Let's multiply vectors a and b by typing a*b. Similarly, we can divide vectors a and b using / operator by typing a/b. The next operation we'll explore is vector scalar multiplication. A scalar is just a number, so vector scalar multiplication is simple and straightforward. We're going to multiply each vector element by the scalar. It is important to memorize that there is a difference if we create a vector as a list or a vector as a ndarray. Let's see it in a basic example. We'll define scalar and name it just scalar, and then create a list as a vector and define it as list_a, and then convert that list into ndarray and define it as list_as_array. If we multiply our first vector with a scalar, we get the list printed two times. That is not an operation of scalar vector multiplication. In the case when a vector is stored as a ndarray, then we have element-wise multiplication. Let's type in scalar*list_as_array to get our vector scalar multiplication. Cool, so we have learned basic arithmetic operations with vectors. What is also special with these operations is that we can see geometrical interpretation of them. To add two vectors, you have to place the vector such that the tail of one vector is at the head of the other vector. The sum vector traverses from the tail of the first vector to the head of the second. To subtract vectors, we also have a straightforward procedure. You have to up the two vectors such that their tails are at the same coordinate. The difference vector is the line that goes from the head of the negative vector to the head of the positive vector. What about vector scalar multiplication? For vector scalar multiplication, we have four possible cases that depend on whether the scalar is greater than one, between one and zero, exactly zero, or negative. Only in the case when the scalar is negative, the direction of the vector will change.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.