Path: blob/master/Natural Language Processing with Classification and Vector Spaces/Week 4 - Machine Translation and Document Search/NLP_C1_W4_lecture_nb_01.ipynb
14375 views
Vector manipulation in Python
In this lab, you will have the opportunity to practice once again with the NumPy library. This time, we will explore some advanced operations with arrays and matrices.
At the end of the previous module, we used PCA to transform a set of many variables into a set of only two uncorrelated variables. This process was made through a transformation of the data called rotation.
In this week's assignment, you will need to find a transformation matrix from English to French vector space embeddings. Such a transformation matrix is nothing else but a matrix that rotates and scales vector spaces.
In this notebook, we will explain in detail the rotation transformation.
Transforming vectors
There are three main vector transformations:
Scaling
Translation
Rotation
In previous notebooks, we have applied the first two kinds of transformations. Now, let us learn how to use a fundamental transformation on vectors called rotation.
The rotation operation changes the direction of a vector, letting unaffected its dimensionality and its norm. Let us explain with some examples.
In the following cells, we will define a NumPy matrix and a NumPy array. Soon we will explain how this is related to matrix rotation.
Example 1
The dot product between a vector and a square matrix produces a rotation and a scaling of the original vector.
Remember that our recommended way to get the dot product in Python is np.dot(a, b):
We are going to use Pyplot to inspect the effect of the rotation on 2D vectors visually. For that, we have created a function plot_vectors()
that takes care of all the intricate parts of the visual formatting. The code for this function is inside the utils_nb.py
file.
Now we can plot the vector in a cartesian plane. The cartesian plane will be centered at [0,0]
and its x and y limits will be between [-4, +4]
Now, let's plot in the same system our vector and its dot product with the matrix
Note that the output vector y
(blue) is transformed in another vector.
Example 2
We are going to use Pyplot to inspect the effect of the rotation on 2D vectors visually. For that, we have created a function that takes care of all the intricate parts of the visual formatting. The following procedure plots an arrow within a Pyplot canvas.
Data that is composed of 2 real attributes is telling to belong to a or space. Rotation matrices in rotate a given vector by a counterclockwise angle in a fixed coordinate system. Rotation matrices are of the form:
The trigonometric functions in Numpy require the angle in radians, not in degrees. In the next cell, we define a rotation matrix that rotates vectors by .
Some points to note:
The norm of the input vector is the same as the norm of the output vector. Rotations matrices do not modify the norm of the vector, only its direction.
The norm of any rotation matrix is always
Frobenius Norm
The Frobenius norm is the generalization to of the already known norm function for vectors
For a given matrix A, the frobenius norm is defined as:
np.square()
is a way to square each element of a matrix. It must be equivalent to use the * operator in Numpy arrays.
Now you can sum over the elements of the resulting array, and then get the square root of the sum.
That was the extended version of the np.linalg.norm()
function. You can check that it yields the same result.
Congratulations!! We've covered a few more matrix operations in this lab. This will come in handy in this week's programming assignment!