Vector Operations
In this lab you will use Python and NumPy functions to perform main vector operations - scalar multiplication, sum of vectors and their dot product. You will also investigate the speed of calculations using loop and vectorized forms of these main linear algebra operations
Packages
Load the NumPy
package to access its functions.
Scalar Multiplication and Sum of Vectors
Visualization of a Vector
You already have seen in the videos and labs, that vectors can be visualized as arrows, and it is easy to do it for a , e.g.
The following code will show the visualization.
The vector is defined by its norm (length, magnitude) and direction, not its actual position. But for clarity and convenience vectors are often plotted starting in the origin (in it is a point ) .
Scalar Multiplication
Scalar multiplication of a vector by a scalar is a vector (element by element multiplication). If , then is a vector pointing in the same direction as and it is times as long as . If , then is a zero vector. If , vector will be pointing in the opposite direction. In Python you can perform this operation with a *
operator. Check out the example below:
Sum of Vectors
Sum of vectors (vector addition) can be performed by adding the corresponding components of the vectors: if and
, then . The so-called parallelogram law gives the rule for vector addition. For two vectors and represented by the adjacent sides (both in magnitude and direction) of a parallelogram drawn from a point, the vector sum is is represented by the diagonal of the parallelogram drawn from the same point:
In Python you can either use +
operator or NumPy
function np.add()
. In the following code you can uncomment the line to check that the result will be the same:
Norm of a Vector
The norm of a vector is denoted as . It is a nonnegative number that describes the extent of the vector in space (its length). The norm of a vector can be found using NumPy
function np.linalg.norm()
:
Dot Product
Algebraic Definition of the Dot Product
The dot product (or scalar product) is an algebraic operation that takes two vectors and
and returns a single scalar. The dot product can be represented with a dot operator and defined as:
Dot Product using Python
The simplest way to calculate dot product in Python is to take the sum of element by element multiplications. You can define the vectors and by listing their coordinates:
Next, let’s define a function dot(x,y)
for the dot product calculation:
For the sake of simplicity, let’s assume that the vectors passed to the above function are always of the same size, so that you don’t need to perform additional checks.
Now everything is ready to perform the dot product calculation calling the function dot(x,y)
:
Dot product is very a commonly used operator, so NumPy
linear algebra package provides quick way to calculate it using function np.dot()
:
Note that you did not have to define vectors and as NumPy
arrays, the function worked even with the lists. But there are alternative functions in Python, such as explicit operator @
for the dot product, which can be applied only to the NumPy
arrays. You can run the following cell to check that.
As both np.dot()
and @
operators are commonly used, it is recommended to define vectors as NumPy
arrays to avoid errors. Let's redefine vectors and as NumPy
arrays to be safe:
Speed of Calculations in Vectorized Form
Dot product operations in Machine Learning applications are applied to the large vectors with hundreds or thousands of coordinates (called high dimensional vectors). Training models based on large datasets often takes hours and days even on powerful machines. Speed of calculations is crucial for the training and deployment of your models.
It is important to understand the difference in the speed of calculations using vectorized and the loop forms of the vectors and functions. In the loop form operations are performed one by one, while in the vectorized form they can be performed in parallel. In the section above you defined loop version of the dot product calculation (function dot()
), while np.dot()
and @
are the functions representing vectorized form.
Let's perform a simple experiment to compare their speed. Define new vectors and of the same size :
Use time.time()
function to evaluate amount of time (in seconds) required to calculate dot product using the function dot(x,y)
which you defined above:
Now compare it with the speed of the vectorized versions:
You can see that vectorization is extremely beneficial in terms of the speed of calculations!
Geometric Definition of the Dot Product
In Euclidean space, a Euclidean vector has both magnitude and direction. The dot product of two vectors and is defined by:
where is the angle between the two vectors:
This provides an easy way to test the orthogonality between vectors. If and are orthogonal (the angle between vectors is ), then since , it implies that the dot product of any two orthogonal vectors must be . Let's test it, taking two vectors and we know are orthogonal:
Application of the Dot Product: Vector Similarity
Geometric definition of a dot product is used in one of the applications - to evaluate vector similarity. In Natural Language Processing (NLP) words or phrases from vocabulary are mapped to a corresponding vector of real numbers. Similarity between two vectors can be defined as a cosine of the angle between them. When they point in the same direction, their similarity is 1 and it decreases with the increase of the angle.
Then equation can be rearranged to evaluate cosine of the angle between vectors:
Zero value corresponds to the zero similarity between vectors (and words corresponding to those vectors). Largest value is when vectors point in the same direction, lowest value is when vectors point in the opposite directions.
This example of vector similarity is given to link the material with the Machine Learning applications. There will be no actual implementation of it in this Course. Some examples of implementation can be found in the Natual Language Processing Specialization.
Conclusion
Well done, you have finished this lab!