Chapter 4: Linear algebraic tools in machine learning
This chapter introduction frames high-dimensional data as structured matrices whose feature-vector rows occupy nonuniform regions of feature space, motivating the linear-algebra tools studied next.
After this chapter you will be able to
- Data matrix feature geometry. Describe how a data matrix represents a dataset: each row is the feature vector for one observed input instance, columns correspond to feature dimensions, and each row can be viewed as a point in feature space.
- Structured high-dimensional data. Explain that feature-vector points are not generally distributed uniformly across feature space; points from a particular class occupy a small subregion, producing structure in the data matrix that linear algebra can analyze.
- Correlated features and topics. Explain how words that tend to occur together create correlated dimensions in document descriptor space. Describe how document points projected onto the two corresponding axes cluster near the 45-degree line when their word frequencies are approximately equal, allowing the pair to be treated as one topic-like dimension.
- Lower-dimensional subspace projection. Explain that when data points cluster near a line, plane, or other lower-dimensional subspace, they can be projected onto that subspace to reduce dimensionality. State that this approximation removes small variation away from the subspace while retaining the larger variation along it, yielding a compressed representation that emphasizes the main pattern.
- Quadratic form definition. Define a quadratic form as the scalar expression xᵀAx for a square symmetric matrix A, and describe why it is useful for expressing geometric relationships in arbitrary dimensions.
- Ellipsoid matrix representation. Describe how shifted circle and ellipse equations can be written with quadratic forms, and explain that the same representation gives hyperspheres and hyperellipsoids in arbitrary dimensions.
- Unit-direction quadratic optimization. Explain why optimizing Q=x̂ᵀAx̂ requires constraining x̂ to unit length when the question is which direction optimizes the quadratic form. State that without this constraint, changing the vector's length can make Q arbitrarily large or small.
- Eigenbasis form of quadratic forms. Describe how diagonalizing a symmetric matrix A transforms Q=x̂ᵀAx̂ into a weighted sum of squared coordinates, Σᵢλᵢyᵢ², using ŷ=Sᵀx̂. Explain that S is orthogonal because the eigenvectors of symmetric A are orthogonal, so the transformation preserves the unit length of x̂.
- Extremal eigenvector directions. State that for a symmetric matrix, Q=x̂ᵀAx̂ over unit vectors is maximized in the eigenvector direction of the largest eigenvalue, with maximum equal to that eigenvalue, and minimized in the eigenvector direction of the smallest eigenvalue. Trace the mass-transfer argument that concentrating the squared coordinate weight on the largest-eigenvalue coordinate increases the weighted sum.
- Positive definiteness criteria. Distinguish symmetric positive semidefinite matrices from symmetric positive definite matrices using their quadratic forms. State the equivalent eigenvalue criterion: semidefinite matrices have eigenvalues greater than or equal to zero, whereas definite matrices have all eigenvalues greater than zero.
- Matrix norm motivation. Explain why a norm is needed for a matrix by relating it to a vector norm as a measure of magnitude, and state that the section will examine spectral and Frobenius norms.
- Spectral norm as amplification. Explain the spectral norm of a matrix A as the maximum length of Ax̂ over all unit-vector directions x̂. Describe why vectors must be restricted to unit length: otherwise simple scaling can make the output arbitrarily large for any matrix.
- Eigenvalue computation of spectral norm. Trace how the squared length of Ax̂ becomes the quadratic form x̂ᵀAᵀAx̂, and explain that its extremal directions align with eigenvectors of AᵀA. State that the maximum is determined by the largest eigenvalue of AᵀA, equivalently relating the spectral norm to A’s largest singular value.
- Frobenius norm definition. Describe the Frobenius norm as an alternative measure of a matrix's magnitude, computed as the root mean square of all matrix elements.
- Mean-centered data spread. Explain why the range between extreme data points can misrepresent spread when points are unevenly distributed, and describe variance as the average squared distance from the mean.
- Covariance matrix structure. Describe the covariance matrix as the multidimensional generalization of scalar variance, formed from mean-centered data vectors and their transposed products; distinguish its diagonal variances from its off-diagonal cross-covariances.
- Projected variance as quadratic form. Explain how projecting data points onto a unit direction vector produces scalar components, and state that their variance is the quadratic form l̂ᵀCl̂, where C is the covariance matrix.
- First principal axis selection. State that the direction maximizing l̂ᵀCl̂ is the eigenvector of the covariance matrix associated with its largest eigenvalue, and identify this direction as the first principal axis.
- PCA dimensionality reduction. Explain how PCA produces a lower-dimensional representation when data points cluster around a lower-dimensional subspace. Describe retaining principal axes with relatively large principal values and discarding axes with small principal values, then projecting the data onto the retained subspace.
- Low-spread directions as noise. Describe why discarding directions with very small spread causes little information loss when points lie close to a line or plane. Explain that the removed perpendicular perturbations are treated as noise, while the retained projection exposes the underlying pattern and reduces storage costs.
- PyTorch PCA demonstrations. Describe the three supplied PyTorch examples: PCA computation, PCA applied to a correlated dataset, and PCA applied to an uncorrelated dataset, with their results plotted.
- PCA limitation on nonlinear patterns. Explain why linear PCA does not capture a nonlinear underlying pattern, such as data distributed around a curve. Describe how projecting such data onto PCA's straight first principal axis causes large position errors and loss of information.
- PCA compression principle. Explain how PCA compresses a dataset by replacing each data point with its projections on only the first few principal axes. State that this reduces the number of data components that must be stored within a fixed byte budget.
- Low-variance component removal. Describe why principal components associated with smaller principal values can be discarded with the least loss of accuracy. State that removing those components can actually help, as in PCA dimensionality reduction.
- Universal SVD factorization. State the SVD factorization of a matrix A as a product of three matrices and explain that it applies whether A is singular or nonsingular and rectangular or square.
- SVD factor dimensions. Describe the dimensions and roles of the three SVD factors for an m × n matrix A: Sigma is m × n and diagonal, U is m × m orthogonal, and V is n × n orthogonal.
- Singular values and eigenvectors. Explain that the diagonal entries of Sigma are the singular values, equal to the square roots of the eigenvalues of A transpose A and arranged in decreasing order; describe V's columns as eigenvectors of A transpose A and U's columns as eigenvectors of A A transpose.
- Properties of A transpose A. Explain why AᵀA is an n×n symmetric positive-semidefinite matrix when A has m rows and n columns. State that its eigenvalues are nonnegative and its eigenvectors can be chosen as orthogonal unit singular vectors, ordered by decreasing eigenvalue.
- Construction of left singular vectors. Trace how a nonzero eigenvalue-eigenvector pair (λᵢ, v̂ᵢ) of AᵀA yields ûᵢ = (1/√λᵢ)Av̂ᵢ. Explain that these constructed vectors are orthogonal unit vectors and that each ûᵢ is an eigenvector of AAᵀ with the same eigenvalue λᵢ.
- SVD construction for tall matrices. Describe how the SVD factors are constructed when A has m rows and n columns with m greater than or equal to n. State that V contains eigenvectors of AᵀA, U contains eigenvectors of AAᵀ, and the singular values in Σ are the real square roots of eigenvalues of AᵀA.
- Completion of the SVD proof. Trace how the constructed product yields A = UΣVᵀ in the m greater than or equal to n case, and state that the m less than or equal to n case follows by analogous reasoning. Include that orthogonality of V gives VVᵀ = I, completing the factorization.
- Covariance from data matrix. Describe how a dataset is arranged in a matrix X for this derivation and explain why X^T X is its covariance matrix after the data has been mean-subtracted.
- SVD computation of PCA. Explain how PCA follows from SVD of a mean-subtracted data matrix: the eigenvalues and eigenvectors of X^T X are the principal components, so performing SVD on X yields the PCA of the data.
- Limits of matrix inversion. Explain why directly solving a linear system with a matrix inverse can be undesirable or impossible. Include the numerical instability caused by a zero or near-zero determinant, and the fact that nonsquare systems do not have an ordinary inverse.
- SVD solution sequence. Trace how an SVD factorization A = UΣVᵀ turns a linear system into simple successive solves. Describe multiplying by Uᵀ using U's orthogonality, solving the diagonal Σ system, and multiplying by V using V's orthogonality to recover the solution.
- Matrix rank definition. Explain that a matrix’s rank is the number of linearly independent rows, equivalently the number of linearly independent columns. Relate rank to the number of nonzero singular values.
- Degeneracy and rank deficiency. Describe a degenerate linear system as one with dependent equations, such as an equation that can be formed by combining other equations. State that a non-full-rank matrix is degenerate, has a zero determinant in the square case, and cannot be inverted.
- SVD-based linear-system solution. Describe that the provided PyTorch listings use singular value decomposition (SVD) to solve linear systems, including an invertible system and an overdetermined system.
- Frobenius approximation error. Describe the error of a rank-r approximation Ar to A as the Frobenius norm of their difference, e = ||A − Ar||F. Explain that the best approximation is the rank-r matrix with the smallest such error.
- Truncated SVD approximation. Trace how to construct the best rank-r approximation of a rank-p matrix A when r < p: sort the singular values in decreasing order, partition U, Σ, and V, and retain the r-part factors to form U1Σ1V1T. State that this product has rank r and is the best rank-r approximation under Frobenius-norm error.
- Document retrieval task. Describe the document retrieval problem as receiving a query phrase and returning documents from a collection that match that query.
- Bag-of-words matching. Explain the bag-of-words model used for matching: it represents a document by how often its words occur and ignores where those words appear in the document.
- Inverse document frequency weighting. Explain that inverse document frequency (IDF) down-weights terms appearing in most documents by taking the inverse and absolute logarithm of the fraction of documents containing the term. Contrast the low IDF of ubiquitous terms such as "the" with the high IDF of relatively esoteric terms.
- Sparse TF-IDF document vectors. Describe how a document becomes a feature vector with one fixed coordinate per vocabulary word, where each coordinate equals that word's TF multiplied by its IDF. Explain that the vocabulary-sized vector is conceptually high-dimensional but is stored sparsely as the document's unique words and their TF×IDF scores.
- Cosine document similarity. Explain cosine similarity as the dot product of two document vectors divided by the product of their lengths, so it measures directional alignment rather than raw vector magnitude. Relate a larger cosine to greater weighted term sharing, with cosine 1 for vectors pointing in the same direction and cosine 0 for perpendicular vectors with no correlation.
- Cosine similarity blind spot. Explain why cosine similarity on original document-term vectors can rate semantically related documents as unrelated when they contain no exact overlapping terms. Describe how co-occurrence evidence from other documents, such as gun and violence appearing together, reveals an indirect relationship that direct term overlap misses.
- Topics as principal components. Describe an LSA topic as a high-variance linear subspace direction in document feature space and as a weighted combination of vocabulary terms. Explain that topics are principal components chosen because they discriminate documents by how strongly documents vary along them.
- SVD topic-space reduction. Trace the LSA procedure: form an m-by-n document-term matrix X with documents as rows and terms as columns, perform SVD, and use columns of V as topic vectors. Describe retaining topics with sufficiently large singular-value weights, projecting X onto the retained topic vectors, and computing cosine similarity between the resulting document-topic rows.
- Synthetic document-topic structure. Describe how the 500-by-3 document-term matrix is constructed and how its term-frequency pattern represents two topics. State that x0 and x1 are correlated because x1 occurs about twice as often as x0, whereas x2 varies independently over a much smaller range.
- Singular-value component interpretation. Explain how the SVD results reveal the expected principal components: one along the x0-x1 topic and one along the x2 topic. Describe why the third, much smaller singular value can be ignored: it represents only small random variation within the x0-x1 topic.
What it builds on
- Matrices and their role in machine learning (chapter 2) · The matrix representation of data, including digital images.
- Geometrical view of machine learning (chapter 1) · The geometrical view of machine learning.
- Vectors and their role in machine learning (chapter 2) · Vectors as numerical representations of objects or data points.
- Dot product of two vectors and its role in machine learning (chapter 2) · The dot product as a comparison of vectors.
- Geometric intuitions for the dot product: Feature similarity (chapter 2) · Geometric intuition for similarity between feature vectors.
- Multidimensional planes and their role in machine learning (chapter 2) · Planes and hyperplanes in multidimensional spaces.
- Multidimensional line equation (chapter 2) · Multidimensional line equations.
- Span of a set of vectors (chapter 2) · A span as the subspace generated by vectors.
Section by section
Introduction
This chapter introduction frames high-dimensional data as structured matrices whose feature-vector rows occupy nonuniform regions of feature space, motivating the linear-algebra tools studied next.
- Data matrix feature geometry: Describe how a data matrix represents a dataset: each row is the feature vector for one observed input instance, columns correspond to feature dimensions, and each row can be viewed as a point in feature space.
Why it matters: This representation connects tabular data to the geometric and matrix-based tools used throughout the chapter. - Structured high-dimensional data: Explain that feature-vector points are not generally distributed uniformly across feature space; points from a particular class occupy a small subregion, producing structure in the data matrix that linear algebra can analyze.
Why it matters: The useful patterns that machine learning seeks are reflected in this nonuniform geometric and matrix structure. - Chapter linear-algebra roadmap: State that the chapter studies linear-algebraic tools for analyzing matrix structures, including quadratic forms, principal component analysis, and a machine-learning document-retrieval application, and that working through the mathematical proofs is encouraged for deeper insight.
Why it matters: Knowing the chapter's purpose helps connect its mathematical tools to practical analysis of large, high-dimensional datasets.
Key terms: data matrix, feature vectors, input instances, feature space, dimensions, high-dimensional data, class, subregion, matrix structures, linear algebraic tools
4.1 Distribution of feature data points and true dimensionality
This section shows how high-dimensional feature data can often be represented more compactly by exploiting correlated features and projecting points onto the lower-dimensional subspace where they cluster.
- Sparse document descriptor vectors: Describe how a document is represented as a vector whose dimensions correspond to vocabulary words and whose values are word frequencies. Explain why this normally uses a sparse representation containing only the words that occur, rather than explicitly storing zeros for every absent word.
Why it matters: Sparse document vectors make very large vocabulary-based representations practical to store and use for document comparison. - Correlated features and topics: Explain how words that tend to occur together create correlated dimensions in document descriptor space. Describe how document points projected onto the two corresponding axes cluster near the 45-degree line when their word frequencies are approximately equal, allowing the pair to be treated as one topic-like dimension.
Why it matters: Correlated features reveal that apparent data dimensions can contain redundant information and can expose meaningful patterns such as topics. - Lower-dimensional subspace projection: Explain that when data points cluster near a line, plane, or other lower-dimensional subspace, they can be projected onto that subspace to reduce dimensionality. State that this approximation removes small variation away from the subspace while retaining the larger variation along it, yielding a compressed representation that emphasizes the main pattern.
Why it matters: This is the geometric rationale for dimensionality reduction methods such as PCA and for obtaining simpler, more insightful data representations.
Key terms: document descriptor vector, document space, vocabulary, sparse representation, word frequency, correlated dimensions, document feature vectors, document descriptor space, 45-degree line, gun-violence topic
4.2 Quadratic forms and their minimization
This section introduces quadratic forms as symmetric matrix expressions that represent hyperspheres and hyperellipsoids in any number of dimensions and arise in Taylor expansions and PCA.
- Quadratic form definition: Define a quadratic form as the scalar expression xᵀAx for a square symmetric matrix A, and describe why it is useful for expressing geometric relationships in arbitrary dimensions.
Why it matters: Quadratic forms provide the matrix language for describing curved geometry used in optimization and dimensionality reduction. - Ellipsoid matrix representation: Describe how shifted circle and ellipse equations can be written with quadratic forms, and explain that the same representation gives hyperspheres and hyperellipsoids in arbitrary dimensions.
Why it matters: This geometric interpretation makes matrix properties visible as the size, shape, and orientation of data-space surfaces. - Ellipsoid rotation and applications: Explain that an axis-aligned ellipsoid has a diagonal A, whereas rotating coordinates transforms A to RᵀAR and generally creates nonzero off-diagonal terms while keeping A symmetric; identify quadratic forms in the Hessian term of a multidimensional Taylor expansion and in PCA.
Why it matters: Recognizing how a quadratic form changes under rotation connects matrix entries to geometry and prepares you to see the form in optimization and PCA.
Key terms: quadratic form, square symmetric matrix, xᵀAx, hypersphere, ellipse, hyperellipsoid, position vector, center vector, diagonal matrix, off-diagonal terms
4.2.1 Minimizing quadratic forms
This section shows that a symmetric quadratic form over unit-vector directions is maximized and minimized by eigenvectors associated with its largest and smallest eigenvalues.
- Unit-direction quadratic optimization: Explain why optimizing Q=x̂ᵀAx̂ requires constraining x̂ to unit length when the question is which direction optimizes the quadratic form. State that without this constraint, changing the vector's length can make Q arbitrarily large or small.
Why it matters: The unit-vector constraint separates the meaningful choice of direction from a trivial rescaling of the input vector. - Eigenbasis form of quadratic forms: Describe how diagonalizing a symmetric matrix A transforms Q=x̂ᵀAx̂ into a weighted sum of squared coordinates, Σᵢλᵢyᵢ², using ŷ=Sᵀx̂. Explain that S is orthogonal because the eigenvectors of symmetric A are orthogonal, so the transformation preserves the unit length of x̂.
Why it matters: The eigenbasis turns a matrix quadratic form into a simple comparison among eigenvalue-weighted coordinate contributions. - Extremal eigenvector directions: State that for a symmetric matrix, Q=x̂ᵀAx̂ over unit vectors is maximized in the eigenvector direction of the largest eigenvalue, with maximum equal to that eigenvalue, and minimized in the eigenvector direction of the smallest eigenvalue. Trace the mass-transfer argument that concentrating the squared coordinate weight on the largest-eigenvalue coordinate increases the weighted sum.
Why it matters: This result converts optimization over infinitely many directions into finding extreme eigenvalues and their eigenvectors.
Key terms: quadratic form, unit vector, unit length, direction, Q=x̂ᵀAx̂, diagonalization, symmetric matrix, orthogonal matrix, eigenvectors, eigenvalues
4.2.2 Symmetric positive (semi)definite matrices
This subsection defines symmetric positive semidefinite and positive definite matrices through their quadratic forms and eigenvalues.
- Positive definiteness criteria: Distinguish symmetric positive semidefinite matrices from symmetric positive definite matrices using their quadratic forms. State the equivalent eigenvalue criterion: semidefinite matrices have eigenvalues greater than or equal to zero, whereas definite matrices have all eigenvalues greater than zero.
Why it matters: These criteria let you recognize when a matrix produces only nonnegative quadratic-form values, a property used in optimization and data analysis.
Key terms: symmetric positive semidefinite matrix, symmetric positive definite matrix, quadratic form, eigenvalues
4.3 Spectral and Frobenius norms of a matrix
This section introduces matrix norms as matrix-level counterparts to a vector's magnitude and previews the spectral and Frobenius norms.
- Matrix norm motivation: Explain why a norm is needed for a matrix by relating it to a vector norm as a measure of magnitude, and state that the section will examine spectral and Frobenius norms.
Why it matters: Matrix norms provide ways to quantify the size of matrices used throughout machine learning.
Key terms: vector norm, matrix norm, spectral norm, Frobenius norm
4.3.1 Spectral norms
This subsection defines a matrix’s spectral norm as its greatest amplification of a unit vector and relates it to the eigenvalues of AᵀA and singular values.
- Spectral norm as amplification: Explain the spectral norm of a matrix A as the maximum length of Ax̂ over all unit-vector directions x̂. Describe why vectors must be restricted to unit length: otherwise simple scaling can make the output arbitrarily large for any matrix.
Why it matters: The spectral norm measures the strongest amount by which a matrix can stretch an input vector. - Eigenvalue computation of spectral norm: Trace how the squared length of Ax̂ becomes the quadratic form x̂ᵀAᵀAx̂, and explain that its extremal directions align with eigenvectors of AᵀA. State that the maximum is determined by the largest eigenvalue of AᵀA, equivalently relating the spectral norm to A’s largest singular value.
Why it matters: This converts a search over all input directions into an eigenvalue or singular-value calculation.
Key terms: spectral norm, unit vector, amplifier, L2 norm, quadratic form, AᵀA, largest eigenvalue, largest singular value
4.3.2 Frobenius norms
This subsection defines the Frobenius norm as a matrix-magnitude measure based on its entries and relates it to singular values.
- Frobenius norm definition: Describe the Frobenius norm as an alternative measure of a matrix's magnitude, computed as the root mean square of all matrix elements.
Why it matters: The Frobenius norm gives a single scalar measure of the overall size of a matrix. - Singular-value characterization: State that the Frobenius norm can also be expressed through the matrix's singular values, identified here as eigenvalues of AᵀA, using their root-mean-square aggregate.
Why it matters: This equivalence connects an entrywise matrix-size measure to the spectral quantities used in singular value decomposition.
Key terms: Frobenius norm, root mean square, singular values, eigenvalues of AᵀA
4.4 Principal component analysis
This section builds the mean-and-covariance description of how one-dimensional and multidimensional data points are distributed, preparing the notion of spread needed for PCA.
- Mean-centered data spread: Explain why the range between extreme data points can misrepresent spread when points are unevenly distributed, and describe variance as the average squared distance from the mean.
Why it matters: Variance measures the typical spread of the data rather than letting a few extreme points determine it. - Multidimensional mean vector: Describe how the scalar mean of one-dimensional data becomes a mean vector for multidimensional data, with one mean coordinate for each coordinate axis.
Why it matters: Centering each data point around a mean vector makes it possible to describe variation across all data dimensions together. - Covariance matrix structure: Describe the covariance matrix as the multidimensional generalization of scalar variance, formed from mean-centered data vectors and their transposed products; distinguish its diagonal variances from its off-diagonal cross-covariances.
Why it matters: The covariance matrix captures both variation along individual axes and joint variation between axes, which PCA uses to find meaningful directions in the data.
Key terms: mean, variance, spread, standard deviation, mean vector, multidimensional data point, coordinate axes, X0, X1, covariance matrix
4.4.1 Direction of maximum spread
This subsection derives PCA’s principal axes by showing that data variance along a unit direction is maximized by covariance-matrix eigenvectors with the largest eigenvalues.
- Projected variance as quadratic form: Explain how projecting data points onto a unit direction vector produces scalar components, and state that their variance is the quadratic form l̂ᵀCl̂, where C is the covariance matrix.
Why it matters: This turns the geometric idea of data spread in a direction into an expression that PCA can optimize. - First principal axis selection: State that the direction maximizing l̂ᵀCl̂ is the eigenvector of the covariance matrix associated with its largest eigenvalue, and identify this direction as the first principal axis.
Why it matters: The first principal axis is the central PCA direction that captures the greatest possible variance in the data. - Principal components and values: Describe principal components as data-vector components along principal axes and principal values as the corresponding variances, or eigenvalues; distinguish the first and second versions by largest and second-largest eigenvalues.
Why it matters: This vocabulary lets you describe both the coordinates PCA produces and how much variation each retained direction represents. - Orthogonality of principal axes: Explain that principal axes are orthogonal because they are eigenvectors of the symmetric covariance matrix.
Why it matters: Orthogonal axes provide separate directions for representing the distinct spreads selected by PCA.
Key terms: unit vector l̂, dot product, covariance matrix C, quadratic form, l̂ᵀCl̂, variance, maximum variance, largest eigenvalue, eigenvector, covariance matrix
4.4.2 PCA and dimensionality reduction
This section explains how PCA reduces data dimensionality by projecting data clustered near a lower-dimensional line or plane onto the principal axes with the largest principal values.
- PCA dimensionality reduction: Explain how PCA produces a lower-dimensional representation when data points cluster around a lower-dimensional subspace. Describe retaining principal axes with relatively large principal values and discarding axes with small principal values, then projecting the data onto the retained subspace.
Why it matters: This lets you simplify a dataset while retaining its main underlying structure. - Low-spread directions as noise: Describe why discarding directions with very small spread causes little information loss when points lie close to a line or plane. Explain that the removed perpendicular perturbations are treated as noise, while the retained projection exposes the underlying pattern and reduces storage costs.
Why it matters: The value of PCA comes from preserving the dominant data pattern while removing small deviations that add complexity. - Line and plane PCA examples: Trace the dimensionality reduction in the two examples: 2D points near a line become 1D by retaining the first principal axis, and 3D points near a plane become 2D by retaining the first two principal axes. State how the number of principal values matches the original data dimensionality and how a much smaller value signals a removable direction.
Why it matters: These examples make the geometric meaning of PCA projections concrete in two and three dimensions.
Key terms: principal component, principal axis, principal value, dimensionality reduction, projection, lower-dimensional subspace, spread, orthogonal, normal, perturbations
4.4.3 PyTorch code: PCA and dimensionality reduction
This subsection provides PyTorch PCA examples for correlated and uncorrelated datasets and plots their results.
- PyTorch PCA demonstrations: Describe the three supplied PyTorch examples: PCA computation, PCA applied to a correlated dataset, and PCA applied to an uncorrelated dataset, with their results plotted.
Why it matters: These examples connect the PCA method to executable code and visible results on different kinds of data.
Key terms: PCA, PyTorch, correlated dataset, uncorrelated dataset, dimensionality reduction
4.4.4 Limitations of PCA
This subsection explains that linear PCA fails to represent nonlinear data patterns accurately because its principal axes are straight lines.
- PCA limitation on nonlinear patterns: Explain why linear PCA does not capture a nonlinear underlying pattern, such as data distributed around a curve. Describe how projecting such data onto PCA's straight first principal axis causes large position errors and loss of information.
Why it matters: Knowing this limitation helps you recognize when dimensionality reduction with linear PCA may give a misleading representation of data.
Key terms: linear PCA, non-linearly correlated data, first principal axis, projection, loss of information
4.4.5 PCA and data compression
This section explains how PCA compresses multidimensional data by retaining projections onto only the leading principal axes and discarding low-variance components.
- PCA compression principle: Explain how PCA compresses a dataset by replacing each data point with its projections on only the first few principal axes. State that this reduces the number of data components that must be stored within a fixed byte budget.
Why it matters: PCA provides a way to make data smaller while preserving its most important variation. - Low-variance component removal: Describe why principal components associated with smaller principal values can be discarded with the least loss of accuracy. State that removing those components can actually help, as in PCA dimensionality reduction.
Why it matters: Choosing which information to discard is what lets PCA reduce storage without throwing away the most useful structure. - JPEG PCA application: State that the section identifies this PCA projection-and-retention approach as the underlying principle in JPEG 98 image compression techniques.
Why it matters: This connects PCA data compression to a concrete image-compression application.
Key terms: PCA, data compression, principal axes, projections, principal components, principal values, smaller principal values, JPEG 98, image compression
4.5 Singular value decomposition
This section introduces the SVD theorem, which decomposes any matrix into orthogonal eigenvector matrices and a diagonal matrix of ordered singular values.
- Universal SVD factorization: State the SVD factorization of a matrix A as a product of three matrices and explain that it applies whether A is singular or nonsingular and rectangular or square.
Why it matters: SVD provides a single matrix decomposition that later supports PCA, latent semantic analysis, linear-system solving, and low-rank approximation. - SVD factor dimensions: Describe the dimensions and roles of the three SVD factors for an m × n matrix A: Sigma is m × n and diagonal, U is m × m orthogonal, and V is n × n orthogonal.
Why it matters: The shapes and orthogonality of the factors make it possible to apply SVD consistently to rectangular data matrices. - Singular values and eigenvectors: Explain that the diagonal entries of Sigma are the singular values, equal to the square roots of the eigenvalues of A transpose A and arranged in decreasing order; describe V's columns as eigenvectors of A transpose A and U's columns as eigenvectors of A A transpose.
Why it matters: This links SVD to eigendecomposition and identifies the ordered quantities that later determine important directions and reduced-rank representations. - SVD machine learning relevance: State that SVD is presented as a particularly important linear-algebra tool in machine learning and that PCA and LSA implementations are built on it.
Why it matters: Knowing why SVD is introduced helps connect this factorization to dimensionality reduction and document-retrieval methods.
Key terms: singular value decomposition, SVD, SVD theorem, singular matrix, nonsingular matrix, U, Σ, Sigma, V, orthogonal matrix
4.5.1 Informal proof of the SVD theorem
This subsection informally builds the singular-value decomposition result by relating the symmetric positive-semidefinite matrices AᵀA and AAᵀ and constructing corresponding orthonormal singular vectors.
- Properties of A transpose A: Explain why AᵀA is an n×n symmetric positive-semidefinite matrix when A has m rows and n columns. State that its eigenvalues are nonnegative and its eigenvectors can be chosen as orthogonal unit singular vectors, ordered by decreasing eigenvalue.
Why it matters: These properties supply the nonnegative values and orthogonal directions from which the SVD is constructed. - Properties of A A transpose: Describe the analogous result for AAᵀ: it is symmetric positive semidefinite, so its eigenvalues are nonnegative and its eigenvectors are orthogonal. Distinguish its size from AᵀA when A is m by n.
Why it matters: AAᵀ provides the corresponding output-space directions needed to pair left and right singular vectors. - Construction of left singular vectors: Trace how a nonzero eigenvalue-eigenvector pair (λᵢ, v̂ᵢ) of AᵀA yields ûᵢ = (1/√λᵢ)Av̂ᵢ. Explain that these constructed vectors are orthogonal unit vectors and that each ûᵢ is an eigenvector of AAᵀ with the same eigenvalue λᵢ.
Why it matters: This construction is the key link that turns the spectral information of AᵀA into paired singular-vector directions for A.
Key terms: AᵀA, symmetric positive semidefinite, singular values, singular vectors, orthogonal eigenvectors, unit vectors, AAᵀ, left singular vector, right singular vector, singular value
4.5.2 Proof of the SVD theorem
This subsection proves the singular value decomposition by constructing the orthogonal matrices and diagonal singular-value matrix, first for matrices with at least as many rows as columns and then analogously for the reverse case.
- SVD construction for tall matrices: Describe how the SVD factors are constructed when A has m rows and n columns with m greater than or equal to n. State that V contains eigenvectors of AᵀA, U contains eigenvectors of AAᵀ, and the singular values in Σ are the real square roots of eigenvalues of AᵀA.
Why it matters: This construction explains where the three factors in A = UΣVᵀ come from rather than treating SVD as a black-box computation. - Zero-padded columns in Σ: Explain why the extra eigenvector columns of U do not affect UΣ when m is greater than n. Describe that AAᵀ has m eigenvectors, but the last m minus n columns of U are multiplied by zero entries in Σ and therefore vanish from the product.
Why it matters: The zero padding reconciles the dimensions of a rectangular matrix with a square orthogonal matrix U. - Completion of the SVD proof: Trace how the constructed product yields A = UΣVᵀ in the m greater than or equal to n case, and state that the m less than or equal to n case follows by analogous reasoning. Include that orthogonality of V gives VVᵀ = I, completing the factorization.
Why it matters: The final multiplication shows that the eigenvector-based construction actually reconstructs the original matrix A.
Key terms: singular value decomposition, AᵀA, AAᵀ, eigenvectors, eigenvalues, singular values, zero entries, Σ, extra eigenvectors, UΣ
4.5.3 Applying SVD: PCA computation
This subsection shows that SVD can compute PCA by forming the covariance matrix from a mean-subtracted, row-oriented data matrix and extracting its eigenstructure.
- Covariance from data matrix: Describe how a dataset is arranged in a matrix X for this derivation and explain why X^T X is its covariance matrix after the data has been mean-subtracted.
Why it matters: This matrix construction turns the spread of a dataset into a linear-algebra object that PCA can analyze. - SVD computation of PCA: Explain how PCA follows from SVD of a mean-subtracted data matrix: the eigenvalues and eigenvectors of X^T X are the principal components, so performing SVD on X yields the PCA of the data.
Why it matters: It provides a practical linear-algebra route to PCA without treating PCA as a separate procedure.
Key terms: data matrix, mean-subtracted, X^T X, covariance matrix, singular value decomposition, SVD, principal component analysis, PCA, eigenvalues, eigenvectors
4.5.4 Applying SVD: Solving arbitrary linear systems
This section shows how singular value decomposition solves square, overdetermined, and underdetermined linear systems more robustly than directly inverting the coefficient matrix.
- Limits of matrix inversion: Explain why directly solving a linear system with a matrix inverse can be undesirable or impossible. Include the numerical instability caused by a zero or near-zero determinant, and the fact that nonsquare systems do not have an ordinary inverse.
Why it matters: This explains why a seemingly direct algebraic solution is not reliable for many real machine learning problems. - SVD solution sequence: Trace how an SVD factorization A = UΣVᵀ turns a linear system into simple successive solves. Describe multiplying by Uᵀ using U's orthogonality, solving the diagonal Σ system, and multiplying by V using V's orthogonality to recover the solution.
Why it matters: SVD replaces one difficult or impossible matrix inversion with transformations and a diagonal solve that are easier to handle. - SVD solution outcomes: Distinguish the result of SVD-based solving for invertible square systems from its result for nonsquare systems. State that it matches the ordinary inverse solution for invertible square A, while for nonsquare A it corresponds to the Moore-Penrose inverse and gives a best-effort approximation.
Why it matters: This clarifies why one method can handle both exactly solvable square problems and systems that cannot be fully solved.
Key terms: matrix inversion, determinant, numerically unstable, overdetermined system, underdetermined system, linear combination, singular value decomposition, SVD, U, Σ
4.5.5 Rank of a matrix
This section defines matrix rank through independent rows or columns and relates rank to degeneracy, singular values, invertibility, and solving linear systems.
- Matrix rank definition: Explain that a matrix’s rank is the number of linearly independent rows, equivalently the number of linearly independent columns. Relate rank to the number of nonzero singular values.
Why it matters: Rank reveals how many genuinely independent constraints or directions a matrix contains. - Degeneracy and rank deficiency: Describe a degenerate linear system as one with dependent equations, such as an equation that can be formed by combining other equations. State that a non-full-rank matrix is degenerate, has a zero determinant in the square case, and cannot be inverted.
Why it matters: This lets you recognize when an apparent set of equations does not provide enough independent information for ordinary matrix inversion. - Full-rank matrix cases: Distinguish rectangular from square matrix cases: a rectangular m-by-n matrix has rank min(m,n) and is not invertible, whereas an n-by-n matrix is full rank and invertible exactly when its rank is n. Explain that SVD can solve rectangular systems and can also be preferable for full-rank square systems because inverse computation is not always numerically stable.
Why it matters: Choosing SVD appropriately avoids assuming that an inverse exists or that explicitly computing one is the best numerical method.
Key terms: rank, linearly independent rows, linearly independent columns, singular values, degenerate, linearly dependent, zero determinant, noninvertible, full rank, rectangular matrix
4.5.6 PyTorch code for solving linear systems with SVD
This subsection points to PyTorch listings that implement SVD to solve invertible and overdetermined linear systems.
- SVD-based linear-system solution: Describe that the provided PyTorch listings use singular value decomposition (SVD) to solve linear systems, including an invertible system and an overdetermined system.
Why it matters: This connects SVD from a mathematical decomposition to a practical PyTorch method for solving systems of equations.
Key terms: singular value decomposition, SVD, linear system, overdetermined linear system, pseudo-inverse, PyTorch
4.5.7 PyTorch code for PCA computation via SVD
4.5.8 Applying SVD: Best low-rank approximation of a matrix
This section explains why and how truncating an SVD produces the best lower-rank approximation of a matrix under Frobenius-norm error.
- Low-rank approximation purpose: Explain why a data matrix may be replaced by a lower-rank matrix: to discard small-variance components that are likely noise and retain the pattern represented by large variations. State that this approach may fail when the underlying pattern is nonlinear.
Why it matters: Low-rank approximation is useful for revealing dominant structure, but it is not a remedy for nonlinear structure. - Frobenius approximation error: Describe the error of a rank-r approximation Ar to A as the Frobenius norm of their difference, e = ||A − Ar||F. Explain that the best approximation is the rank-r matrix with the smallest such error.
Why it matters: The Frobenius norm gives a precise meaning to what “best” low-rank approximation means. - Truncated SVD approximation: Trace how to construct the best rank-r approximation of a rank-p matrix A when r < p: sort the singular values in decreasing order, partition U, Σ, and V, and retain the r-part factors to form U1Σ1V1T. State that this product has rank r and is the best rank-r approximation under Frobenius-norm error.
Why it matters: Truncated SVD gives a direct, principled way to reduce a matrix while losing as little information as possible under the stated error measure.
Key terms: data matrix, lower-rank matrix, small variances, noise, nonlinear pattern, Frobenius norm, approximation error, rank-r approximation, e = ||A − Ar||F, singular value decomposition
4.6 Machine learning application: Document retrieval
This section frames document retrieval as matching a query to a small document collection using a bag-of-words representation.
- Document retrieval task: Describe the document retrieval problem as receiving a query phrase and returning documents from a collection that match that query.
Why it matters: This defines the practical machine learning problem that the following retrieval methods address. - Bag-of-words matching: Explain the bag-of-words model used for matching: it represents a document by how often its words occur and ignores where those words appear in the document.
Why it matters: Bag-of-words gives the simple document representation on which the section's retrieval example is built. - Toy corpus purpose: State that the section uses a small illustrative collection, d0 through d6, to demonstrate document retrieval, and that bag of words is chosen for conceptual simplicity rather than sophistication.
Why it matters: Recognizing this as a toy example helps distinguish the teaching setup from a claim that bag of words is the most advanced retrieval method.
Key terms: document retrieval, query phrase, documents, bag of words model, matching approach, word appears, document, toy example, conceptual simplicity
4.6.1 Using TF-IDF and cosine similarity
This section explains document retrieval by representing documents as sparse TF-IDF word vectors and comparing them with cosine similarity.
- Term frequency definition: Define term frequency (TF) as a term's number of occurrences in a document, including the looser use of a quantity proportional to that count. Describe the normalization choices that treat case variants and usually singular, plural, and stem-related forms as the same term.
Why it matters: Term frequency supplies the basic evidence that a document emphasizes a particular word or term. - Inverse document frequency weighting: Explain that inverse document frequency (IDF) down-weights terms appearing in most documents by taking the inverse and absolute logarithm of the fraction of documents containing the term. Contrast the low IDF of ubiquitous terms such as "the" with the high IDF of relatively esoteric terms.
Why it matters: IDF prevents universally common words from dominating retrieval results and gives distinctive terms more influence. - Sparse TF-IDF document vectors: Describe how a document becomes a feature vector with one fixed coordinate per vocabulary word, where each coordinate equals that word's TF multiplied by its IDF. Explain that the vocabulary-sized vector is conceptually high-dimensional but is stored sparsely as the document's unique words and their TF×IDF scores.
Why it matters: This representation converts documents into points that can be compared mathematically without explicitly storing enormous mostly-zero vectors. - Cosine document similarity: Explain cosine similarity as the dot product of two document vectors divided by the product of their lengths, so it measures directional alignment rather than raw vector magnitude. Relate a larger cosine to greater weighted term sharing, with cosine 1 for vectors pointing in the same direction and cosine 0 for perpendicular vectors with no correlation.
Why it matters: Cosine similarity can rank documents by shared weighted terminology without automatically favoring long documents.
Key terms: term frequency, TF, term, word, inverse document frequency, IDF, down-weight, common terms, document feature vector, vocabulary
4.6.2 Latent semantic analysis
This section explains how latent semantic analysis uses PCA/SVD-derived topics to compare documents through indirect term relationships rather than only shared words.
- Cosine similarity blind spot: Explain why cosine similarity on original document-term vectors can rate semantically related documents as unrelated when they contain no exact overlapping terms. Describe how co-occurrence evidence from other documents, such as gun and violence appearing together, reveals an indirect relationship that direct term overlap misses.
Why it matters: This explains why document retrieval needs a semantic representation instead of relying only on matching words. - Topics as principal components: Describe an LSA topic as a high-variance linear subspace direction in document feature space and as a weighted combination of vocabulary terms. Explain that topics are principal components chosen because they discriminate documents by how strongly documents vary along them.
Why it matters: Topics replace individual-word coordinates with meaningful shared patterns that can reveal latent semantic similarity. - SVD topic-space reduction: Trace the LSA procedure: form an m-by-n document-term matrix X with documents as rows and terms as columns, perform SVD, and use columns of V as topic vectors. Describe retaining topics with sufficiently large singular-value weights, projecting X onto the retained topic vectors, and computing cosine similarity between the resulting document-topic rows.
Why it matters: This is the practical transformation that turns raw term vectors into lower-dimensional representations suitable for semantic document retrieval.
Key terms: cosine similarity, document vectors, direct overlap, latent semantic similarities, term co-occurrence, topic, document feature space, linear subspace, principal components, high variance
4.6.3 PyTorch code to perform LSA
This subsection points to a PyTorch listing that computes latent semantic analysis (LSA) for the chapter's toy document dataset.
- PyTorch LSA computation: Describe that Listing 4.8 provides a working PyTorch implementation for computing LSA on the toy dataset introduced in table 4.1, with complete runnable code available through the supplied link.
Why it matters: This gives you an executable implementation of the LSA method discussed in the preceding subsection.
Key terms: latent semantic analysis, LSA, PyTorch, toy dataset
4.6.4 PyTorch code to compute LSA and SVD on a large dataset
This subsection demonstrates PyTorch SVD-based LSA on a large synthetic document-term dataset and interprets its principal components and singular values.
- Synthetic document-topic structure: Describe how the 500-by-3 document-term matrix is constructed and how its term-frequency pattern represents two topics. State that x0 and x1 are correlated because x1 occurs about twice as often as x0, whereas x2 varies independently over a much smaller range.
Why it matters: Recognizing correlation structure in document vectors explains why LSA can uncover latent topics. - Singular-value component interpretation: Explain how the SVD results reveal the expected principal components: one along the x0-x1 topic and one along the x2 topic. Describe why the third, much smaller singular value can be ignored: it represents only small random variation within the x0-x1 topic.
Why it matters: The relative sizes of singular values indicate which latent directions carry meaningful structure and which can be discarded. - Large dataset LSA workflow: Trace the demonstrated PyTorch workflow: create the synthetic dataset, compute its SVD, plot the document vectors, display the first two principal components, and print and graph the singular values.
Why it matters: This workflow connects the numerical SVD output with a visual interpretation of latent topics and insignificant variation.
Key terms: document vector, data matrix X, vocabulary, x0, x1, x2, singular values, principal components, SVD, dimensionality
4.7 Summary
This summary recaps how quadratic forms, PCA, SVD, and document-vector methods reveal important structure in machine-learning data.
Where this is used later
- Linear latent space manifolds and PCA (chapter 14) · PCA is used as a model of linear latent-space manifolds.
- PyTorch code for dimensionality reduction using PCA (chapter 14) · It applies PCA for dimensionality reduction in PyTorch.
Concepts explained in this chapter
- principal component analysisA multivariate statistical technique that finds orthogonal directions of maximal spread in data using covariance-matrix eigenvectors and eigenvalues.
- singular value decompositionThe statement that any matrix can be decomposed as a product of orthogonal matrices and a diagonal matrix in singular value decomposition form.
- embeddingAn ordered mathematical object representing a point or direction in a coordinate space.
- projectionA transformation that maps points from a higher-dimensional space to a lower-dimensional one by discarding coordinates or components.
- principal componentThe projection values of data onto a principal axis in PCA, representing coordinates along that direction.
- similarity searchThe task of finding documents relevant or similar to a query or another document using feature representations.
- quadratic formA homogeneous degree-two expression, here written with a symmetric matrix as x^T A x or equivalent forms describing geometric shapes and curvature.
- cosine similarityA dot product computed on unit-length vectors so that the score is insensitive to vector length.
- dimensionality reductionThe reduction of the effective number of dimensions in a representation by a transformation such as a singular weight matrix.
- arrayAn indexed sequence of values, here used to represent a signal processed by a convolution.
- covariance matrixA matrix summarizing variances and covariances among dimensions.
- latent semantic analysisA document-retrieval technique that uses low-dimensional structure in document-term data to capture latent topics and relationships.
- principal axisAn axis in PCA aligned with a principal component, used as the direction along which data spread is measured.
- varianceA scalar measure of how dispersed a set of values is around their mean, used here as the quantity assumed known in one conjugate-prior case.
- unit vectorA vector whose length is exactly 1, used here to restrict the optimization to direction rather than magnitude.
- term frequency-inverse document frequencyA document-representation weighting scheme that combines term frequency with inverse document frequency to emphasize discriminative words.
- singular valueA nonnegative quantity from singular value decomposition that measures how much a matrix stretches along an orthogonal direction.
- S / ATA / A^T A / A A^TThe matrix product of A transpose and A, used here as the matrix whose eigenvalues are the singular values mentioned in the section.
- document-term matrixA matrix with documents as rows and terms as columns, storing term-frequency values for retrieval or analysis.
- semantic similaritySimilarity of meaning between terms or documents, as opposed to exact string overlap.
- topicA latent group of semantically related terms represented as a direction or subspace in document feature space.
- topic spaceThe reduced vector space whose coordinates are topic dimensions rather than original terms.
- topic vectorA vector whose entries indicate how strongly each term contributes to a topic.
- feature spaceThe set of all possible input values represented geometrically as the space in which data points lie.
- inner productA scalar computed from two vectors by multiplying corresponding components and summing them, used here to measure one vector's projection along another.
- similarity metricA measure of how alike two represented items are, especially when computed from their vector representations.
- meanThe expected value or center parameter of a probability distribution, especially a Gaussian distribution.
- eigenvalueA scalar associated with a linear transformation that gives the scaling factor for a corresponding eigenvector.
- eigenvectorA nonzero vector that is scaled without changing direction by a linear transformation.
- standard deviationThe square root of variance, used as a spread measure in the same units as the underlying values.
- independenceA variable that is statistically unrelated to another variable, with their joint probability equal to the product of their marginal probabilities.
- linear systemA set of linear equations considered together, typically represented in matrix form and solved for unknown variables.
- pseudo-inverseA pseudoinverse is a generalized matrix inverse that provides least-squares solutions when an ordinary inverse does not exist.
- high-dimensional dataA dataset whose instances are represented with many dimensions or features, making it suitable for dimensionality-reduction methods.
- probability theoryA measure of how likely an event or value is.
- PyTorchAn open-source machine learning library used here to provide the chapter's executable code examples and automatic gradient computation.
- l2 normThe Euclidean norm measuring a vector's length by squaring components, summing them, and taking the square root.
- principal valueA scalar associated with a principal axis in this PCA discussion, where smaller values indicate directions that can be discarded with little information loss.
- objective functionA function assigning a scalar penalty to model parameters or predictions.
- diagonal matrixA square matrix whose only possibly nonzero entries are on the main diagonal.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
