tutorials
Vector basics (beginner)
Overview
In this lesson, we'll learn about the components of vectors, as well as some basic vector operations.
Outcomes
After completing this lesson, you'll be able to …
- describe vectors in terms of magnitude and direction
- define scalar
- add, subtract, (element-wise) multiply, and (element-wise) divide vectors
- add, subtract, multiply, and divide vectors using a scalar
Vectors
Vectors can be thought of as n-dimensional points in space.
1D
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.
2D
x,y
0,1
12,7
3,3
5,6
3D
x,y,z
0,1,2
12,7,5
3,3,3
5,6,6
n-D
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.
Properties of vectors
All vectors have two components:
- length or magnitude
- direction
Magnitude
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.
Norm
To calculate the magnitude (aka 2-norm) of any vector, we can use the following form of the distance formula:
Unit vector
Mathematical symbols:
Mathematical symbols:
Direction
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 …
Operations
We can perform operations such as addition, subtraction, multiplication2, and division on a vector and a scalar or a vector and another vector.
Scalar and vector
What is a scalar?
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 and vector
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 …
Subtraction
Using NumPy
Invoke the IPython interpreter via docker:
[hahnpowell@gubuntu] $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])
Element-wise Multiplication
Using NumPy
Invoke the IPython interpreter via docker:
[hahnpowell@gubuntu] $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])
# when vectors have the same shape,
# the * operator performs element-wise
# multiplication
x * y
The result:
np.array([3, 8, 12])
Next steps
You now know some basic vector operations. Before moving on to learning about comparing vectors, let's practice …
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 ?
Footnotes
Footnotes
- You'll learn why in the next lesson ↩
- Multiplication can mean different things when talking about vectors. In this tutorial, we'll only focus on two forms: multiplication with a scalar and element-wise multiplication. ↩