9 Basic Linear Algebra Operations with NumPy

Linear algebra is one of the most important mathematical topics that is highly useful to do a good data science. Learning the basics of linear algebra adds a valuable tool set to your data science skill.

Python’s NumPy has fast efficient functions for all standard linear albegra/matrix operations. Here we will see 9 important and useful linear algebra operations using NumPy and linalg module in NumPy.

1. How to Compute Dot product of two vectors?

Let us create two 1d-arrays using np.array function.

x = np.array([1,2,3])
y = np.array([4,5,6])

And we can print to see the content of the two arrays.

print(x)
print(y)
[1 2 3]
[4 5 6]

We can compute dot product of the two NumPy arrays using np.dot() function that takes the two 1d-array as inputs. Note that the order input arguments does not matter for the dot product of two vectors.

print(np.dot(x,y))
print(np.dot(y,x))

32
32

2. How To Compute Dot product of a Matrix and vector?

We can also compute dot product of a vector and a matrix using np.dot() function. Let us create a 2×3 matrix with NumPy’s array function.

a = np.array([[1,2,3], [4,5,6]])
print(a)
[[1 2 3]
 [4 5 6]]

We can NumPy’s dot function to compute dot product of a matrix and vector. And the result would be a vector.

np.dot(a,x)
array([14, 32])

3. How to Compute transpose of a matrix?

NumPy’s transpose() function changes rows with columns. In this example, we start with 2 x 3 matrix.

print(a)
[[1 2 3]
 [4 5 6]]

After transposing, we get 3 x 2 matrix.

np.transpose(a)
array([[1, 4],
       [2, 5],
       [3, 6]])

4. How to Create Identity Matrix?

Identity matrix is matrix with ones along the diagnola and zeroes in all off-diagonal elements. NumPy’s eye() function creates identity matrix. We just need to provide the dimension of identity matrix we want.

np.eye(3)

array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

5. How to Compute Inverse of a Matrix?

Computing inverse of a matrix is one of the important concepts and has many useful applications. Not all matrices can have inverse. Finding an inverse of a matrix A is find a matrix B such that the product of A with B is the identity matrix.

Let us create a 3 x 3 NumPy array.

b = np.array([[1.,2.,3.], [10.,5,6.],[7.,8.,9.]])
print(b)

[[ 1.  2.  3.]
 [10.  5.  6.]
 [ 7.  8.  9.]]

NumPy’s linalg module has the function inv.

b_inv = np.linalg.inv(b)
print(b_inv)

[[-0.08333333  0.16666667 -0.08333333]
 [-1.33333333 -0.33333333  0.66666667]
 [ 1.25        0.16666667 -0.41666667]]

6. How to Perform Matrix Multiplication?

Matrix multiplication or product of matrices is one of the most common operations we do in linear algebra. We can use NumPy’s dot() function to compute matrix multiplication.

Let us see a example of matrix multiplication using the previous example of computing matrix inverse. We noted that, if we multiply a Matrix and its inverse, we get identity matrix as the result.

Here we use NumPy’ dot() function with a matrix and its inverse. We get output that looks like a identity matrix.

np.dot(b,np.linalg.inv(b) )

array([[ 1.00000000e+00, -2.77555756e-17, -1.11022302e-16],
       [ 0.00000000e+00,  1.00000000e+00,  2.22044605e-16],
       [ 0.00000000e+00, -8.32667268e-17,  1.00000000e+00]])

Let us check if the matrix we have is close to identical matrix. NumPy’s allclose() function can help us with that.

np.allclose(np.dot(b, b_inv), np.eye(3))

True

7. How to Compute Trace of a Matrix?

Let us create a matrix a.

print(a)

[[1 2 3]
 [4 5 6]
 [7 8 9]]

We can compute trace of a matrix using NumPy’s trace() function..

print(np.trace(a))

8. How to Compute Determinant of a Matrix?

Let us create 2×2 square matrix.

a = np.array([[3.,4.],[5,6]])
print(a)

[[3. 4.]
 [5. 6.]]

We can compute determinant of a matrix in NumPy with linalg module’s det() function.

np.linalg.det(a)

-2.0000000000000004

9. How to Compute Eigen Value and Eigen Vector of a Matrix with NumPy?

Let us create diagonal matrix. We can create a diagonal matrix with diag() function in NumPy.

a = np.diag((3, 4, 5))
a

array([[3, 0, 0],
       [0, 4, 0],
       [0, 0, 5]])

We can use linalg module’s eig() function to computer eigen vector and eigen values of a matrix. In our example, since our input matrix is a diagonal matrix, the eigen vectors make a identity matrix and the eigen values are simply the diagonal elements of the input matrix.

e_val,e_vec = np.linalg.eig(a)
print(e_val)

[3. 4. 5.]
print(e_vec)

[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]