Numpy Linear Algebra with Examples

One of the key features of NumPy is its extensive collection of linear algebra functions. In this tutorial, we will explore the fundamental linear algebra functions provided by NumPy, along with code examples to help beginners get started.

Importing NumPy

To use NumPy’s linear algebra functions, you need to import the library into your Python script or notebook:

import numpy as np

Creating Matrices and Arrays in NumPy

Before we start using linear algebra functions, let’s quickly learn how to create matrices and arrays using NumPy:

# Create a 2x2 matrix
matrix = np.array([[1, 2], [3, 4]])

# Create a 1D array
array = np.array([5, 6, 7])

Basic Linear Algebra Operations in NumPy

1. Matrix Multiplication (dot)

Matrix multiplication is a fundamental operation in linear algebra. NumPy provides the dot function to perform matrix multiplication:

matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

result = np.dot(matrix1, matrix2)
print(result)

Output:

[[19 22]
[43 50]]

2. Multi-Dot (linalg.multi_dot)

The multi_dot function calculates the dot product of two or more arrays efficiently by automatically selecting the fastest evaluation order.

arrays = [np.array([1, 2]), np.array([3, 4]), np.array([5, 6])]
multi_dot_result = np.linalg.multi_dot(arrays)
print(multi_dot_result)

Output:

44

3. Vector Dot Product (vdot)

The vdot function returns the dot product of two vectors. It differs from dot as it treats the input arrays as vectors and performs their dot product.

vector1 = np.array([1, 2, 3])
vector2 = np.array([4, 5, 6])

vector_dot_product = np.vdot(vector1, vector2)
print(vector_dot_product)

Output:

32

4. Inner Product (inner)

The inner product of two arrays can be calculated using the inner function:

array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

inner_product = np.inner(array1, array2)
print(inner_product)

Output:

32

5. Outer Product (outer)

The outer product of two arrays can be calculated using the outer function:

array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

outer_product = np.outer(array1, array2)
print(outer_product)

Output:

[[ 4 5 6]
[ 8 10 12]
[12 15 18]]

6. Matrix Multiplication (matmul)

The matmul function performs matrix multiplication between two arrays.

matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

matrix_product = np.matmul(matrix1, matrix2)
print(matrix_product)

Output:

[[19 22]
[43 50]]

7. Tensor Dot Product (tensordot)

The tensordot function computes the tensor dot product along specified axes of two arrays.

array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6], [7, 8]])

tensor_dot_product = np.tensordot(array1, array2, axes=1)
print(tensor_dot_product)

Output:

[[[ 5 6]
[10 12]]

[[21 24]
[28 32]]]

8. Determinant (det)

The determinant of a square matrix is an important value used in various mathematical operations:

matrix = np.array([[1, 2], [3, 4]])
determinant = np.linalg.det(matrix)
print(determinant)

Output:

-2.0000000000000004

9. Inverse (inv)

To find the inverse of a square matrix, you can use the inv function:
matrix = np.array([[1, 2], [3, 4]])
inverse_matrix = np.linalg.inv(matrix)
print(inverse_matrix)

Output:

[[-2. 1. ]
[ 1.5 -0.5]]

Solving Linear Systems in NumPy

NumPy makes it easy to solve systems of linear equations using its linear algebra functions:

# Ax = B
A = np.array([[2, 3], [1, -1]])
B = np.array([7, 1])

solution = np.linalg.solve(A, B)
print(solution)

Output:

[2. 1.]

Additional Linear Algebra Functions in NumPy

1. Trace (trace)

The trace of a square matrix is the sum of its diagonal elements:

matrix = np.array([[1, 2], [3, 4]])
trace_sum = np.trace(matrix)
print(trace_sum)

Output:

5

2. Einstein Summation (einsum)

The einsum function evaluates the Einstein summation convention on the operands. This allows you to perform a wide range of element-wise and matrix operations in a concise manner.

array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

einsum_result = np.einsum('i,i', array1, array2)
print(einsum_result)

Output:

32

3. Matrix Power (linalg.matrix_power)

The matrix_power function raises a square matrix to the power of an integer n.

matrix = np.array([[1, 2], [3, 4]])
power = 3

matrix_to_power = np.linalg.matrix_power(matrix, power)
print(matrix_to_power)

Output:

[[ 37 54]
[ 81 118]]

4. Kronecker Product (kron)

The kron function computes the Kronecker product of two arrays.

array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6], [7, 8]])

kron_product = np.kron(array1, array2)
print(kron_product)

Output:

[[ 5 6 10 12]
[ 7 8 14 16]
[15 18 20 24]
[21 24 28 32]]

Conclusion

These functions are indispensable tools for data analysis, scientific computing, and machine learning tasks. By understanding and utilizing these functions effectively, you’ll be better equipped to manipulate and analyze arrays and matrices in your Python projects. Happy coding!

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.