Vectors and Vector Spaces
π Vectors & Vector Spaces: The Foundation of Data
Linear Algebra is the study of linear mappings between vector spaces. In software engineering and machine learning, it provides the fundamental framework for representing data, model parameters, and geometric transformations.
π’ Level 1: Core Definitions
1. Vector Spaces ()
A vector space is a set equipped with two operations: Vector Addition and Scalar Multiplication. To be a formal vector space, it must satisfy 8 axioms, including closure, commutativity, associativity, and the existence of identity and inverse elements.
- Real Coordinate Space (): The most common vector space in computing, consisting of all possible -tuples of real numbers.
2. Linear Combinations
A vector is a linear combination of a set of vectors if it can be expressed as: where are scalars.
import numpy as np
# Defining vectors in R^3
v1 = np.array([1, 2, 3])
v2 = np.array([4, 5, 6])
# A linear combination: 2*v1 + 0.5*v2
v_combined = 2 * v1 + 0.5 * v2
print(f"Linear Combination: {v_combined}")π‘ Level 2: Basis and Dimension
3. Linear Independence
A set of vectors is linearly independent if no vector in the set can be written as a linear combination of the others. Formally: If any , the vectors are linearly dependent.
4. Span and Basis
- Span: The set of all possible linear combinations of a set of vectors. It represents the βreachβ of those vectors.
- Basis: A linearly independent set of vectors that spans the entire space. It is the βminimalβ set of vectors needed to reach every point in the space.
- Dimension: The number of vectors in any basis for the space. For , the dimension is .
5. Change of Basis
In Machine Learning (e.g., PCA), we often represent data in a new coordinate system that better captures the variance. We use a Transition Matrix to move from basis to basis :
π΄ Level 3: Orthogonality and Norms
6. Dot Product and Similarity
The dot product measures the alignment between two vectors: .
- Orthogonal: If , the vectors are at 90 degrees to each other.
- Orthonormal: Orthogonal vectors that each have a magnitude (length) of 1.
7. Vector Norms ( Norms)
Norms measure the βsizeβ or βlengthβ of a vector, which is critical for regularization in ML.
- Norm (Manhattan): . Used in Lasso regression to promote sparsity.
- Norm (Euclidean): . The standard distance metric; used in Ridge regression to prevent large outliers.
- Norm (Max): . The maximum absolute component.
# Calculating norms with NumPy
x = np.array([3, -4])
l1_norm = np.linalg.norm(x, ord=1) # 3 + 4 = 7
l2_norm = np.linalg.norm(x, ord=2) # sqrt(3^2 + (-4)^2) = 5
print(f"L1: {l1_norm}, L2: {l2_norm}")