2.7. Chapter Summary#
\(~\)
2.7.1. Key Take-aways:#
Introduction to Vectors
A vector is an ordered collection of numbers with accompanying mathematical operations.
Purpose: Vectors represent quantities with magnitude and direction, and store collections of numerical data in science and data analysis.
Notation: Written as bold lowercase letters (e.g., \(\mathbf{u}\))
Representation: Typically shown as a column of numbers enclosed in square brackets, like \begin{equation*} \mathbf{u} = \begin{bmatrix} 0.75\ -1\ 1.75\ 2.5 \end{bmatrix} \end{equation*}
or horizontally with transpose: \(\begin{bmatrix} 0.75 ~~-1 ~~1.75 ~~2.5\end{bmatrix}^{\text{T}}\)
Components: Individual values accessed via indices (e.g., \(u_i\) for the \(i\)th component)
A scalar is a single numeric value; scalar variables are represented by italicized letters, like \(c\).
Characterization of vectors
Order: The number of axes needed to represent a mathematical object. Vectors are order 1, and scalars are order 0.
Size/dimension: Number of components in a vector. We say a vector with \(n\) components is an \(n\)-dimensional vector or just \(n\)-vector
Implementation
Using NumPy, the most direct way to create a vector is to pass a list of values to the
np.array()
class constructor, likeu = np.array([1, 4, 9])
. Such a vector is neither explicitly a row-vector nor a column vector, but acts like a row vector in most NumPy operations.Using NumPy, row vectors can be created like
u = np.array([[1, 4, 9]])
– note the double square brackets. Column vectors are most easily created likeu = np.array([[1, 4, 9]]).T
.To create vectors in PyTorch, modify any of the above NumPy calls by replacing
np.array
bytorch.tensor
. For instance,u2 = torch.tensor([[1, 4, 9]])
Visualizing Vectors
Vectors are commonly displayed using quiver plots that show the vectors as arrows that use the components of the vector as displacements; if no starting point is specified for a vector, the origin is used.
In 2-vectors, components represent \(x\) and \(y\) displacements: \(\mathbf{a} = \begin{bmatrix} a_x & a_y \end{bmatrix}.T\)
Each vector has a tail (starting point) and a head (endpoint), with the arrow pointing to the head.
Applications of Vectors
Vectors can be used for many purposes, including:
Geometrical features: Vectors represent spatial location or displacement. Example: A robot’s movements shown as arrows, where consecutive vectors indicate path segments.
Multi-dimensional numerical data: Vectors store collections of related numerical values. Example: Health survey data with height and weight.
Time-series data: Vectors represent observations collected over time at regular intervals. Example: Annual temperature measurements or monthly average house prices in an area.
Distributional data: Vectors represent allocations across categories or proportional distributions. Example: stock market portfolio allocations.
Other visualization techniques
Scatter plots are often used for large 2D data sets (less clutter than quiver plots)
Bar charts are often used for distributional data
Special Vectors
Zero vector: A vector containing all zeros, denoted as \(\mathbf{0}_n\) for a vector of size \(n\). In NumPy, created using
np.zeros(n)
.Ones vector: A vector containing all ones, denoted as \(\mathbf{1}_n\) for a vector of size \(n\). In NumPy, created using
np.ones(n)
.Standard unit vector: A vector with all components equal to zero except one element that equals one. Denoted as \(\mathbf{e}_i\) where \(i\) is the position of the 1.
Vector Operations
Vector addition: Component-wise addition: \((\mathbf{a}+\mathbf{b})_i = a_i + b_i\).
Scalar-vector multiplication: Each component is multiplied by that scalar: \((\alpha \mathbf{u})_i = \alpha u_i\).
Vector subtraction: Component-wise subtraction: \((\mathbf{a}-\mathbf{b})_i = a_i - b_i\).
Hadamard (component-wise) product: Denoted as \(\mathbf{u} \odot \mathbf{v}\), multiplies corresponding elements. In NumPy, uses the
*
operator.Dot product: Multiplication between two vectors that produces a scalar: \(\mathbf{u} \cdot \mathbf{v} = \sum_{i=0}^{n-1} u_i v_i\). In NumPy, calculated using the
@
operator.Vector norm/length: Length of a vector defined as \(\|\mathbf{b}\| = \sqrt{\mathbf{b} \cdot \mathbf{b}} = \sqrt{\sum_{i=0}^{n-1} b_i^2}\). In NumPy, calculated using
np.linalg.norm()
.Unit vectors: Vectors with length 1, created by dividing a vector by its norm.
Distance between vectors: The norm of their difference: \(\operatorname{d}(\mathbf{a},\mathbf{b}) = \|\mathbf{a}-\mathbf{b}\|\).
Self-Assessment:
The following question can be used to check your understanding of the material covered in Chapter 2 of Linear Algebra for Data Science with Python. Reload this page to get a different selection of practice questions.