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

Real Python: Working With Linear Systems in Python With scipy.linalg
blow post content copied from  Planet Python
click here to view original post

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
  • Solve linear systems using scipy.linalg

Now you’re ready to get started!

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.

Getting Started With scipy.linalg

SciPy is 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 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.

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 using SciPy, you’re also 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 tutorial 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, you should create a virtual environment in which you’ll install the packages. Assuming you have Python installed, you can create and activate a virtual environment named linalg:

PS> python -m venv linalg
PS> .\linalg\Scripts\Activate
$ python -m venv linalg
$ source ./linalg/bin/activate

After you activate the virtual 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 use scipy.linalg and Jupyter.

Before opening Jupyter Notebook, you need to register the 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 »

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

January 18, 2023 at 07:30PM
Click here for more details...

The original post is available in Planet Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.