{"course":{"slug":"maths-you-need","title":"The Maths You Actually Need","url":"https://addaly.com/learn/maths-you-need","level":"intermediate","track":"deep"},"lesson":{"slug":"singular-values-and-svd","title":"Singular values, and the honest measure of importance","url":"https://addaly.com/learn/maths-you-need/singular-values-and-svd","position":18,"of":76,"minutes":10,"answer":"Every matrix without exception factors into a rotation, a set of axis stretches, and another rotation, and the sizes of those stretches tell you exactly how much you lose by throwing each one away.","keyIdea":"Every matrix without exception factors into a rotation, a set of axis stretches, and another rotation, and the sizes of those stretches tell you exactly how much you lose by throwing each one away."},"body":{"format":"markdown","text":"## The factorisation that always exists\n\nEigenvectors need a square matrix and still sometimes fail. The singular value decomposition has no such conditions. **Every** matrix (rectangular, singular, whatever) can be written as\n\n```\nA = U S V^T\n```\n\nwhere U and V are orthogonal (rotations, possibly with a reflection) and S is diagonal with non-negative entries down it. Those diagonal entries are the **singular values**, conventionally listed largest first.\n\nThe geometric reading is clean and worth memorising: any linear transformation, however complicated it looks, is **rotate, stretch along the new axes, rotate again**. That is all a matrix can do. The singular values are the stretch factors.\n\nFor an `m × n` matrix, there are `min(m, n)` singular values. The number of non-zero ones is the rank. The largest divided by the smallest is the condition number. Three concepts from earlier lessons all fall out of one decomposition.\n\n## Reading the decay\n\nThe practically useful thing is not the decomposition but the *list* of singular values, because it tells you how much of the matrix lives in how few directions.\n\n```python\nimport numpy as np\ns = np.linalg.svd(W, compute_uv=False)\nenergy = np.cumsum(s**2) / np.sum(s**2)\nprint(\"dims for 90% of the energy:\", np.searchsorted(energy, 0.90) + 1)\n```\n\nSquared singular values sum to the total squared magnitude of the matrix, so the cumulative fraction tells you what proportion you keep. If 90 per cent sits in the first 50 of 768 directions, the matrix is close to rank 50 and you can compress it hard. If you need 700 directions to reach 90 per cent, you cannot, and no amount of tuning will change that.\n\n> Figure: Two layers, and how much of each lives in how few directions. Line chart of Share of the squared magnitude kept, % against Singular values kept, out of 768: A layer that compresses: 90 per cent by 60 directions (10, 52) (25, 71) (50, 86) (60, 90) (100, 95) (200, 98.5) (400, 99.7) (768, 100); A layer that does not: 90 per cent needs 620 (10, 7) (50, 22) (100, 36) (200, 56) (400, 78) (620, 90) (768, 100). Four lines of NumPy produce this before you attempt any compression. The first layer can be replaced by two thin ones for a known error; the second cannot, and no amount of tuning changes that.\n\nThis is the check to run *before* attempting low-rank compression, not after it disappoints.\n\n## Truncation, and the guarantee behind it\n\nKeep only the largest `k` singular values, zero the rest, and multiply back. The result is the best possible rank-`k` approximation of the original matrix, in the sense of squared error. This is the Eckart–Young theorem, and it is unusual in being both a real theorem and directly practical: no cleverer rank-k approximation exists, so if truncated SVD is not good enough, low-rank approximation is not good enough.\n\nThe error is exactly computable in advance:\n\n```\nsquared error of rank-k truncation = sum of the squares of the discarded singular values\n```\n\nYou know what compression will cost before you compress. That is rare, and it is why SVD is the right first tool.\n\nWorked, in storage terms. A 4096 × 4096 matrix truncated to rank 256:\n\n```\noriginal:  4096 * 4096       = 16,777,216 numbers\nrank 256:  4096*256 + 256 + 256*4096 = 2,097,408 numbers\nsaving:    8x, and the error is the tail of the singular values\n```\n\n## Where SVD earns its place\n\n- **PCA is SVD.** Centre your data matrix and take its SVD; the right singular vectors are the principal components and the squared singular values are proportional to the variance explained. Any implementation of PCA you will use calls an SVD routine underneath, because doing it that way avoids forming the covariance matrix and is numerically better.\n- **Latent semantic analysis**, the 1990s ancestor of embeddings, is a truncated SVD of a term–document matrix. The idea that meaning lives in a low-dimensional space of a big sparse count matrix is thirty years older than the transformer.\n- **Least squares, robustly.** `np.linalg.lstsq` uses SVD, which is why it returns a sensible answer even when the closed form would divide by something close to zero. The pseudoinverse is defined by inverting only the non-negligible singular values.\n- **Recommenders.** Matrix factorisation for ratings is SVD's idea, adapted because the actual matrix is mostly missing and plain SVD needs a complete one.\n- **Diagnosing a trained model.** The singular value spectrum of a layer tells you whether that layer is using its full width. A rapidly decaying spectrum is a layer that could be smaller.\n\n## The cost, and how to avoid paying it\n\nA full SVD of an `n × n` matrix costs about `O(n^3)`. For 4096 that is roughly 7 × 10^10 operations: seconds on a laptop, not free but affordable. For a 50,000 × 768 embedding matrix, a full SVD is wasteful when you only want the top 100 components.\n\nThe answer is a truncated or randomised SVD, which computes only the leading components and costs a fraction. `sklearn.decomposition.TruncatedSVD` and `scipy.sparse.linalg.svds` both do this and both work on sparse matrices, which full SVD cannot. All free, all CPU.\n\n## The limitation to state plainly\n\nSVD finds the best **linear** low-dimensional summary. If your data lies on a curved surface (a spiral, a sphere, most real embedding manifolds) the best linear approximation can be poor while a non-linear method does much better. That is why UMAP and t-SNE exist for visualisation and why autoencoders exist for compression.\n\nThere is a second, quieter limitation. SVD optimises squared reconstruction error, which is a statement about *fidelity to the numbers*, not about usefulness for your task. A direction with a small singular value can still be the one carrying the signal your classifier needs. Compress by SVD, then measure your actual task metric; do not assume that keeping 95 per cent of the energy keeps 95 per cent of the performance.\n\n## The rule to keep\n\nLook at the singular values before you decide anything about a matrix's size. They tell you the rank, the conditioning, and exactly what compression will cost, and they are one function call away."},"check":{"question":"Before compressing a 2048x2048 layer, an engineer computes its singular values and finds the cumulative squared energy reaches 90% only at component 1,900. What should they conclude?","options":[{"text":"The layer is well-conditioned, so truncation to rank 256 will be safe.","correct":false,"tempting":"Slow decay says the opposite of compressible, and conditioning is about the ratio of largest to smallest, not the decay profile."},{"text":"The singular values must be computed on the centred matrix, and the uncentred spectrum is misleading.","correct":false,"tempting":"Centring matters for PCA on data; a weight matrix is not data and is not centred before its SVD."},{"text":"The layer's behaviour genuinely spans nearly all its directions, so no rank-k truncation will approximate it well, and Eckart-Young says no other rank-k method will either.","correct":true},{"text":"They should try a randomised SVD instead, which will find a better low-rank approximation than the full one.","correct":false,"tempting":"Randomised SVD is an approximation of the same decomposition and is faster, never more accurate."}]},"prev":{"slug":"eigenvectors-plainly","title":"The directions a matrix does not turn","url":"https://addaly.com/learn/maths-you-need/eigenvectors-plainly"},"next":{"slug":"rates-of-change-before-derivatives","title":"Slope, before anyone says the word derivative","url":"https://addaly.com/learn/maths-you-need/rates-of-change-before-derivatives"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Singular values, and the honest measure of importance', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/singular-values-and-svd (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Singular values, and the honest measure of importance","courseTitle":"The Maths You Actually Need","module":{"index":2,"slug":"matrices-and-layers","title":"Matrices, and what a layer really does"},"answer":null,"keyIdea":"Every matrix without exception factors into a rotation, a set of axis stretches, and another rotation, and the sizes of those stretches tell you exactly how much you lose by throwing each one away.","hero":{"kind":"figure","figure":{"t":"figure","label":"Figure","figure":{"caption":"Four lines of NumPy produce this before you attempt any compression. The first layer can be replaced by two thin ones for a known error; the second cannot, and no amount of tuning changes that.","series":[{"label":"A layer that compresses: 90 per cent by 60 directions","points":[[10,52],[25,71],[50,86],[60,90],[100,95],[200,98.5],[400,99.7],[768,100]]},{"label":"A layer that does not: 90 per cent needs 620","points":[[10,7],[50,22],[100,36],[200,56],[400,78],[620,90],[768,100]]}],"title":"Two layers, and how much of each lives in how few directions","type":"line","xLabel":"Singular values kept, out of 768","yLabel":"Share of the squared magnitude kept, %"},"alt":"Figure: Two layers, and how much of each lives in how few directions. Line chart of Share of the squared magnitude kept, % against Singular values kept, out of 768: A layer that compresses: 90 per cent by 60 directions (10, 52) (25, 71) (50, 86) (60, 90) (100, 95) (200, 98.5) (400, 99.7) (768, 100); A layer that does not: 90 per cent needs 620 (10, 7) (50, 22) (100, 36) (200, 56) (400, 78) (620, 90) (768, 100). Four lines of NumPy produce this before you attempt any compression. The first layer can be replaced by two thin ones for a known error; the second cannot, and no amount of tuning changes that."}},"blocks":[{"t":"h2","id":"the-factorisation-that-always-exists","runs":[{"text":"The factorisation that always exists"}]},{"t":"p","runs":[{"text":"Eigenvectors need a square matrix and still sometimes fail. The singular value decomposition has no such conditions. "},{"text":"Every","b":true},{"text":" matrix (rectangular, singular, whatever) can be written as"}]},{"t":"code","lang":"","label":"Text","text":"A = U S V^T"},{"t":"p","runs":[{"text":"where U and V are orthogonal (rotations, possibly with a reflection) and S is diagonal with non-negative entries down it. Those diagonal entries are the "},{"text":"singular values","b":true},{"text":", conventionally listed largest first."}]},{"t":"p","runs":[{"text":"The geometric reading is clean and worth memorising: any linear transformation, however complicated it looks, is "},{"text":"rotate, stretch along the new axes, rotate again","b":true},{"text":". That is all a matrix can do. The singular values are the stretch factors."}]},{"t":"p","runs":[{"text":"For an "},{"text":"m × n","code":true},{"text":" matrix, there are "},{"text":"min(m, n)","code":true},{"text":" singular values. The number of non-zero ones is the rank. The largest divided by the smallest is the condition number. Three concepts from earlier lessons all fall out of one decomposition."}]},{"t":"h2","id":"reading-the-decay","runs":[{"text":"Reading the decay"}]},{"t":"p","runs":[{"text":"The practically useful thing is not the decomposition but the "},{"text":"list","i":true},{"text":" of singular values, because it tells you how much of the matrix lives in how few directions."}]},{"t":"code","lang":"python","label":"Python","text":"import numpy as np\ns = np.linalg.svd(W, compute_uv=False)\nenergy = np.cumsum(s**2) / np.sum(s**2)\nprint(\"dims for 90% of the energy:\", np.searchsorted(energy, 0.90) + 1)"},{"t":"p","runs":[{"text":"Squared singular values sum to the total squared magnitude of the matrix, so the cumulative fraction tells you what proportion you keep. If 90 per cent sits in the first 50 of 768 directions, the matrix is close to rank 50 and you can compress it hard. If you need 700 directions to reach 90 per cent, you cannot, and no amount of tuning will change that."}]},{"t":"p","runs":[{"text":"This is the check to run "},{"text":"before","i":true},{"text":" attempting low-rank compression, not after it disappoints."}]},{"t":"h2","id":"truncation-and-the-guarantee-behind-it","runs":[{"text":"Truncation, and the guarantee behind it"}]},{"t":"p","runs":[{"text":"Keep only the largest "},{"text":"k","code":true},{"text":" singular values, zero the rest, and multiply back. The result is the best possible rank-"},{"text":"k","code":true},{"text":" approximation of the original matrix, in the sense of squared error. This is the Eckart–Young theorem, and it is unusual in being both a real theorem and directly practical: no cleverer rank-k approximation exists, so if truncated SVD is not good enough, low-rank approximation is not good enough."}]},{"t":"p","runs":[{"text":"The error is exactly computable in advance:"}]},{"t":"code","lang":"","label":"Text","text":"squared error of rank-k truncation = sum of the squares of the discarded singular values"},{"t":"p","runs":[{"text":"You know what compression will cost before you compress. That is rare, and it is why SVD is the right first tool."}]},{"t":"p","runs":[{"text":"Worked, in storage terms. A 4096 × 4096 matrix truncated to rank 256:"}]},{"t":"code","lang":"","label":"Text","text":"original:  4096 * 4096       = 16,777,216 numbers\nrank 256:  4096*256 + 256 + 256*4096 = 2,097,408 numbers\nsaving:    8x, and the error is the tail of the singular values"},{"t":"h2","id":"where-svd-earns-its-place","runs":[{"text":"Where SVD earns its place"}]},{"t":"ul","items":[[{"text":"PCA is SVD.","b":true},{"text":" Centre your data matrix and take its SVD; the right singular vectors are the principal components and the squared singular values are proportional to the variance explained. Any implementation of PCA you will use calls an SVD routine underneath, because doing it that way avoids forming the covariance matrix and is numerically better."}],[{"text":"Latent semantic analysis","b":true},{"text":", the 1990s ancestor of embeddings, is a truncated SVD of a term–document matrix. The idea that meaning lives in a low-dimensional space of a big sparse count matrix is thirty years older than the transformer."}],[{"text":"Least squares, robustly.","b":true},{"text":" "},{"text":"np.linalg.lstsq","code":true},{"text":" uses SVD, which is why it returns a sensible answer even when the closed form would divide by something close to zero. The pseudoinverse is defined by inverting only the non-negligible singular values."}],[{"text":"Recommenders.","b":true},{"text":" Matrix factorisation for ratings is SVD’s idea, adapted because the actual matrix is mostly missing and plain SVD needs a complete one."}],[{"text":"Diagnosing a trained model.","b":true},{"text":" The singular value spectrum of a layer tells you whether that layer is using its full width. A rapidly decaying spectrum is a layer that could be smaller."}]]},{"t":"h2","id":"the-cost-and-how-to-avoid-paying-it","runs":[{"text":"The cost, and how to avoid paying it"}]},{"t":"p","runs":[{"text":"A full SVD of an "},{"text":"n × n","code":true},{"text":" matrix costs about "},{"text":"O(n^3)","code":true},{"text":". For 4096 that is roughly 7 × 10^10 operations: seconds on a laptop, not free but affordable. For a 50,000 × 768 embedding matrix, a full SVD is wasteful when you only want the top 100 components."}]},{"t":"p","runs":[{"text":"The answer is a truncated or randomised SVD, which computes only the leading components and costs a fraction. "},{"text":"sklearn.decomposition.TruncatedSVD","code":true},{"text":" and "},{"text":"scipy.sparse.linalg.svds","code":true},{"text":" both do this and both work on sparse matrices, which full SVD cannot. All free, all CPU."}]},{"t":"h2","id":"the-limitation-to-state-plainly","runs":[{"text":"The limitation to state plainly"}],"role":"limit"},{"t":"p","runs":[{"text":"SVD finds the best "},{"text":"linear","b":true},{"text":" low-dimensional summary. If your data lies on a curved surface (a spiral, a sphere, most real embedding manifolds) the best linear approximation can be poor while a non-linear method does much better. That is why UMAP and t-SNE exist for visualisation and why autoencoders exist for compression."}]},{"t":"p","runs":[{"text":"There is a second, quieter limitation. SVD optimises squared reconstruction error, which is a statement about "},{"text":"fidelity to the numbers","i":true},{"text":", not about usefulness for your task. A direction with a small singular value can still be the one carrying the signal your classifier needs. Compress by SVD, then measure your actual task metric; do not assume that keeping 95 per cent of the energy keeps 95 per cent of the performance."}]},{"t":"h2","id":"the-rule-to-keep","runs":[{"text":"The rule to keep"}]},{"t":"p","runs":[{"text":"Look at the singular values before you decide anything about a matrix’s size. They tell you the rank, the conditioning, and exactly what compression will cost, and they are one function call away."}]}],"check":{"question":[{"text":"Before compressing a 2048x2048 layer, an engineer computes its singular values and finds the cumulative squared energy reaches 90% only at component 1,900. What should they conclude?"}],"options":[{"text":[{"text":"The layer is well-conditioned, so truncation to rank 256 will be safe."}],"correct":false,"tempting":[{"text":"Slow decay says the opposite of compressible, and conditioning is about the ratio of largest to smallest, not the decay profile."}]},{"text":[{"text":"The singular values must be computed on the centred matrix, and the uncentred spectrum is misleading."}],"correct":false,"tempting":[{"text":"Centring matters for PCA on data; a weight matrix is not data and is not centred before its SVD."}]},{"text":[{"text":"The layer’s behaviour genuinely spans nearly all its directions, so no rank-k truncation will approximate it well, and Eckart-Young says no other rank-k method will either."}],"correct":true,"tempting":null},{"text":[{"text":"They should try a randomised SVD instead, which will find a better low-rank approximation than the full one."}],"correct":false,"tempting":[{"text":"Randomised SVD is an approximation of the same decomposition and is faster, never more accurate."}]}],"order":[0,3,1,2],"why":null},"prev":{"slug":"eigenvectors-plainly","title":"The directions a matrix does not turn","minutes":9},"next":{"slug":"rates-of-change-before-derivatives","title":"Slope, before anyone says the word derivative","minutes":8}}}