Topics: AI | Data Science | Data Visualization | Deep Learning | Machine Learning | NLP | Python | R | Statistics

KDnuggets Home » News » 2021 » Feb » Tutorials, Overviews » Essential Math for Data Science: Scalars and Vectors ( 21:n07 )

# Essential Math for Data Science: Scalars and Vectors

Linear algebra is the branch of mathematics that studies vector spaces. You’ll see how vectors constitute vector spaces and how linear algebra applies linear transformations to these spaces. You’ll also learn the powerful relationship between sets of linear equations and vector equations.

By Hadrien Jean, Machine Learning Scientist

Machines only understand numbers. For instance, if you want to create a spam detector, you have first to convert your text data into numbers (for instance, through word embeddings). Data can then be stored in vectors, matrices, and tensors. For instance, images are represented as matrices of values between 0 and 255 representing the luminosity of each color for each pixel. It is possible to leverage the tools and concepts from the field of linear algebra to manipulate these vectors, matrices and tensors.

Linear algebra is the branch of mathematics that studies vector spaces. You’ll see how vectors constitute vector spaces and how linear algebra applies linear transformations to these spaces. You’ll also learn the powerful relationship between sets of linear equations and vector equations, related to important data science concepts like least squares approximation. You’ll finally learn important matrix decomposition methods: eigendecomposition and Singular Value Decomposition (SVD), important to understand unsupervised learning methods like Principal Component Analysis (PCA).

### What are Vectors?

Linear algebra deals with vectors. Other mathematical entities in the field can be defined by their relationship to vectors: scalars, for example, are single numbers that scale vectors (stretching or contracting) when they are multiplied by them.

However, vectors refer to various concepts according to the field they are used in. In the context of data science, they are a way to store values from your data. For instance, take the height and weight of people: since they are distinct values with different meanings, you need to store them separately, for instance using two vectors. You can then do operations on vectors to manipulate these features without loosing the fact that the values correspond to different attributes.

You can also use vectors to store data samples, for instance, store the height of ten people as a vector containing ten values.

Notation

We’ll use lowercase, boldface letters to name vectors (such as vv). As usual, refer to the Appendix in Essential Math for Data Science to have the summary of the notations used in this book.

### Geometric and Coordinate Vectors

Coordinates are values describing a position. For instance, any position on earth can be specified by geographical coordinates (latitude, longitude, and elevation).

Geometric Vectors

Geometric vectors, also called Euclidean vectors, are mathematical objects defined by their magnitude (the length) and their direction. These properties allow you to describe the displacement from a location to another.

Figure 1: A geometric vector running from A to B.

For instance, Figure 1 shows that the point A has coordinates (1, 1) and the point B has coordinates (3, 2). The geometric vectors v describes the displacement from A to B, but since vectors are defined by their magnitude and direction, you can also represent v as starting from the origin.

Cartesian Plane

In Figure 1, we used a coordinate system called the Cartesian plane. The horizontal and vertical lines are the coordinate axes, usually labeled respectively x and y. The intersection of the two coordinates is called the origin and corresponds to the coordinate 0 for each axis.

In a Cartesian plane, any position can be specified by the x and the y coordinates. The Cartesian coordinate system can be extended to more dimensions: the position of a point in a n-dimensional space is specified by nn coordinates. The real coordinate n-dimensional space, containing n-tuples of real numbers, is named $\boldsymbol{\mathbf{\mathbf{R}^n}}$. For instance, the space $\boldsymbol{\mathbf{\mathbf{R}^2}}$ is the two-dimensional space containing pairs of real numbers (the coordinates). In three dimensions ($\boldsymbol{\mathbf{\mathbf{R}^3}}$), a point in space is represented by three real numbers.

Coordinate Vectors

Coordinate vectors are ordered lists of numbers corresponding to the vector coordinates. Since vector initial points are at the origin, you need to encode only the coordinates of the terminal point.

Figure 2: The vector vv has coordinates (3, 2) corresponding to three units from the origin on the x-axis and two on the y-axis.

For instance, let’s take the vector v represented in Figure 2. The corresponding coordinate vector is as follows:

$\boldsymbol{\mathbf{v = \begin{bmatrix} 3 \\\\ 2 \end{bmatrix}}}$

Each value is associated with a direction: in this case, the first value corresponds to the the xx-axis direction and the second number to the y-axis.

Figure 3: Components of a coordinate vector.

As illustrated in Figure 3, these values are called components or entries of the vector.

Figure 4: Vectors can be represented as points in the Cartesian plane.

In addition, as represented in Figure 4, you can simply represent the terminal point of the arrow: this is a scatter-plot.

Indexing

Indexing refers to the process of getting a vector component (one of the values from the vector) using its position (its index).

Python uses zero-based indexing, meaning that the first index is zero. However mathematically, the convention is to use one-based indexing. I’ll denote the component i of the vector v with a subscript, as vi, without bold font because the component of the vector is a scalar.

Numpy

In Numpy, vectors are called one-dimensional arrays. You can use the function np.array() to create one:

v = np.array([3, 2])
v

array([3, 2])

More Components

Let’s take the example of v, a three-dimensional vectors defined as follows:

$\boldsymbol{\mathbf{v = \begin{bmatrix} 3 \\\\ 4 \\\\ 2 \end{bmatrix}}}$

As shown in in Figure 5, you can reach the endpoint of the vector by traveling 3 units in the xx-axis, 4 in the yy-axis, and 2 in the zz-axis.

Figure 5: Three-dimensional representation of the origin at (0, 0, 0) and the point at (3, 4, 2).

More generally, in a n-dimensional space, the position of a terminal point is described by n components.

Dimensions

You can denote the dimensionality of a vector using the set notation $\boldsymbol{\mathbf{\mathbf{R}^n}}$. It expresses the real coordinate space: this is the nn-dimensional space with real numbers as coordinate values.

For instance, vectors in $\boldsymbol{\mathbf{\mathbf{R}^3}}$ have three components, as the following vector v for example:

$\boldsymbol{\mathbf{v = \begin{bmatrix} 2.0 \\\\ 1.1 \\\\ -2.5 \end{bmatrix}}}$

Vectors in Data Science

In the context of data science, you can use coordinate vectors to represent your data.

You can represent data samples as vectors with each component corresponding to a feature. For instance, in a real estate dataset, you could have a vector corresponding to an apartment with its features as different components (like the number of rooms, the location etc.).

Another way to do it is to create one vector per feature, each containing all observations.

Storing data in vectors allows you to leverage linear algebra tools. Note that, even if you can’t visualize vectors with a large number of components, you can still apply the same operations on them. This means that you can get insights about linear algebra using two or three dimensions, and then, use what you learn with a larger number of dimensions.

### The Dot Product

The dot product (referring to the dot symbol used to characterize this operation), also called scalar product, is an operation done on vectors. It takes two vectors, but unlike addition and scalar multiplication, it returns a single number (a scalar, hence the name). It is an example of a more general operation called the inner product.

Figure 6: Illustration of the dot product.

Figure 6 shows an illustration of how the dot product works. You can see that it corresponds to the sum of the multiplication of the components with same index.

### Definition

The dot product between two vectors u and v, denoted by the symbol ⋅, is defined as the sum of the product of each pair of components. More formally, it is expressed as:

$\boldsymbol{\mathbf{u \cdot v = \sum_{i=1}^m u_i v_i}}$

with m the number of components of the vectors u and v (they must have the same number of components), and i the index of the current vector component.

Dot Symbol

Note that the symbol of the dot product is the same as the dot used to refer to multiplication between scalars. The context (if the elements are scalars or vectors) tells you which one it is.

Let’s take an example. You have the following vectors:

$\boldsymbol{\mathbf{u = \begin{bmatrix} 2 \\\\ 4 \\\\ 7 \end{bmatrix}}}$

and

$\boldsymbol{\mathbf{v = \begin{bmatrix} 5 \\\\ 1 \\\\ 3 \end{bmatrix}}}$

The dot product of these two vectors is defined as:

$\boldsymbol{\mathbf{u \cdot v = \begin{bmatrix} 2 \\\\ 4 \\\\ 7 \end{bmatrix} \cdot \begin{bmatrix} 5 \\\\ 1 \\\\ 3 \end{bmatrix} = 2 \times 5 + 4 \times 1 + 7 \times 3 = 35}}$

The dot product between u and v is 35. It converts the two vectors u and v into a scalar.

Let’s use Numpy to calculate the dot product of these vectors. You can use the method dot() of Numpy arrays:

u = np.array([2, 4, 7])
v = np.array([5, 1, 3])
u.dot(v)

35

It is also possible to use the following equivalent syntax:

np.dot(u, v)

35

Or, with Python 3.5+, it is also possible to use the @ operator:

u @ v

35

Vector Multiplication

Note that the dot product is different from the element-wise multiplication, also called the Hadamard product, which returns another vector. The symbol ⊙⊙ is generally used to characterize this operation. For instance:

$\boldsymbol{\mathbf{u \odot v = \begin{bmatrix} 2 \\\\ 4 \\\\ 7 \end{bmatrix} \odot \begin{bmatrix} 5 \\\\ 1 \\\\ 3 \end{bmatrix} = \begin{bmatrix} 2 \cdot 5 \\\\ 4 \cdot 1 \\\\ 7 \cdot 3 \end{bmatrix} = \begin{bmatrix} 10 \\\\ 4 \\\\ 21 \end{bmatrix}}}$

Dot Product and Vector Length

The squared L2 norm can be calculated using the dot product of the vector with itself (u ⋅ u):

$\boldsymbol{\mathbf{||u||_2^2 = u \cdot u}}$

This is an important property in machine learning, as you saw in Essential Math for Data Science.

Special Cases

The dot product between two orthogonal vectors is equal to 0. In addition, the dot product between a unit vector and itself is equal to 1.

### Geometric interpretation: Projections

How can you interpret the dot product operation with geometric vectors. You have seen in Essential Math for Data Science the geometric interpretation of the addition and scalar multiplication of vectors, but what about the dot product?

Let’s take the two following vectors:

$\boldsymbol{\mathbf{u = \begin{bmatrix} 1 \\\\ 2 \end{bmatrix}}}$

and

$\boldsymbol{\mathbf{v = \begin{bmatrix} 2 \\\\ 2 \end{bmatrix}}}$

First, let’s calculate the dot product of u and v:

$\boldsymbol{\mathbf{u \cdot v = \begin{bmatrix} 1 \\\\ 2 \end{bmatrix} \cdot \begin{bmatrix} 2 \\\\ 2 \end{bmatrix} = 2 \cdot 1 + 2 \cdot 2 = 6}}$

What is the meaning of this scalar? Well, it is related to the idea of projecting u onto v.

Figure 7: The dot product can be seen as the length of vv multiplied by the length of the projection (the vector uprojuproj).

As shown in Figure 7, the projection of u on the line with the direction of v is like the shadow of the vector u on this line. The value of the dot product (6 in our example) corresponds to the multiplication of the length of v (the L2 norm ∥v∥) and the length of the projection of u on v (the L2 norm ∥uproj∥). You want to calculate:

$\boldsymbol{\mathbf{||u_{\text{proj}}||_2 \cdot ||v||_2 \phantom{s}}}$

Note that the elements are scalars, so the dot symbol refers to a multiplication of these values. And you have:

$\boldsymbol{\mathbf{||v||_2 = \sqrt{2^2 + 2^2} = \sqrt{8}}}$

The projection of u onto v is defined as follows (you can refer to Essential Math for Data Science to see the mathematical details about the projection of a vector onto a line):

$\boldsymbol{\mathbf{u_{\text{proj}} = \frac{u^{\text{T}}v}{v^{\text{T}}v}v = \frac{6}{8}v = 0.75v}}$

So the L2 norm of uproj is the L2 norm of 0.75 times v:

$\boldsymbol{\mathbf{||u_{\text{proj}}||_2 = 0.75 ||v||_2 = 0.75 \cdot \sqrt{8}}}$

Finally, the multiplication of the length of v and the length of the projection is:

$\boldsymbol{\mathbf{||v||_2 \cdot ||u_{\text{proj}}||_2 = 0.75 \cdot \sqrt{8} \cdot \sqrt{8} = 0.75 \cdot 8 = 6}}$

This shows that you can think of the dot product on geometric vectors as a projection. Using the projection gives you the same result as with the dot product formula.

Furthermore, the value that you obtain with the dot product tells you the relationship between the two vectors. If this value is positive, the angle between the vectors is less than 90 degrees, if it is negative, the angle is greater than 90 degrees, if it is zero, the vectors are orthogonal and the angle is 90 degrees.

### Properties

Let’s review some properties of the dot product.

Distributive

The dot product is distributive. This means that, for instance, with the three vectors uv and w, you have:

$\boldsymbol{\mathbf{u \cdot (v + w) = u \cdot v + u \cdot w}}$

Associative

The dot product is not associative, meaning that the order of the operations matters. For instance:

$\boldsymbol{\mathbf{u \cdot (v \cdot w) \neq (u \cdot v) \cdot w}}$

The dot product is not a binary operator: the result of the dot product between two vectors is not another vector (but a scalar).

Commutative

The dot product between vectors is said to be commutative. This means that the order of the vectors around the dot product doesn’t matter. You have:

$\boldsymbol{\mathbf{u \cdot v = v \cdot u}}$

However, be careful, because this is not necessarily true for matrices.

Bio: Hadrien Jean is a machine learning scientist. He owns a Ph.D in cognitive science from the Ecole Normale Superieure, Paris, where he did research on auditory perception using behavioral and electrophysiological data. He previously worked in industry where he built deep learning pipelines for speech processing. At the corner of data science and environment, he works on projects about biodiversity assessement using deep learning applied to audio recordings. He also periodically creates content and teaches at Le Wagon (data science Bootcamp), and writes articles in his blog (hadrienj.github.io).

Original. Reposted with permission.

Related: