Lesson 18
Linear Algebra
Compute dot, inner, and outer products, transpose and trace, then use np.linalg for determinants, inverses, and solving linear equations.
Linear algebra works with vectors and matrices. NumPy has the products at the top level and the matrix tools in np.linalg. Results are floats, so compare them with np.allclose instead of ==.
dot, inner, and outer
For two 1-D arrays, dot and inner multiply matching values and add them up. For 2-D arrays, inner pairs each row of the first with each row of the second. outer multiplies every value of one array by every value of the other.
import numpy as np
u = np.array([1, 3, 5])
w = np.array([2, 4, 6])
print(np.dot(u, w), np.inner(u, w))
print(np.outer(u, w))
a = np.array([[1, 3], [5, 7]])
b = np.array([[2, 4], [6, 8]])
print(np.inner(a, b))Output:
44 44
[[ 2 4 6]
[ 6 12 18]
[10 20 30]]
[[14 30]
[38 86]]The dot product is 1×2 + 3×4 + 5×6 = 44. In the last result, the top-left value is row 0 of a with row 0 of b: 1×2 + 3×4 = 14.
Transpose and trace
.T swaps rows and columns. np.trace adds up the main diagonal.
import numpy as np
m = np.array([[6, 3, 5], [9, 2, 1], [7, 8, 4]])
print(m.T)
print(np.trace(m))Output:
[[6 9 7]
[3 2 8]
[5 1 4]]
12Determinant and inverse
np.linalg.det gives the determinant of a square matrix. When it is not 0, np.linalg.inv gives the inverse, and the matrix times its inverse is the identity. A matrix with determinant 0 has no inverse and raises LinAlgError.
import numpy as np
A = np.array([[2, 4], [6, 8]])
print(round(np.linalg.det(A), 6))
inverse = np.linalg.inv(A)
print(inverse)
print(np.allclose(A @ inverse, np.eye(2)))
try:
np.linalg.inv(np.array([[1, 2], [2, 4]]))
except np.linalg.LinAlgError as error:
print("LinAlgError:", error)Output:
-8.0
[[-1. 0.5 ]
[ 0.75 -0.25]]
True
LinAlgError: Singular matrixSolve a system of equations
np.linalg.solve(A, b) finds x with A @ x == b. Here it solves 2x + 4y = 5 and 6x + 8y = 6. A must be square with a non-zero determinant.
import numpy as np
A = np.array([[2, 4], [6, 8]])
b = np.array([5, 6])
x = np.linalg.solve(A, b)
print(x)
print(np.allclose(A @ x, b))Output:
[-2. 2.25]
TrueKey takeaways
-
dotandinneragree for 1-D.outerpairs every value -
.Ttransposes andtraceadds the diagonal -
invandsolveneed a square matrix with a non-zero determinant -
Check float results with
np.allclose -
Previous: Unique Values and Sets
Loading playground…
Check your understanding
Linear Algebra Quiz
What is np.dot([1, 3, 5], [2, 4, 6])?
1 / 10