Linear algebra is an important topic across a variety of subjects. It allows you to solve problems related to vectors, matrices, and linear equations. In Python, most of the routines related to this subject are implemented in scipy.linalg
, which offers very fast linear algebra capabilities.
In particular, linear models play an important role in a variety of real-world problems, and scipy.linalg
provides tools to compute them in an efficient way.
In this tutorial, you’ll learn how to:
- Study linear systems using determinants and solve problems using matrix inverses
- Interpolate polynomials to fit a set of points using linear systems
- Use Python to solve linear regression problems
- Use linear regression to predict prices based on historical data
This is the second part of a series of tutorials on linear algebra using scipy.linalg
. So, before continuing, make sure to take a look at the first tutorial of the series before reading this one.
Free Source Code:Click here to download the free code and dataset that you’ll use to work with linear systems and algebra in Python with scipy.linalg
.
Now you’re ready to get started!
Getting Started With Linear Algebra in Python
Linear algebra is a branch of mathematics that deals with linear equations and their representations using vectors and matrices. It’s a fundamental subject in several areas of engineering, and it’s a prerequisite to a deeper understanding of machine learning.
To work with linear algebra in Python, you can count on SciPy, which is an open-source Python library used for scientific computing, including several modules for common tasks in science and engineering.
Of course, SciPy includes modules for linear algebra, but that’s not all. It also offers optimization, integration, interpolation, and signal processing capabilities. It’s part of the SciPy stack, which includes several other packages for scientific computing, such as NumPy, Matplotlib, SymPy, IPython, and pandas.
scipy.linalg
includes several tools for working with linear algebra problems, including functions for performing matrix calculations, such as determinants, inverses, eigenvalues, eigenvectors, and the singular value decomposition.
In the previous tutorial of this series, you learned how to work with matrices and vectors in Python to model practical problems using linear systems. You solved these problems using scipy.linalg
.
In this tutorial, you’re going a step further, using scipy.linalg
to study linear systems and build linear models for real-world problems.
In order to use scipy.linalg
, you have to install and set up the SciPy library. Besides that, you’re going to use Jupyter Notebook to run the code in an interactive environment. SciPy and Jupyter Notebook are third-party packages that you need to install. For installation, you can use the conda
or pip
package manager. Revisit Working With Linear Systems in Python With scipy.linalg
for installation details.
Note: Using Jupyter Notebook to run the code isn’t mandatory, but it facilitates working with numerical and scientific applications.
For a refresher on working with Jupyter Notebooks, take a look at Jupyter Notebook: An Introduction.
Next, you’ll go through some fundamental concepts of linear algebra and explore how to use Python to work with these concepts.
Understanding Vectors, Matrices, and the Role of Linear Algebra
A vector is a mathematical entity used to represent physical quantities that have both magnitude and direction. It’s a fundamental tool for solving engineering and machine learning problems. So are matrices, which are used to represent vector transformations, among other applications.
Note: In Python, NumPy is the most used library for working with matrices and vectors. It uses a special type called ndarray
to represent them. As an example, imagine that you need to create the following matrix:

With NumPy, you can use np.array()
, to create it, providing a nested list containing the elements of each row of the matrix:
In [1]: importnumpyasnpIn [2]: np.array([[1,2],[3,4],[5,6]])Out[2]:array([[1, 2], [3, 4], [5, 6]])
NumPy provides several functions to facilitate working with vector and matrix computations. You can find more information on how to use NumPy to represent vectors and matrices and perform operations with them in the previous tutorial in this series.
A linear system or, more precisely, a system of linear equations, is a set of equations linearly relating to a set of variables. Here’s an example of a linear system relating to the variables x₁ and x₂:

Here you have two equations involving two variables. In order to have a linear system, the values that multiply the variables x₁ and x₂ must be constants, like the ones in this example. It’s common to write linear systems using matrices and vectors. For example, you can write the previous system as the following matrix product:

Comparing the matrix product form with the original system, you can notice the elements of matrix A correspond to the coefficients that multiply x₁ and x₂. Besides that, the values in the right-hand side of the original equations now make up vector b.
Read the full article at https://realpython.com/python-linear-algebra/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]