Vector Norm, Matrix Determinant, and Inverse Matrix with SciPy
Introduction to Linear Algebra in SciPy
Linear algebra is at the heart of many scientific disciplines, including data science, engineering, and machine learning. Understanding the basics of linear algebra can open doors to solving complex computational problems. In this lesson, you will learn how to utilize the SciPy library — a powerful tool for performing linear algebra operations efficiently.
In this lesson, we will focus on three key linear algebra operations: calculating the vector norm, matrix determinant, and inverse of a matrix using SciPy. By the end of this lesson, you will be familiar with these operations and ready to apply them to real-world problems.
Quick Recap: NumPy Arrays
Before we dive into SciPy, let's quickly revisit NumPy arrays. In earlier lessons (if any), you may have encountered NumPy — an essential library for numerical computing in Python. NumPy arrays are the building blocks of SciPy, especially in linear algebra, where vectors and matrices are manipulated as arrays.
Here's a simple reminder of creating a NumPy array:
This line of code creates an array with elements 1, 2, and 3. We will use similar arrays to create vectors and matrices throughout this lesson.
Vector Norms
A vector norm is a measure of a vector's length or magnitude. It helps in quantifying the size of vectors in vector spaces and is frequently used in machine learning algorithms.
The Euclidean norm (also known as the L2 norm) is just one type of norm. SciPy's norm() function can be used to compute various other norms by passing different values to the ord parameter.
As a short reminder:
- L2 Norm (Euclidean norm): The formula for the L2 norm of a vector is:
- L1 Norm (Manhattan norm): The formula for the L1 norm is:
- Infinity Norm (Max norm): The formula for the infinity norm is:
Vector Norms: Python
Here's how you can compute different norms of a vector using SciPy:
- L2 Norm (Euclidean norm): This is the default when using the
norm()function without specifying theordparameter. It calculates the standard Euclidean length of the vector. - L1 Norm (Manhattan norm): Set
ord=1to calculate this norm, which is the sum of the absolute values of the vector's components. - Infinity Norm (Max norm): Set
ord=np.infto calculate this norm, which is the maximum absolute value among the components of the vector.
Using these different norms, you can analyze vectors from various perspectives, focusing on different aspects of magnitude.
