Lecture 01 - Vectors and Matrices

📚 Lecture 01 - Vectors and Matrices.pdf

Brief overview

This document introduces the foundational concepts of vectors and matrices, essential mathematical tools for representing and manipulating data. Understanding these concepts is crucial for data science and machine learning as they form the basis for many algorithms and data structures.

Key points

  • Introduction to vectors, vector spaces, and fundamental vector operations.
  • How vectors represent data and their applications in machine learning tasks.
  • Challenges of high-dimensional vector spaces and the curse of dimensionality.
  • An introduction to matrices as representations of linear transformations.
  • Key matrix operations including multiplication, special forms, and inversion.

1. Introduction to vectors and vector spaces

1.1 The necessity of vectors in data representation

  • Traditional text representations (strings) lack the structural and semantic depth required for advanced computational tasks like machine translation 3.
  • Simple dictionary lookups of words can lead to nonsensical translations when context and nuanced meaning are absent 4.
  • To imbue text fragments (words, phrases, sentences) with additional mathematical structure and enable semantic operations, they are placed within a vector space 5.

1.2 Understanding vectors and vector spaces

  • Vectors are fundamentally ordered tuples of real numbers, each representing a distance along an orthogonal (independent) direction.
  • A vector has a fixed dimension, denoted as nn, representing the number of such orthogonal directions 7.
  • The set of all nn-dimensional vectors forms the vector space Rn\mathbb{R}^n.
  • Vectors can be conceptualized as points in space, arrows originating from the origin, or simply numerical tuples. The "points in space" model is often most useful for data representation, where vectors represent data points and matrices warp this space 14.

This diagram illustrates the mathematical representation of a vector as an ordered tuple of real numbers.

Key Concept: Vectors are ordered tuples of real numbers with fixed dimensions.

1.3 Core vector operations

  • Scalar multiplication: For a scalar aa and a vector x=[x1,x2,…,xn]\mathbf{x} = [x_1, x_2, \dots, x_n], the operation axa\mathbf{x} results in a new vector where each element is scaled by aa, i.e., [ax1,ax2,…,axn][ax_1, ax_2, \dots, ax_n]. This operation maps R×Rn→Rn\mathbb{R} \times \mathbb{R}^n \to \mathbb{R}^n.
  • Vector addition: For two vectors x=[x1,x2,…,xn]\mathbf{x} = [x_1, x_2, \dots, x_n] and y=[y1,y2,…,yn]\mathbf{y} = [y_1, y_2, \dots, y_n] of equal dimension, their sum x+y\mathbf{x} + \mathbf{y} is a new vector where each element is the sum of the corresponding elements of x\mathbf{x} and y\mathbf{y}, i.e., [x1+y1,x2+y2,…,xn+yn][x_1+y_1, x_2+y_2, \dots, x_n+y_n]. This operation maps Rn×Rn→Rn\mathbb{R}^n \times \mathbb{R}^n \to \mathbb{R}^n 10.

This diagram illustrates the concept of vectors in a 2D space, showing vector addition, scalar multiplication, and the norm of a vector.

1.4 Measuring vectors and their relationships

  • Norm (∥x∥\|\mathbf{x}\|): A function that assigns a non-negative length to each vector, mapping Rn→R≥0\mathbb{R}^n \to \mathbb{R}^{\geq 0}. Vector spaces equipped with a norm are called normed vector spaces or topological vector spaces.
  • Inner product (x⋅y\mathbf{x} \cdot \mathbf{y} or ⟨x,y⟩\langle \mathbf{x}, \mathbf{y} \rangle): An operation that determines the angle between two vectors, mapping Rn×Rn→R\mathbb{R}^n \times \mathbb{R}^n \to \mathbb{R}. For real vectors, it is defined as x⋅y=x1y1+x2y2+⋯+xnyn\mathbf{x} \cdot \mathbf{y} = x_1y_1 + x_2y_2 + \dots + x_ny_n 11.
  • The inner product of two orthogonal vectors is zero.
  • Vector spaces equipped with an inner product are called inner product spaces 12.

1.4.1 Relationships between topological and geometric spaces

A hierarchy exists among different types of vector spaces:

Space TypeDefinitionRelationship to Others
Vector SpaceSet with addition and scalar multiplicationBroadest category
Metric SpaceDefines distance between pointsBroader than Normed
Normed Vector SpaceVector space with a defined normSubset of Metric
Inner Product SpaceVector space with a defined inner productSubset of Normed

This Venn diagram illustrates the hierarchical relationship between different types of vector spaces.

  • A norm can be derived from an inner product: ∥x∥=⟨x,x⟩\|\mathbf{x}\| = \sqrt{\langle \mathbf{x}, \mathbf{x} \rangle}.
  • However, not all norms can be derived from an inner product 13.

This diagram illustrates vector operations and geometric concepts in an inner product space, showing vectors x, y, x+y, and ax, along with their norms and a neighborhood around vector x.

1.5 Vectors in practice

  • In computational contexts, 1D floating-point arrays often serve as practical approximations of mathematical vectors 15.
  • Vectors are a fundamental lingua franca for data because they can be composed, compared, and weighted.
  • Libraries like NumPy utilize efficient ndarray data structures to operate on vectors concisely and efficiently 16.

Key Insight: Vectors provide the mathematical framework to represent and manipulate diverse data types, enabling semantic understanding and complex operations in data science.

Three diagrams illustrate vector space representations of word relationships, including male-female, verb tense, and country-capital pairings.

2. Vector data representation and applications

2.1 Representing data as vectors

  • Datasets are frequently structured as 2D tables, where each row represents an observation or data point, and each column signifies a feature or attribute.
  • Each row can be interpreted as a vector in Rn\mathbb{R}^n, where nn is the number of features.

A 2D table representing vector data with features like heart rate, systolic, diastolic, and vo2.

2.2 Geometric operations with vectors

  • Vectors enable fundamental geometric operations in multi-dimensional spaces.
  • These operations include transformations such as scaling, rotation, mirroring, and translation, which are crucial for manipulating geometric data.
  • Applications range from 3D model representations, where vertices are defined by vectors, to sophisticated algorithms in machine learning 18.

2.3 Vector applications in machine learning

  • Machine learning extensively relies on vector representations.
  • The core ML process involves transforming raw data into feature vectors and then developing functions to map these vectors to predictions (e.g., class labels).
  • Many ML algorithms function by performing geometric operations on these vectors, such as calculating distances, warping data spaces, or computing angles.

2.3.1 Classification tasks: k-Nearest Neighbors

  • The k-Nearest Neighbors (k-NN) algorithm is a prime example of a distance-based classification method.
  • It classifies a new data point based on the majority class of its kk nearest neighbors in the feature space.
  • The process involves calculating the distance between the new data point and all training examples, identifying the kk closest ones, and assigning the label that appears most frequently among them 19 21.

This diagram illustrates the k-nearest neighbors algorithm for classification, showing the initial data, distance calculation, and neighbor identification for voting.

  • The choice of kk is critical and can significantly influence the prediction outcome.

This diagram illustrates the k-Nearest Neighbors (k-NN) classification algorithm, showing how a new data point is classified based on its nearest neighbors.

  • The selection of a suitable distance function (norm or cosine similarity) is also a key consideration for effective classification 22.

2.3.2 Image compression

  • Images can be represented as vectors by treating pixel data, or groups of pixels (e.g., 8x8 patches), as elements of a high-dimensional vector. An 8x8 patch becomes a 64-dimensional vector.

A bar chart displays the 'Image vector' with 'Vector element' on the x-axis and 'Value' on the y-axis, representing pixel values.

  • Image compression can be achieved by clustering these image vectors into a smaller set of representative vectors (a "codebook"). The compressed image is then reconstructed using these representative vectors, significantly reducing storage requirements when the number of representative vectors (mm) is much smaller than the total number of patches (nn) 23.

2.4 Measuring vector magnitude: Norms

  • The "size" or magnitude of a vector is quantified using norms.
  • The L2-norm, also known as the Euclidean norm, is commonly used and represents the straight-line distance from the origin to the vector's endpoint in Euclidean space. It is computed as the square root of the sum of the squares of its elements 26.

For a vector x=[x1,x2,…,xn]\mathbf{x} = [x_1, x_2, \dots, x_n], the L2-norm is: ∣∣x∣∣2=∑i=1nxi2||\mathbf{x}||_2 = \sqrt{\sum_{i=1}^n x_i^2}

  • More generally, Lp-norms (or Minkowski norms) are defined as: ∣∣x∣∣p=(∑i=1n∣xi∣p)1/p||\mathbf{x}||_p = \left(\sum_{i=1}^n |x_i|^p\right)^{1/p} 27.

  • Different norms result in different geometric interpretations of distance:

  • L1-norm (Taxicab norm): Sum of absolute values of elements. Contour lines are squares rotated by 45 degrees.

  • L2-norm (Euclidean norm): Square root of the sum of squared elements. Contour lines are circles (or spheres in higher dimensions).

  • L-infinity norm: Maximum absolute value of elements. Contour lines are squares aligned with the axes.

Geometric visualizations of different norms, showing contour lines representing points of equal distance from the origin.

2.5 Unit vectors and normalization

  • A unit vector is a vector with a norm of 1. The specific definition of a unit vector depends on the norm being used.
  • Normalization, particularly for the Euclidean norm, involves scaling a vector x\mathbf{x} by 1/∣∣x∣∣21/||\mathbf{x}||_2.
  • Unit vectors are conceptually useful for representing pure direction, independent of magnitude 29.

2.6 Inner products and vector relationships

  • The inner product (or dot product for real vectors) of two vectors in Rn\mathbb{R}^n measures the angle between them and is computed as the sum of their elementwise products. x⋅y=∑i=1nxiyi\mathbf{x} \cdot \mathbf{y} = \sum_{i=1}^n x_i y_i 30.

  • The inner product is closely related to the cosine similarity, which is defined as: cos⁡θ=x⋅y∣∣x∣∣⋅∣∣y∣∣\cos \theta = \frac{\mathbf{x} \cdot \mathbf{y}}{||\mathbf{x}|| \cdot ||\mathbf{y}||}

  • For unit vectors, the cosine similarity simplifies to the inner product itself, as their norms are 1.

  • The inner product is defined for vectors of the same dimension and within inner product spaces. It is distinct from the general dot product operation in some libraries 31.

2.7 Basic vector statistics

  • Statistical operations common to scalar numbers can be generalized to vectors.
  • The mean vector of a collection of NN vectors is computed by averaging each dimension across all vectors. This vector represents the geometric centroid or "center of mass" of the vector set 32.
OperationDescriptionTypical Use
Mean vectorThe geometric centroid capturing the average position of a set of vectors.Finding the central tendency of multivariate data.
CenteringSubtracting the mean vector from each vector in a dataset to achieve zero mean.Data preprocessing for algorithms sensitive to scale.
  • Centering a dataset involves subtracting the mean vector from each data point (row) to ensure the dataset has a zero mean. This is a common preprocessing step 33.

🔑 Important: Vector representations are foundational in modern data science, enabling sophisticated geometric computations that underpin many machine learning algorithms and data analysis techniques.

3. High-dimensional vector spaces and the curse of dimensionality

3.1 Understanding high-dimensional vector spaces

  • High-dimensional vector spaces are ubiquitous in data science, referring to spaces with more than a few dimensions (e.g., >3>3). Datasets with tens or hundreds of dimensions are common, and millions of dimensions are not unheard of.

  • The curse of dimensionality describes the phenomenon where algorithms that perform well in low-dimensional spaces degrade significantly as the number of dimensions increases. This degradation arises from the exponential increase in the data required for meaningful representation.

📝 Example: Consider predicting if tomorrow's temperature will exceed 30°C using weather station data. With one dimension (temperature), a simple histogram suffices. With two dimensions (temperature and humidity), a 2D histogram can visualize relationships.

A 2D histogram visualizing the relationship between temperature and relative humidity.

  • As the number of features (dimensions) increases, the number of bins needed for a histogram grows exponentially. For instance, with 10 features, each discretized into 20 bins, we would require 201020^{10} (over 10 trillion) bins.

  • This exponential growth in required bins vastly outstrips the available data points (e.g., 10,000 measurements), leading to sparse data and hindering generalization.

3.2 Matrices as linear operators

  • Matrices are fundamental mathematical objects, defined as 2D arrays of real numbers. They are used to represent linear maps or linear transformations between vector spaces.

  • While vectors can be thought of as points in space, matrices represent operations that transform these points.

  • A matrix AA of dimensions n×mn \times m (denoted as A∈Rn×mA \in \mathbb{R}^{n \times m}) defines a function that maps vectors from an mm-dimensional space to an nn-dimensional space (Rm→Rn\mathbb{R}^m \to \mathbb{R}^n).

A general n x m matrix A is shown with its elements denoted as a_i,j, where i represents the row and j represents the column.

  • The core property of linear maps is linearity:

  • f(x+y)=f(x)+f(y)f(\mathbf{x} + \mathbf{y}) = f(\mathbf{x}) + f(\mathbf{y}) (the transform of a sum is the sum of transforms).

  • f(cx)=cf(x)f(c\mathbf{x}) = cf(\mathbf{x}) (the transform of a scalar multiple is the scalar multiple of the transform). This property makes linear operations tractable and predictable.

  • When an n×nn \times n matrix maps a vector space onto itself (Rn→Rn\mathbb{R}^n \to \mathbb{R}^n), it is called a linear transform. A specific type of linear map is a linear projection, where applying the transformation twice yields the same result as applying it once (f(x)=f(f(x))f(\mathbf{x}) = f(f(\mathbf{x}))).

🔑 Important: Linear maps preserve the structure of vector addition and scalar multiplication, making them analytically tractable.

3.3 Matrix operations and applications

  • Several fundamental operations can be performed with matrices:
  • Addition: C=A+BC = A + B where A,B,C∈Rn×mA, B, C \in \mathbb{R}^{n \times m}. Requires matrices to have identical dimensions.
  • Scalar Multiplication: C=sAC = sA where A,C∈Rn×mA, C \in \mathbb{R}^{n \times m} and s∈Rs \in \mathbb{R}. Each element of AA is multiplied by ss.
  • Transposition: B=ATB = A^T where A∈Rn×mA \in \mathbb{R}^{n \times m} and B∈Rm×nB \in \mathbb{R}^{m \times n}. Rows become columns and vice versa.

This diagram illustrates the elementwise addition of two matrices, A and B, of equal size.

  • Matrices can also be applied to vectors (y=Ax\mathbf{y} = A\mathbf{x}). This operation maps an mm-dimensional vector to an nn-dimensional vector, resulting in a vector y∈Rn\mathbf{y} \in \mathbb{R}^n.

  • The application of a matrix to a vector can be understood as forming a linear combination (or weighted sum) of the matrix's columns, where the weights are the elements of the vector. Specifically, for A∈Rn×mA \in \mathbb{R}^{n \times m} and x∈Rm\mathbf{x} \in \mathbb{R}^m, the result y=Ax\mathbf{y} = A\mathbf{x} is computed by summing the ii-th column of AA scaled by the ii-th element of x\mathbf{x} (xix_i) for all ii from 1 to mm.

This diagram illustrates the process of matrix-vector multiplication, showing how the elements of the matrix A are multiplied by the elements of the vector x.

  • Linear transformations can geometrically warp space, such as rotations. For example, a 90-degree rotation on a 2D plane transforms the grid accordingly.

A diagram illustrates the effect of a linear transformation, specifically a 90-degree rotation, on a 2D plane.

  • The composition of two matrices, C=ABC = AB, represents the sequential application of the linear transformations defined by AA and BB 34.

🔑 Important: Matrix multiplication represents the composition of linear transformations. If AA represents ff and BB represents gg, then ABAB represents f∘gf \circ g.

OperationInput DimensionsOutput DimensionsDescription
AdditionA∈Rn×m,B∈Rn×mA \in \mathbb{R}^{n \times m}, B \in \mathbb{R}^{n \times m}C∈Rn×mC \in \mathbb{R}^{n \times m}Element-wise summation.
Scalar MultiplicationA∈Rn×m,s∈RA \in \mathbb{R}^{n \times m}, s \in \mathbb{R}C∈Rn×mC \in \mathbb{R}^{n \times m}Each element of AA multiplied by scalar ss.
TranspositionA∈Rn×mA \in \mathbb{R}^{n \times m}B∈Rm×nB \in \mathbb{R}^{m \times n}Rows and columns are interchanged.
Vector ApplicationA∈Rn×m,x∈RmA \in \mathbb{R}^{n \times m}, \mathbf{x} \in \mathbb{R}^my∈Rn\mathbf{y} \in \mathbb{R}^nLinear combination of matrix columns by vector elements.
Matrix MultiplicationA∈Rn×m,B∈Rm×pA \in \mathbb{R}^{n \times m}, B \in \mathbb{R}^{m \times p}C∈Rn×pC \in \mathbb{R}^{n \times p}Composition of linear transformations.

🔑 Important: The curse of dimensionality implies that as dimensions increase, the volume of the space grows exponentially, leading to data sparsity and computational challenges for algorithms. Matrices provide a powerful framework for manipulating and transforming data in these high-dimensional spaces via linear operations.

4. Matrix multiplication, special forms, and inversion

4.1 Matrix multiplication

Matrix multiplication defines the product C=ABC = AB where AA, BB, and CC are matrices. This operation is fundamental for composing linear transformations.

If matrix AA represents linear transformation f(x)f(x) and matrix BB represents linear transformation g(x)g(x), then the product BABA represents the composite transformation g(f(x))g(f(x)), where the transformations are applied from right to left. This composition means that BAx=B(Ax)BAx = B(Ax), signifying that the transformation AA is applied to xx first, followed by the application of transformation BB to the result 60.

Cij=∑kAikBkjC_{ij} = \sum_{k} A_{ik} B_{kj} This diagram shows the formula for matrix multiplication, where the element Cij is calculated by summing the products of elements Aik and Bkj over the index k.

  • Multiplication compatibility: For C=ABC=AB, matrix AA must be of dimensions p×qp \times q and matrix BB must be of dimensions q×rq \times r. The resulting matrix CC will have dimensions p×rp \times r 61.
  • Implementation: In Python, matrix multiplication is commonly performed using np.dot(a, b) or the more concise @ operator (a @ b) 62.
  • Non-commutativity: Matrix multiplication is generally not commutative, meaning AB≠BAAB \neq BA. The order of multiplication is critical 63.

4.2 Time complexity and special cases

The general time complexity for multiplying two square matrices of size n×nn \times n is O(n3)O(n^3). However, for matrices with specific structures, this complexity can be significantly reduced.

🔑 Important: Specialized algorithms exist that achieve complexities greater than O(n2)O(n^2) but less than O(n3)O(n^3) for general matrix multiplication 64.

4.3 Matrix transposition

The transpose of a matrix AA, denoted as ATA^T, is obtained by interchanging its rows and columns. Both .T and np.transpose(A) in NumPy can be used to compute the transpose 65.

This diagram illustrates the concept of matrix transposition using Python code and its output.

4.4 Special matrix multiplication forms

Two common forms of matrix multiplication are:

  • Outer product: The product of a p×1p \times 1 vector and a 1×q1 \times q vector results in a p×qp \times q matrix.
  • Inner product: The product of a 1×q1 \times q vector and a q×1q \times 1 vector yields a 1×11 \times 1 matrix, which is a scalar 66.

4.5 Matrix multiplication as composed linear transformations

Matrix multiplication is directly analogous to the composition of linear transformations. If AA represents a linear transformation f(x)f(x) and BB represents a linear transformation g(x)g(x), then the product BABA represents the composite transformation g(f(x))g(f(x)).

This means that applying BABA to a vector xx is equivalent to first applying AA to xx and then applying BB to the result 67.

🔑 Important: In graphics pipelines, multiple operations like translation, scaling, and rotation can be combined into a single matrix transform by multiplying their respective matrices 71.

4.6 Special matrix forms

Several matrix forms have properties that simplify operations and computations:

Matrix TypeDescriptionNumPy Function
Diagonal matrixNon-zero elements only on the main diagonal.np.diag(x)
Identity matrixDiagonal matrix with all diagonal elements equal to 1.np.eye(n)
Zero matrixAll elements are zero.np.zeros()
Upper triangularAll elements below the main diagonal are zero.N/A
Lower triangularAll elements above the main diagonal are zero.N/A

Examples of special matrix forms including diagonal, identity, zero, upper triangular, and lower triangular matrices.

4.7 Matrices for measuring spread: Covariance

Covariance matrices are crucial for understanding the spread and interdependencies of data.

  • Variance: Measures the spread of a single dataset.
  • Covariance: Measures how two datasets or variables co-vary.
  • Covariance >0> 0: Indicates variables tend to increase together.
  • Covariance <0< 0: Indicates variables tend to move in opposite directions.
  • Covariance =0= 0: Suggests no linear relationship between variables 72.
  • Covariance Matrix Properties:
  • The main diagonal contains the variances of individual variables.
  • The matrix is symmetric.
  • Eigenvalues are non-negative 73.

The formula for covariance measures how two datasets co-vary. A covariance matrix is presented, showing the covariances between multiple variables.

For a 2-dimensional dataset, a mean vector captures the "center," and a 2×22 \times 2 covariance matrix captures the "spread" or orientation of the data points 75.

A scatter plot showing data points and covariance ellipses, illustrating the spread and center of a 2D dataset.

4.8 Matrix inversion

Matrix inversion provides an operation that "undoes" a linear transformation. If AA is a matrix representing a linear transformation, its inverse, A−1A^{-1}, is a matrix such that A−1A=IA^{-1}A = I, where II is the identity matrix.

This property implies that A−1(Ax)=xA^{-1}(Ax) = x.

🔑 Important: Inversion is only defined for square matrices that are non-singular (i.e., they have a non-zero determinant).

Key properties of matrix inversion include:

  • (A−1)−1=A(A^{-1})^{-1} = A: Inverting the inverse returns the original matrix.
  • (AB)−1=B−1A−1(AB)^{-1} = B^{-1}A^{-1}: The inverse of a product of matrices is the product of their inverses in reverse order.

4.8.1 Inversion as an "undo" operation

The inverse of a matrix effectively creates a new linear operator that reverses the action of the original matrix. For example, if AA is a rotation matrix, A−1A^{-1} will be a rotation matrix in the opposite direction, returning any rotated vector to its original orientation 78.

🔑 Important: Matrix inversion is a fundamental concept for solving systems of linear equations and understanding the properties of linear transformations, acting as the multiplicative inverse in the realm of matrices 77.

© 2026 Maeve. All rights reserved. This content is protected by copyright.

See also