In this lesson, we'll learn about the components of vectors, as well as some basic vector operations.
After completing this lesson, you'll be able to ...
Vectors can be thought of as n-dimensional points in space.
If we decided to represent words using a single feature (ex. a sentiment score), we could represent them as 1D vectors:
x
0.5
0.75
0.9
A single dimension is pretty limiting, though.
Even if we go beyond a single dimension, we can continue to think of vectors as points in n-dimensional space.
x,y
0,1
12,7
3,3
5,6
x,y,z
0,1,2
12,7,5
3,3,3
5,6,6
It's quite common to use 100s or 1000s of features (dimensions) to represent words and documents.
Beyond 3 dimensions, though, vectors become less straighforward to visualize. Some options include using properties such as color and shape to represent additional dimensions.
All vectors have two components:
The magnitude of a vector is its distance from the origin (i.e., the vector comprised of all zeros). Though it can be zero in a special case, length is never negative.
For a vector , you'll often see used to refer to its magnitude. If we're being more precise 1, we'll use to refer to the magnitude of .
How do we calculate length? One familiar way of thinking about this is to consider a 2D vector and apply the distance formula to it and the origin. For this example, we'll consider the 2D vector :
A 3-dimensional point (vector) will work the same way as will any -dimensional vector.
To calculate the magnitude (aka 2-norm) of any vector, we can use the following form of the distance formula:
A vector with a magnitude of 1 is called a unit vector.
is a way of specifying the cardinality or size of the vector . For instance, if , then .
Let's look at an example using Python.
docker run -it "parsertongue/python:latest" ipython
x = [1, 2, 3, 4, 5]
cardinality = lambda vec: len(vec)
assert cardinality(x) == 5
exit
The ("sigma") symbol is just a way of denoting a summation. For instance, simply means to sum all number in a vector from the 0 index until the last element of the vector.
Let's look at an example using Python.
docker run -it "parsertongue/python:latest" ipython
x = [1, 2, 3, 4, 5]
sigma = lambda vec: sum(vec)
assert sigma(x) == 15
exit
The other component of a vector is its direction.
To calculate direction, we need to normalize a vector by . We do this by dividing each element of by . As an example, consider again the 2D vector :
This resulting vector is known as a unit vector. It contains information solely about the direction of the vector.
We can can simplify the formula for normalizing a vector as ...
We can perform operations such as addition, subtraction, multiplication, and division on a vector and a scalar or a vector and another vector.
A scalar is a number that scales a vector.
Say we have a two dimensional vector, :
We can visualize it as a single point:
If multiply this vector by the scalar 2, we get the following result:
Vector-vector operations rely on the two vectors having the same shape (dimensionality). These operations are performed against pairs of elements in the two vectors that share the same position (index). Let's look at a couple of examples ...
Invoke the IPython interpreter via docker
:
docker run -it "parsertongue/python:latest" ipython
# run using the following command:
# docker run -it "parsertongue/python:latest" ipython
import numpy as np
x = np.array([1, 2, 4])
y = np.array([3, 4, 3])
x - y
The result:
np.array([-2, -2, 1])
Invoke the IPython interpreter via docker
:
docker run -it "parsertongue/python:latest" ipython
# run using the following command:
# docker run -it "parsertongue/python:latest" ipython
import numpy as np
x = np.array([1, 2, 4])
y = np.array([3, 4, 3])
x * y
The result:
np.array([3, 8, 12])
You now know some basic vector operations. Before moving on to learning about comparing vectors, let's practice ...
Given , what is its magnitude?
Given , what vector has the same direction, but 3 times the magnitude of ?
Given , what vector has the same magnitude, but the opposite direction to ?