Math for AI course · Lesson 33 of 180
Matrix Rank Counts Independent Information in AI Features
Calculate matrix rank from pivots or features, connect row and column views, and use singular values to spot redundant data features.
A dataset can have three feature columns and still contain only two independent directions of variation. That difference matters when a model tries to assign separate effects to features that always move together. Matrix rank counts the independent directions in a matrix, whether we inspect its feature columns or its sample rows.
By the end, you will be able to compute a matrix’s rank by finding pivots, explain why row and column perspectives give the same number, and read a singular-value preview without treating every small value as exactly zero.
Learning outcome: Compute rank and connect row and column perspectives.
This lesson brings together the row-echelon form and dimension ideas: pivots identify independent directions, and rank counts them. Section 2.6.2 of Mathematics for Machine Learning defines rank through independent rows and columns. The data matrix, feature interpretation, singular-value chart, calculations, and exercises here are original.
Three columns can carry only two directions
Imagine four sessions described by minutes on a mobile app, minutes on a website, and total active minutes. Store each session as a row and each measurement as a column:
The third feature is calculated as mobile time plus website time in every row:
So the three columns are dependent. The total column is useful as a named measurement, but it adds no new linear direction to this particular matrix. The first two columns are independent: in row three, only the website column is nonzero; then row one rules out a nonzero multiple of the mobile column. Thus the column space has dimension two.
Here sessions and features, so rank can be at most three. We have already shown that the three columns depend on one another while the first two do not, giving .
Pivots calculate rank from rows
For a linear system, a row operation must also be applied to its right-hand side to preserve the solutions. On this feature matrix, elimination preserves rank and reveals the pivots. First replace row 2 with row 2 minus twice row 1, and row 4 with row 4 minus row 1. Then subtract row 3 from the new row 4 and move the nonzero third row into the second position:
There are two nonzero rows and two pivot columns, so the rank is two. Columns one and two of the original matrix form a basis for its column space; the pivot in column three would be absent because that column is the sum of the first two.
The row view reaches the same count directly. Write the sample rows as . The second row equals , and the fourth equals . Rows one and three are independent, so the row space also has dimension two. In general, this equality of row rank and column rank holds for every finite matrix, including rectangular ones; transposing swaps the two spaces but cannot change how many independent directions they contain.
Singular values preview the directions
For noisy or large matrices, software commonly estimates rank from a singular value decomposition (SVD). The SVD represents a matrix as , where the diagonal entries of are nonnegative singular values sorted from largest to smallest. Each value tells how strongly the matrix acts along a paired direction. An exact zero means that direction is absent.
For this example, the singular values are approximately , , and zero (up to floating-point roundoff). Dividing by the largest value makes a scale-free preview: , , and approximately . The third bar vanishes because the total feature is an exact sum. The second direction is smaller than the first, but it is still nonzero. These ratios are not percentages of variance explained: singular values show scale, while their squares are eigenvalues of . For centered data, those squares track variance along the corresponding directions, up to a shared normalization.
Singular-value preview
Two directions remain in the feature matrix
Each bar shows a singular value divided by the largest one. The third direction is zero up to floating-point roundoff because total time is the sum of the other features.
- First direction1.000
- Second direction0.059
- Third direction≈ 0
The chart is a preview of one matrix, not a universal cutoff. NumPy’s
matrix_rank
counts singular values above a tolerance. By default, that tolerance scales
with the largest singular value, the matrix’s largest dimension, and the
floating-point precision of the data type. This handles roundoff in exact
dependencies; a different tolerance may be more suitable when measurement
noise is larger than floating-point error. The
svd
routine returns singular values in descending order.
import numpy as np
X = np.array([
[1, 2, 3],
[2, 4, 6],
[0, 1, 1],
[1, 3, 4],
], dtype=float)
singular_values = np.linalg.svd(X, compute_uv=False)
rank_columns = np.linalg.matrix_rank(X)
rank_rows = np.linalg.matrix_rank(X.T)
print(X.shape) # (4, 3)
print(np.round(singular_values, 3)) # [9.882 0.581 0. ]
print(rank_columns, rank_rows) # 2 2
Floating-point SVD may return a tiny positive number where exact arithmetic
gives zero. NumPy’s tolerance classifies that value as zero for the rank
estimate. The displayed 0. is rounded output, not a claim that every
computer’s computed singular value is bit-for-bit zero.
Rank identifies redundant model weights
An intercept-free linear model with weights predicts the four session outcomes as . Let the weights be for mobile, web, and total time. Since total time is the sum of the first two features,
Only two combined weights affect predictions, even though the model stores three. For any number , replacing with leaves every prediction unchanged. On this dataset, rank two therefore means that the three coefficients cannot be uniquely identified from the predictions alone. Regularization can select one set of weights, but the rank calculation by itself does not say which feature deserves a causal or semantic interpretation.
That is the AI application: rank measures effective linear feature diversity in a data matrix. It can expose redundant derived features before they complicate coefficient interpretation or least-squares fitting. The number does not measure all information in a dataset, predict model accuracy, or capture nonlinear relationships. Changing the data rows, feature definitions, or preprocessing can change the rank.
Near dependence needs a stated tolerance
The constructed total is an exact sum. Real measurements may instead satisfy . An exact mathematical rank could then be three, even when the smallest singular value is tiny. A numerical rank may count that direction as negligible when it falls below a chosen tolerance. Report the tolerance and the data scale when that distinction affects a decision.
Feature scaling also changes singular-value sizes. Multiplying a feature column by a nonzero constant preserves exact rank, but it can alter the singular-value ratios used for a numerical or approximate-rank judgment. Standardization is therefore not cosmetic when interpreting small singular values. Rank describes linear dependence in the represented matrix under the chosen encoding; it does not award a fixed amount of semantic information to each column.
When a matrix has feature columns but only independent directions, the row-echelon form shows which columns can serve as a basis, while the singular values show how strong those directions appear numerically. The next step is to ask what a matrix maps to and which input directions it loses: the kernel and image make those two questions explicit.
Check your understanding
Question 1
For a 5 × 3 matrix, what is the largest possible rank, and what does a rank of 2 imply about its columns and rows?
Show the step-by-step solution
The largest rank is . If the rank is two, the column space has dimension two, so only two columns can be independent and at least one of the three columns is a combination of the others. The row space also has dimension two, meaning only two of the five rows are independent. It does not mean the matrix has only two entries or two records.
Question 2
In the session matrix, verify the column relation that makes one feature redundant. Which two columns are independent?
Show the step-by-step solution
Read the columns as vectors: , , and . Component by component, , so the three are dependent. The first two are independent: a combination has third coordinate , then first coordinate . They span a two-dimensional column space.
Question 3
The second row is twice the first, and the fourth is the sum of the first and third. Why does this give a row rank of 2?
Show the step-by-step solution
Those two equations show rows two and four add no new direction. Rows one and three are independent: if , the first coordinate gives , and then the second coordinate gives . Thus two independent rows form a basis for the row space, so its dimension is two.
Question 4
After row reduction, a matrix has three nonzero rows and pivot columns 1, 2, and 4. What is its rank, and which original columns form a basis for the column space?
Show the step-by-step solution
The number of pivots is three, so the rank is three. To obtain a basis for the original column space, take columns 1, 2, and 4 from the original matrix, not from its row-reduced version. Row operations reveal which original columns are independent, but row operations can change the columns themselves.
Question 5
Why might `np.linalg.svd` return a tiny positive third singular value for the exact feature relation in this lesson, while `np.linalg.matrix_rank` returns 2?
Show the step-by-step solution
Floating-point arithmetic rounds intermediate calculations, so the SVD may
not produce a bit-for-bit zero even when the input columns are exactly
dependent as stored. matrix_rank compares singular values with a tolerance;
the tiny value falls below NumPy’s default threshold and is counted as zero.
The estimate is numerical and depends on the tolerance and data type.
Question 6
Show why infinitely many coefficient triples produce the same predictions when total time equals mobile time plus web time.
Show the step-by-step solution
The prediction vector is , which simplifies to . For any , the replacement keeps both combined weights the same: and similarly for web. Therefore predictions do not change, and the original three weights are not uniquely determined by these predictions.
Question 7
A matrix has singular values 8, 0.4, and 0.001. Does that prove its exact rank is 2? What extra information would help decide a numerical rank?
Show the step-by-step solution
No. All three singular values are positive, so if those were exact values the exact rank would be three. The small third value may be treated as negligible for an approximate or numerical rank, but the decision needs a tolerance related to matrix scale, floating-point precision, measurement noise, and the downstream task. The ratios alone do not establish a universal cutoff.
Question 8
If two features are exactly proportional in the training matrix, can rank alone tell you that one is useless for every future prediction? Explain the scope.
Show the step-by-step solution
Rank describes linear dependence among the feature columns in the matrix that was analyzed. The proportionality may come from a formula that also holds for future records, in which case one column adds no new linear direction. Or it may be an accident of a narrow sample and fail on new data. Rank alone does not establish which case applies, and it says nothing by itself about nonlinear usefulness or predictive accuracy.