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.

Share this article

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

I2=[1001].I_2= \begin{bmatrix} 1&0\\ 0&1 \end{bmatrix}.

Multiplying by I2I_2 leaves every coordinate unchanged. If x=[x1,x2]T\boldsymbol{x}=[x_1,x_2]^{\mathsf T}, then

I2x=[1001][x1x2]=[x1x2]=x.I_2\boldsymbol{x} = \begin{bmatrix} 1&0\\ 0&1 \end{bmatrix} \begin{bmatrix}x_1\\x_2\end{bmatrix} = \begin{bmatrix}x_1\\x_2\end{bmatrix} =\boldsymbol{x}.

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 11: 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 AA be a square matrix. An inverse, written A−1A^{-1}, is a matrix that satisfies

AA−1=InandA−1A=In.AA^{-1}=I_n \qquad\text{and}\qquad A^{-1}A=I_n.

If a feature vector first becomes z=Ax\boldsymbol{z}=A\boldsymbol{x}, then multiplying by A−1A^{-1} recovers the input:

A−1z=A−1(Ax)=(A−1A)x=Inx=x.A^{-1}\boldsymbol{z} =A^{-1}(A\boldsymbol{x}) =(A^{-1}A)\boldsymbol{x} =I_n\boldsymbol{x} =\boldsymbol{x}.

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.

A reversible 2D feature transformation sends a unit square through A and returns it with A⁻¹. The tables name every corner so the diagram is not the only source of meaning.
Forward and inverse feature transformationA unit square with corners p zero through p three is mapped by matrix A to a parallelogram with transformed coordinates, then mapped back by the inverse matrix A inverse to the original square.original x-spacep₀p₁p₂p₃x₁x₂transformed z-spacep₀p₁p₂p₃z₁z₂restored x-spacep₀p₁p₂p₃x₁x₂
Corner coordinates under the forward map and its inverse
PointOriginal xAfter A: z = AxAfter 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

A=[2111],A−1=[1−1−12].A=\begin{bmatrix}2&1\\1&1\end{bmatrix}, \qquad A^{-1}=\begin{bmatrix}1&-1\\-1&2\end{bmatrix}.

The unit square becomes a parallelogram under AA and returns to the same corners under A−1A^{-1}. The table is the static, text-readable record of each corner correspondence. The shape stays one-to-one because the determinant of this AA is 11, not zero.

Worked example: invert a 2 × 2 matrix

For

A=[abcd],A=\begin{bmatrix}a&b\\c&d\end{bmatrix},

the determinant is

det⁡(A)=ad−bc.\det(A)=ad-bc.

When this number is nonzero, the 2 × 2 inverse formula is

A−1=1ad−bc[d−b−ca].A^{-1}=\frac{1}{ad-bc} \begin{bmatrix}d&-b\\-c&a\end{bmatrix}.

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

x=[32].\boldsymbol{x}=\begin{bmatrix}3\\2\end{bmatrix}.

The forward map gives

z=Ax=[2111][32]=[6+23+2]=[85].\boldsymbol{z}=A\boldsymbol{x} =\begin{bmatrix}2&1\\1&1\end{bmatrix} \begin{bmatrix}3\\2\end{bmatrix} =\begin{bmatrix}6+2\\3+2\end{bmatrix} =\begin{bmatrix}8\\5\end{bmatrix}.

The inverse returns it:

A−1z=[1−1−12][85]=[8−5−8+10]=[32].A^{-1}\boldsymbol{z} =\begin{bmatrix}1&-1\\-1&2\end{bmatrix} \begin{bmatrix}8\\5\end{bmatrix} =\begin{bmatrix}8-5\\-8+10\end{bmatrix} =\begin{bmatrix}3\\2\end{bmatrix}.

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 ATA^{\mathsf T} swaps rows and columns:

(AT)ij=Aji.(A^{\mathsf T})_{ij}=A_{ji}.

That is an axis reorientation. The inverse solves the different requirement AA−1=IAA^{-1}=I. They can be equal in special cases, but one operation cannot be substituted for the other in general.

For the worked matrix, AA happens to be symmetric, so AT=AA^{\mathsf T}=A. But its inverse is

A−1=[1−1−12],A^{-1}=\begin{bmatrix}1&-1\\-1&2\end{bmatrix},

which is clearly not AA. Symmetry alone does not make a matrix orthogonal or make its transpose an inverse.

Here is a nonsymmetric counterexample:

B=[1201],BT=[1021],B−1=[1−201].B=\begin{bmatrix}1&2\\0&1\end{bmatrix}, \qquad B^{\mathsf T}=\begin{bmatrix}1&0\\2&1\end{bmatrix}, \qquad B^{-1}=\begin{bmatrix}1&-2\\0&1\end{bmatrix}.

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 QTQ=IQ^{\mathsf T}Q=I—for example, for a pure rotation or reflection—does Q−1=QTQ^{-1}=Q^{\mathsf T}.

AI application: reverse a learned feature coordinate change

Many pipelines work with a centered feature vector rather than the raw feature values. Let x∈R2\boldsymbol{x}\in\mathbb{R}^2 be the raw two-feature vector and μ\boldsymbol{\mu} be a fixed mean vector. A linear preprocessing step can be written as

z=A(x−μ).\boldsymbol{z}=A(\boldsymbol{x}-\boldsymbol{\mu}).

The matrix AA 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 AA is invertible and the same μ\boldsymbol{\mu} is retained, the raw vector can be recovered with

x=μ+A−1z.\boldsymbol{x}=\boldsymbol{\mu}+A^{-1}\boldsymbol{z}.

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

μ=[104],x=[136],A=[2111].\boldsymbol{\mu}=\begin{bmatrix}10\\4\end{bmatrix}, \qquad \boldsymbol{x}=\begin{bmatrix}13\\6\end{bmatrix}, \qquad A=\begin{bmatrix}2&1\\1&1\end{bmatrix}.

First center the feature vector:

x−μ=[32].\boldsymbol{x}-\boldsymbol{\mu} =\begin{bmatrix}3\\2\end{bmatrix}.

The transformed coordinates are z=[8,5]T\boldsymbol{z}=[8,5]^{\mathsf T}, as in the worked example. Reversing them gives

μ+A−1z=[104]+[32]=[136].\boldsymbol{\mu}+A^{-1}\boldsymbol{z} =\begin{bmatrix}10\\4\end{bmatrix} +\begin{bmatrix}3\\2\end{bmatrix} =\begin{bmatrix}13\\6\end{bmatrix}.

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

S=[1224].S=\begin{bmatrix}1&2\\2&4\end{bmatrix}.

Its determinant is

det⁡(S)=(1)(4)−(2)(2)=0.\det(S)=(1)(4)-(2)(2)=0.

The second row is twice the first, so SS has only one independent direction. It also maps different inputs to the same output:

S[20]=[24]=S[01].S\begin{bmatrix}2\\0\end{bmatrix} =\begin{bmatrix}2\\4\end{bmatrix} =S\begin{bmatrix}0\\1\end{bmatrix}.

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 SS.

Nearly singular is a numerical warning, not a success

Now change one entry by a very small amount:

N=[1111+10−8].N=\begin{bmatrix}1&1\\1&1+10^{-8}\end{bmatrix}.

Its determinant is approximately 10−810^{-8}, 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 4×1084\times10^8 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 stateDeterminant testPractical interpretation
Nonsingular and well-conditionednonzero, not tiny relative to scaleAn inverse or solve can be dependable for the data precision available.
Singularexactly zero in exact arithmeticDistinct inputs may have collapsed to one output; no ordinary inverse exists.
Nearly singularnonzero but small relative to scaleAn 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

Ax=bA\boldsymbol{x}=\boldsymbol{b}

has the mathematical solution x=A−1b\boldsymbol{x}=A^{-1}\boldsymbol{b} when AA is invertible. In code, however, if all you need is x\boldsymbol{x}, forming the full A−1A^{-1} first is unnecessary. numpy.linalg.solve accepts AA and b\boldsymbol{b} 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 Ax=bA\boldsymbol{x} =\boldsymbol{b}.

If several right-hand sides share the same AA, pass them as columns in a matrix BB and solve AX=BAX=B in one call. If AA 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:

r=Ax−b.\boldsymbol{r}=A\boldsymbol{x}-\boldsymbol{b}.

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.

I2=[1001].I_2=\begin{bmatrix}1&0\\0&1\end{bmatrix}.

Step 2: multiply row by column.

I2[4−3]=[1⋅4+0⋅(−3)0⋅4+1⋅(−3)]=[4−3].I_2\begin{bmatrix}4\\-3\end{bmatrix} =\begin{bmatrix}1\cdot4+0\cdot(-3)\\0\cdot4+1\cdot(-3)\end{bmatrix} =\begin{bmatrix}4\\-3\end{bmatrix}.

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 a=1a=1, b=2b=2, c=3c=3, and d=7d=7.

Step 2: compute the determinant.

ad−bc=(1)(7)−(2)(3)=7−6=1.ad-bc=(1)(7)-(2)(3)=7-6=1.

The determinant is nonzero, so the inverse exists.

Step 3: swap and negate.

A−1=11[7−2−31]=[7−2−31].A^{-1}=\frac{1}{1}\begin{bmatrix}7&-2\\-3&1\end{bmatrix} =\begin{bmatrix}7&-2\\-3&1\end{bmatrix}.

The division by the determinant does not change the entries in this particular example because the determinant equals 11.

Question 3

Verify the inverse from Question 2 by calculating A A⁻¹.

Show the step-by-step solution

Use

A=[1237],A−1=[7−2−31].A=\begin{bmatrix}1&2\\3&7\end{bmatrix}, \qquad A^{-1}=\begin{bmatrix}7&-2\\-3&1\end{bmatrix}.

Step 1: compute each entry.

AA−1=[1⋅7+2⋅(−3)1⋅(−2)+2⋅13⋅7+7⋅(−3)3⋅(−2)+7⋅1]=[1001].AA^{-1} =\begin{bmatrix} 1\cdot7+2\cdot(-3)&1\cdot(-2)+2\cdot1\\ 3\cdot7+7\cdot(-3)&3\cdot(-2)+7\cdot1 \end{bmatrix} =\begin{bmatrix}1&0\\0&1\end{bmatrix}.

Step 2: compare with the target. The result is I2I_2, 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 A−1AA^{-1}A 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.

BT=[1021].B^{\mathsf T}=\begin{bmatrix}1&0\\2&1\end{bmatrix}.

Step 2: multiply B by its transpose.

BBT=[1201][1021]=[5221].BB^{\mathsf T} =\begin{bmatrix}1&2\\0&1\end{bmatrix} \begin{bmatrix}1&0\\2&1\end{bmatrix} =\begin{bmatrix}5&2\\2&1\end{bmatrix}.

Step 3: compare with the identity. This is not [1001]\begin{bmatrix}1&0\\0&1\end{bmatrix}, so BTB^{\mathsf T} is not the inverse. In fact, B−1=[1−201]B^{-1}=\begin{bmatrix}1&-2\\0&1\end{bmatrix}; 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.

x=μ+A−1z.x=\mu+A^{-1}z.

Step 2: use the already verified inverse.

A−1z=[1−1−12][85]=[32].A^{-1}z =\begin{bmatrix}1&-1\\-1&2\end{bmatrix} \begin{bmatrix}8\\5\end{bmatrix} =\begin{bmatrix}3\\2\end{bmatrix}.

Step 3: restore the mean.

x=[104]+[32]=[136].x=\begin{bmatrix}10\\4\end{bmatrix}+\begin{bmatrix}3\\2\end{bmatrix} =\begin{bmatrix}13\\6\end{bmatrix}.

Forgetting μ\mu 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.

det⁡(S)=(1)(4)−(2)(2)=0.\det(S)=(1)(4)-(2)(2)=0.

The zero determinant means the rows and columns do not contain two independent directions.

Step 2: exhibit the information loss.

S[20]=[24]=S[01].S\begin{bmatrix}2\\0\end{bmatrix}=\begin{bmatrix}2\\4\end{bmatrix} =S\begin{bmatrix}0\\1\end{bmatrix}.

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 4×1084\times10^8 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 Ax=bAx=b, 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.

Sources

  1. Mathematics for Machine Learning companion website
  2. Mathematics for Machine Learning book PDF
  3. NumPy documentation: numpy.eye
  4. NumPy documentation: numpy.linalg.inv
  5. NumPy documentation: numpy.linalg.solve
  6. NumPy documentation: numpy.linalg.det
  7. NumPy documentation: numpy.linalg.cond