Skip to main content
Editor

NumPy Linear Algebra: Dot Products, Inverses, and Eigenvalues

Advanced25 min5 exercises100 XP
0/5 exercises

Linear algebra is the math behind machine learning, computer graphics, physics simulations, and data science. Every time a game rotates a 3D object, a search engine ranks pages, or a neural network makes a prediction, matrix operations are doing the heavy lifting.

NumPy's linalg module gives you all the essential linear algebra tools: dot products, matrix multiplication, transposes, inverses, determinants, eigenvalues, and solvers for systems of equations. In this tutorial, you'll learn each one with practical examples.

What Is a Dot Product?

A dot product takes two arrays of the same length, multiplies corresponding elements, and sums the results. It's a single number that measures how "aligned" two vectors are. Think of it like a similarity score - two vectors pointing the same direction have a large positive dot product.

dot_product.py
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# Three ways to compute dot product
print(np.dot(a, b)) # 32
print(a @ b) # 32
print(np.sum(a * b)) # 32 (manual: 1*4 + 2*5 + 3*6)
Output
32
32
32

A real-world example: imagine you bought 3 apples at $1.50 each, 2 bananas at $0.75, and 5 oranges at $1.00. Your total cost is a dot product of quantities and prices: [3, 2, 5] . [1.50, 0.75, 1.00] = 4.50 + 1.50 + 5.00 = $11.00.

How Does Matrix Multiplication Work?

Matrix multiplication extends the dot product to 2D arrays. For two matrices A (shape m x n) and B (shape n x p), each element in the result is the dot product of a row from A and a column from B. The result has shape (m x p).

matmul.py
import numpy as np
A = np.array([[1, 2],
[3, 4]])
B = np.array([[5, 6],
[7, 8]])
# Matrix multiplication with @
C = A @ B
print(C)
# [[19 22]
# [43 50]]
# Verify: C[0,0] = 1*5 + 2*7 = 19
# Verify: C[1,1] = 3*6 + 4*8 = 50
Output
[[19 22]
 [43 50]]
Element-wise (*)
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A * B)
# [[ 5 12]
#  [21 32]]
Matrix Multiply (@)
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B)
# [[19 22]
#  [43 50]]

What Does Transposing a Matrix Do?

Transposing flips a matrix over its diagonal - rows become columns and columns become rows. A (2, 3) matrix becomes (3, 2). Think of it like rotating a spreadsheet so rows and columns swap.

transpose.py
import numpy as np
A = np.array([[1, 2, 3],
[4, 5, 6]])
print(A.shape) # (2, 3)
print(A.T) # Transposed
# [[1 4]
# [2 5]
# [3 6]]
print(A.T.shape) # (3, 2)
Output
(2, 3)
[[1 4]
 [2 5]
 [3 6]]
(3, 2)

What Are the Inverse and Determinant?

The inverse of a square matrix A is another matrix A_inv such that A @ A_inv equals the identity matrix (1s on the diagonal, 0s elsewhere). It's like dividing by a matrix. Not all matrices have inverses - only those with a non-zero determinant.

inverse.py
import numpy as np
A = np.array([[2, 1],
[5, 3]])
# Determinant
det = np.linalg.det(A)
print(f'Determinant: {det:.1f}') # 1.0
# Inverse
A_inv = np.linalg.inv(A)
print('Inverse:')
print(A_inv)
# [[ 3. -1.]
# [-5. 2.]]
# Verify: A @ A_inv should be identity
print('A @ A_inv:')
print(np.round(A @ A_inv))
# [[1. 0.]
# [0. 1.]]
Output
Determinant: 1.0
Inverse:
[[ 3. -1.]
 [-5.  2.]]
A @ A_inv:
[[1. 0.]
 [0. 1.]]

How Do You Solve a System of Equations?

A system of linear equations like:

2x + y = 5

x + 3y = 10

can be written as the matrix equation A @ x = b, where A holds the coefficients, x is the unknowns, and b is the right-hand side. NumPy solves it with np.linalg.solve(A, b).

solve.py
import numpy as np
# 2x + y = 5
# x + 3y = 10
A = np.array([[2, 1],
[1, 3]])
b = np.array([5, 10])
x = np.linalg.solve(A, b)
print(x) # [1. 3.]
# So x = 1, y = 3
# Verify: 2(1) + 1(3) = 5, 1(1) + 3(3) = 10
print(A @ x) # [ 5. 10.]
Output
[1. 3.]
[ 5. 10.]

Here's a practical example: a coffee shop sells lattes for $4 and muffins for $3. On Monday they made $47 from 14 items. How many lattes and muffins did they sell?

coffee_shop.py
import numpy as np
# 4L + 3M = 47 (revenue)
# L + M = 14 (total items)
A = np.array([[4, 3],
[1, 1]])
b = np.array([47, 14])
solution = np.linalg.solve(A, b)
print(f'Lattes: {solution[0]:.0f}') # 5
print(f'Muffins: {solution[1]:.0f}') # 9
Output
Lattes: 5
Muffins: 9

What Are Eigenvalues and Eigenvectors?

An eigenvector of a matrix A is a special vector that, when multiplied by A, only gets scaled (stretched or shrunk), not rotated. The scale factor is the eigenvalue. In math: A @ v = lambda * v, where v is the eigenvector and lambda is the eigenvalue.

Eigenvalues power many algorithms: Principal Component Analysis (PCA) for dimensionality reduction, Google's PageRank algorithm, vibration analysis in engineering, and quantum mechanics. They reveal the "natural modes" of a system.

eigenvalues.py
import numpy as np
A = np.array([[4, 2],
[1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(A)
print('Eigenvalues:', eigenvalues) # [5. 2.]
print('Eigenvectors (columns):')
print(np.round(eigenvectors, 4))
# Verify: A @ v = lambda * v for the first eigenvalue
v1 = eigenvectors[:, 0] # first eigenvector (first column)
lambda1 = eigenvalues[0] # first eigenvalue
print('A @ v1:', A @ v1)
print('lambda * v1:', lambda1 * v1)
Output
Eigenvalues: [5. 2.]
Eigenvectors (columns):
[[ 0.8944 -0.7071]
 [ 0.4472  0.7071]]
A @ v1: [4.47213595 2.23606798]
lambda * v1: [4.47213595 2.23606798]

Practice Exercises

Shopping Cart Total
Write Code

A customer buys quantities [4, 2, 6, 1] of items priced at [2.50, 1.75, 0.99, 12.00]. Use the @ operator to compute the total cost, round it to 2 decimals, and print it.

import numpy as np
quantities = np.array([4, 2, 6, 1])
prices = np.array([2.50, 1.75, 0.99, 12.00])
# Compute and print total cost using @
Matrix Multiply
Write Code

Compute the matrix product of A and B, then print the result and its shape.

A = np.array([[1, 2, 3],
              [4, 5, 6]])
B = np.array([[7, 8],
              [9, 10],
              [11, 12]])
import numpy as np
A = np.array([[1, 2, 3],
[4, 5, 6]])
B = np.array([[7, 8],
[9, 10],
[11, 12]])
# Compute and print the product and its shape
Solve the Price Puzzle
Write Code

A store sells pens and notebooks. The prices are unknown.

  • 3 pens + 2 notebooks = $13
  • 1 pen + 4 notebooks = $17
  • Set up the coefficient matrix and the right-hand side vector, then use np.linalg.solve() to find the price of each item. Print the price of a pen and then the price of a notebook, each on a separate line, rounded to 2 decimal places.

    import numpy as np
    # Set up the system and solve
    Predict the Determinant
    Predict Output

    What does the following code print? Remember the determinant formula for a 2x2 matrix: ad - bc.

    import numpy as np
    M = np.array([[3, 8],
                  [4, 6]])
    print(np.linalg.det(M))
    import numpy as np
    M = np.array([[3, 8],
    [4, 6]])
    print(round(np.linalg.det(M)))
    Find the Eigenvalues
    Write Code

    Find and print the eigenvalues of the following matrix, sorted from largest to smallest:

    A = np.array([[5, 1],
                  [1, 5]])

    Use np.sort() with [::-1] to reverse the order.

    import numpy as np
    A = np.array([[5, 1],
    [1, 5]])
    # Find eigenvalues, sort descending, print

    Harder problems

    More problems on this topic: Math

    Related Tutorials

    Was this tutorial helpful?