Math for AI course · Lesson 22 of 180
Identity and Inverse Transformations: Undoing a Feature Map
Use identity matrices and a small inverse to reverse a 2D feature transform, detect unsafe cases, and understand why solve beats explicit inversion.
Suppose a preprocessing step mixes two features before they enter an AI model. Later, you want to recover the original coordinates for debugging, a report, or another model. How can you tell whether the transformation is genuinely reversible, and how can you undo it without making a fragile numerical choice?
By the end of this lesson, you will be able to recognize identity behavior, compute and verify a small matrix inverse, distinguish an inverse from a transpose, identify singular and nearly singular cases, and explain why a linear system is usually solved directly instead of by explicitly forming an inverse.
Learning outcome: Recognize identity behavior and verify a small matrix inverse.
Lesson 20: Matrix-matrix products compose transformations and batches showed that a product can compose linear maps. The official Mathematics for Machine Learning companion develops identity, inverse, and transpose operations in Section 2.2 of the book PDF, then connects them to systems of equations in Section 2.3. The notation here follows that path, but the numbers, visual, preprocessing scenario, code, and questions are original.
Identity means “leave the coordinates alone”
For a two-dimensional column vector, the identity matrix is
Multiplying by leaves every coordinate unchanged. If , then
The diagonal ones preserve each coordinate, while the off-diagonal zeros stop one coordinate from contributing to the other. The identity is the matrix version of the number : multiplying by it changes nothing, but it gives us a precise target for checking whether another matrix has been undone.
The two sides matter. A matrix product is ordered, so a useful inverse must restore the identity when multiplied on either side, not merely make one selected vector look correct.
An inverse reverses a transformation
Let be a square matrix. An inverse, written , is a matrix that satisfies
If a feature vector first becomes , then multiplying by recovers the input:
This is a cancellation rule for matrices. It is not a claim that every matrix has an inverse. A matrix must preserve enough independent directions for an inverse to exist; we will see what fails when it does not.
| Point | Original x | After A: z = Ax | After A⁻¹: x = A⁻¹z |
|---|---|---|---|
| p₀ | (0, 0) | (0, 0) | (0, 0) |
| p₁ | (1, 0) | (2, 1) | (1, 0) |
| p₂ | (1, 1) | (3, 2) | (1, 1) |
| p₃ | (0, 1) | (1, 1) | (0, 1) |
The parallelogram is not lost information: its four corners remain in a one-to-one correspondence with the original corners because the determinant of this example is nonzero.
The visual uses
The unit square becomes a parallelogram under and returns to the same corners under . The table is the static, text-readable record of each corner correspondence. The shape stays one-to-one because the determinant of this is , not zero.
Worked example: invert a 2 × 2 matrix
For
the determinant is
When this number is nonzero, the 2 × 2 inverse formula is
The formula has two checks built into it: swap the diagonal entries, negate the off-diagonal entries, and divide by the determinant. The nonzero determinant is what makes that division legal.
We can also test the inverse by transforming one feature vector. Let
The forward map gives
The inverse returns it:
The matrix product check is stronger than this one-vector check. Many incorrect matrices can happen to undo one particular vector; only the identity equations show that the transformation has been reversed for every vector in the space.
An inverse is not a transpose
The transpose swaps rows and columns:
That is an axis reorientation. The inverse solves the different requirement . They can be equal in special cases, but one operation cannot be substituted for the other in general.
For the worked matrix, happens to be symmetric, so . But its inverse is
which is clearly not . Symmetry alone does not make a matrix orthogonal or make its transpose an inverse.
Here is a nonsymmetric counterexample:
The transpose preserves the entries but changes their orientation. The inverse changes signs and combines coordinates so that the two matrix products become the identity. Only when —for example, for a pure rotation or reflection—does .
AI application: reverse a learned feature coordinate change
Many pipelines work with a centered feature vector rather than the raw feature values. Let be the raw two-feature vector and be a fixed mean vector. A linear preprocessing step can be written as
The matrix mixes and rescales the centered coordinates. This can be useful when a later model works better in a rotated or otherwise re-expressed feature system. If is invertible and the same is retained, the raw vector can be recovered with
The inverse is not a magic undo button for an entire preprocessing pipeline. It reverses this particular linear map. A pipeline that then clips values, drops a feature, rounds to integers, or applies a non-injective operation has discarded information that no matrix inverse can reconstruct.
For example, take
First center the feature vector:
The transformed coordinates are , as in the worked example. Reversing them gives
The practical debugging habit is to record the axis meanings, the mean used for centering, and the exact matrix version together. An inverse from a newer preprocessing fit may be mathematically valid and still be the wrong inverse for an older batch.
Singular transformations lose distinctions
Consider
Its determinant is
The second row is twice the first, so has only one independent direction. It also maps different inputs to the same output:
Once those two inputs collide, an output-only observer cannot know which input
was used. An inverse would have to send one output to two different vectors,
which is impossible for a function. NumPy reports this failure as a
LinAlgError when numpy.linalg.inv or numpy.linalg.solve is asked to work
with a singular square matrix; see the inverse
documentation
and solve documentation.
Singularity is not only a software error. It can mean that a feature map has collapsed a direction, two columns are redundant, or a model’s representation does not contain enough information for the requested reconstruction. A pseudoinverse or a least-squares estimate may still be useful for a different question, but it is not the exact two-sided inverse of .
Nearly singular is a numerical warning, not a success
Now change one entry by a very small amount:
Its determinant is approximately , so it is technically invertible.
But its rows are almost parallel. Small measurement or rounding changes can
produce large changes in the recovered vector. A condition number summarizes
this sensitivity; in the usual 2-norm, a value near means that
many digits of input accuracy may be consumed by the solve. NumPy exposes this
diagnostic as numpy.linalg.cond.
The distinction is important:
| Matrix state | Determinant test | Practical interpretation |
|---|---|---|
| Nonsingular and well-conditioned | nonzero, not tiny relative to scale | An inverse or solve can be dependable for the data precision available. |
| Singular | exactly zero in exact arithmetic | Distinct inputs may have collapsed to one output; no ordinary inverse exists. |
| Nearly singular | nonzero but small relative to scale | An inverse exists on paper, but input noise can be strongly amplified. |
Determinant alone is not a universal scale-free safety threshold. Multiplying a matrix by a large scalar also changes its determinant, so inspect a condition number, singular values, residuals, and the scale of the application. A nonzero floating-point determinant is evidence of invertibility for that representation, not a guarantee of a trustworthy reconstruction.
Why solve is preferred to building an explicit inverse
The equation
has the mathematical solution when
is invertible. In code, however, if all you need is , forming
the full first is unnecessary. numpy.linalg.solve
accepts and directly and computes the solution of the
system; numpy.linalg.inv
constructs the inverse matrix itself and warns that ill-conditioned results may
be inaccurate.
The preferred implementation pattern is therefore:
import numpy as np
A = np.array([[2.0, 1.0],
[1.0, 1.0]])
b = np.array([2.0, 1.0])
x_from_solve = np.linalg.solve(A, b)
x_from_inverse = np.linalg.inv(A) @ b
np.testing.assert_allclose(x_from_solve, [1.0, 0.0])
np.testing.assert_allclose(x_from_inverse, x_from_solve)
np.testing.assert_allclose(A @ x_from_solve, b)
Both paths agree for this small, well-conditioned example. The important
engineering difference is that solve computes the requested solution without
materializing a matrix that you may never use. It also keeps the system-solving
algorithm explicit, so the code communicates the real task: solve .
If several right-hand sides share the same , pass them as columns in a
matrix and solve in one call. If is singular, solve is the
wrong tool for an exact solution; choose a method such as least squares only
when its different objective matches the application. Always check a residual:
A small residual in the problem’s units is evidence that the computed vector solves the supplied system. It does not, by itself, prove that the system was well-conditioned or that the matrix represented the right preprocessing step.
The next lesson will make the solving step itself explicit with row operations. For now, identity is the target, inverse is the reversible map, transpose is an axis swap, and conditioning tells us whether a mathematically legal reversal is numerically sensible.
Check your understanding
Question 1
What does I₂ do to the vector [4, −3]ᵀ, and why?
Show the step-by-step solution
Step 1: write the identity.
Step 2: multiply row by column.
Step 3: interpret it. The diagonal ones keep each coordinate and the zeros prevent cross-coordinate mixing, so the identity leaves the vector unchanged.
Question 2
Find the inverse of A = [[1, 2], [3, 7]] by using the 2 × 2 formula.
Show the step-by-step solution
Step 1: identify the entries. Here , , , and .
Step 2: compute the determinant.
The determinant is nonzero, so the inverse exists.
Step 3: swap and negate.
The division by the determinant does not change the entries in this particular example because the determinant equals .
Question 3
Verify the inverse from Question 2 by calculating A A⁻¹.
Show the step-by-step solution
Use
Step 1: compute each entry.
Step 2: compare with the target. The result is , so the proposed matrix passes the left-product check. Because this is a square inverse and the formula produced the matching two-sided inverse, it also reverses the matrix on the other side; computing is an additional useful code check.
Question 4
For B = [[1, 2], [0, 1]], is Bᵀ a valid inverse? Show enough multiplication to decide.
Show the step-by-step solution
Step 1: write the transpose.
Step 2: multiply B by its transpose.
Step 3: compare with the identity. This is not , so is not the inverse. In fact, ; transpose and inverse answer different questions.
Question 5
A preprocessing step uses z = A(x − μ), with μ = [10, 4]ᵀ, A = [[2, 1], [1, 1]], and z = [8, 5]ᵀ. Recover x.
Show the step-by-step solution
Step 1: use the inverse form of the preprocessing equation.
Step 2: use the already verified inverse.
Step 3: restore the mean.
Forgetting would recover centered coordinates, not the original feature values.
Question 6
Why does S = [[1, 2], [2, 4]] have no ordinary inverse? Give a geometric or information-based explanation.
Show the step-by-step solution
Step 1: check the determinant.
The zero determinant means the rows and columns do not contain two independent directions.
Step 2: exhibit the information loss.
Two different inputs produce one output. A function that receives only that output cannot decide which input to return, so no two-sided inverse exists.
Question 7
A matrix has a nonzero determinant but a condition number of about 4 × 10⁸. Is it safe to claim that inversion is harmless? Explain.
Show the step-by-step solution
Step 1: separate existence from sensitivity. A nonzero determinant says the matrix is invertible in exact arithmetic. It does not say the inverse is stable under rounded or noisy inputs.
Step 2: interpret the condition number. A condition number near indicates that the matrix is nearly singular: small relative changes in the input can be amplified by roughly many orders of magnitude in a recovered solution.
Step 3: choose a conclusion. Do not call inversion harmless. Inspect the data precision, residuals, singular values or condition number, and the application’s tolerance. A mathematically valid inverse may still be a poor numerical reconstruction.
Question 8
Rewrite x = A⁻¹b as NumPy code when you only need x, and name one verification to run afterward.
Show the step-by-step solution
Step 1: express the direct task. The equation is , so ask NumPy to solve that system:
x = np.linalg.solve(A, b)Step 2: explain the choice. This avoids constructing np.linalg.inv(A)
when the inverse matrix itself is not needed. It also states the intended
operation directly.
Step 3: verify the result. Compute the residual and compare it with the right-hand side:
np.testing.assert_allclose(A @ x, b)This checks that the returned vector solves the supplied system within a tolerance. Also check conditioning when the input may be nearly singular.