Wednesday, June 16, 2021

Real Python: Working With Linear Systems in Python With scipy.linalg

Linear algebra is widely used across a variety of subjects, and you can use it to solve many problems once you organize the information using concepts like vectors 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 systems play an important role in modeling a variety of real-world problems, and scipy.linalg provides tools to study and solve them in an efficient way.

In this tutorial, you’ll learn how to:

  • Apply linear algebra concepts to practical problems using scipy.linalg
  • Work with vectors and matrices using Python and NumPy
  • Model practical problems using linear systems
  • How to solve linear systems using scipy.linalg

Let’s get started!

Free Bonus: Click here to get access to a free NumPy Resources Guide that points you to the best tutorials, videos, and books for improving your NumPy skills.

Getting Started With scipy.linalg

SciPy is as an open source Python library used for scientific computing, including several modules for common tasks in science and engineering such as linear algebra, optimization, integration, interpolation, and signal processing. It’s part of the SciPy stack, which includes several other packages for scientific computing such as NumPy, Matplotlib, SymPy, IPython, and pandas.

Linear algebra is a branch of mathematics that concerns linear equations and their representations using vectors and matrices. It’s a fundamental subject used in several areas of engineering, and it’s a prerequisite to a deeper understanding of machine learning.

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 this tutorial, you’re going to use some of the functions from scipy.linalg to work on practical problems involving linear systems. In order to use scipy.linalg, you have to install and set up the SciPy library, which you can do by using the Anaconda Python distribution and the conda package and environment management system.

Note: To learn more about Anaconda and conda, check out Setting Up Python for Machine Learning on Windows.

To begin, create a conda environment and activate it:

$ conda create --name linalg
$ conda activate linalg

After you activate the conda environment, your prompt will show its name, linalg. Then you can install the necessary packages inside the environment:

(linalg) $ conda install scipy jupyter

After you execute this command, it should take a while for the system to figure out the dependencies and proceed with the installation.

Note: Besides SciPy, you’re going to use Jupyter Notebook to run the code in an interactive environment. Doing so 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.

If you prefer to follow along with the article using a different Python distribution and the pip package manager, then expand the collapsible section below to see how to set up your environment.

First, it’s recommended to create a virtual environment in which you’ll install the packages. Assuming you have Python installed, you can create a virtual environment named linalg:

$ python -m venv linalg

After creating the environment, you need to activate it so that you can use pip to install packages in the environment. If you’re on Linux or macOS, then you can activate the environment with the command:

$ source ./linalg/bin/activate

On Windows, the command you have to use is slightly different:

C:> \linalg\Scripts\activate.bat

After you activate the conda environment, your prompt will show its name, linalg. Then you can install the necessary packages inside the environment using pip:

(linalg) $ python -m pip install scipy jupyter

The system will take a while to figure out the dependencies and proceed with the installation. After the command finishes, you’re all set to open Jupyter and use scipy.linalg.

Before opening Jupyter Notebook, you need to register the conda linalg environment so that you can create Notebooks using it as the kernel. To do that, with the linalg environment activated, run the following command:

(linalg) $ python -m ipykernel install --user --name linalg

Now you can open Jupyter Notebook by running the following command:

$ jupyter notebook

After Jupyter loads in your browser, create a new Notebook by clicking Newlinalg, as shown below:

Read the full article at https://realpython.com/python-scipy-linalg/ »


[ 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 ]



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...