Abstract: apply the concept of linear algebra to practical problems scipy.linalg Use Python and NumPy to process vectors and matrices Use linear systems to simulate actual problems Use to solve linear systems scipy.linalg

This article is shared from the HUAWEI cloud community "Using scipy.linalg to use linear systems in Python", author: Yuchuan.

Linear algebra widely used in various disciplines. Once you vector and linear equation , you can use it to solve many problems. In Python, most routines related to this topic are implemented in scipy.linalg, which provides very fast linear algebra functions.

In particular, the linear system plays an important role in simulating various real-world problems, and scipy.linalg provides tools to study and solve these problems in an effective way.

In this tutorial, you will learn how to:

  • Apply linear algebra concepts to practical problems scipy.linalg
  • Use Python and NumPy to process vectors and matrices
  • Use linear systems to simulate real problems
  • How to solve scipy.linalg using linear system

let's start!

Getting started with scipy.linalg

SciPy is an open source Python library for scientific computing, including several modules for common tasks in science and engineering, such as linear algebra, optimization, integration, interpolation, and signal processing. It is 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 involves linear equations and their representation using vectors and matrices. It is a basic discipline used in many engineering fields and a prerequisite for an in-depth understanding of machine learning.

scipy.linalg includes a variety of tools for dealing with linear algebra problems, including functions for performing matrix calculations, such as determinants, inverse matrices, eigenvalues, eigenvectors, and singular value decomposition.

In this tutorial, you will use some of the functions scipy.linalg from from to solve practical problems involving linear systems. In order to use scipy.linalg, you must install and set up the SciPy library, you can use the Anaconda Python distribution and conda package and environment management system to complete.

Note: To learn more about Anaconda and conda, please see Setting up Python for machine learning on Windows.

First, create a conda environment and activate it:

$condacreate--namelinalg
$condaactivatelinalg

After activating the conda environment, your prompt will display its name linalg. Then you can install the necessary packages in the environment:

(linalg)$condainstallscipyjupyter
After executing this command, it should take a while for the system to determine the dependencies and continue the installation.

Note: In addition to SciPy, you will also use JupyterNotebook to run code in an interactive environment. Doing so is not mandatory, but it helps to deal with numerical and scientific applications.

For a review of using JupyterNotebooks, please check JupyterNotebook: Introduction.

If you prefer to use a different Python distribution and pip package manager to read this article, please expand the foldable section below to learn how to set up your environment.

Set the environment to use pip to show and hide

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

(linalg)$python-mipykernelinstall--user--namelinalg

Now you can open JupyterNotebook by running the following command:

$jupyternotebook
After loading Jupyter in the browser, create a new laptop by clicking New→linalg, as shown in the following figure:
image.png

Inside the laptop, you can test whether the installation is successful by importing the scipy package:

In[1]:importscipy
Now that you have finished setting up the environment, you will see how to use vectors and matrices in Python, which is the basis for using scipy.linalg linear algebra applications.

Use NumPy to process vectors and matrices

A vector is a mathematical entity used to represent physical quantities that have both magnitude and direction. It is a basic tool for solving engineering and machine learning problems. It is like a matrix , which is used to represent applications such as vector transformation.

NumPy is the most commonly used library for processing matrices and vectors in Python for processing scipy.linalg linear algebra applications. In this section, you will learn the basics of using it to create matrices and vectors and perform operations on them.

To start working with matrices and vectors, the first thing you need to do in JupyterNotebook is to import numpy. The usual way is to use the alias np:

In[2]:importnumpyasnp
To represent matrices and vectors, NumPy uses a type called ndarray.

To create an ndarray object, you can use np.array(), which requires an array-like object, such as a list or nested list.

For example, suppose you need to create the following matrix:
image.png

To create it with NumPy, you can use np.array(), which provides a nested list containing the elements of each row of the matrix:

In[3]:A=np.array([[1,2],[3,4],[5,6]])
...:A
Out[3]:
array([[1,2],
[3,4],
[5,6]])

You may notice that NumPy provides a visual representation of a matrix, where you can identify its columns and rows.

It is worth noting that the elements of the NumPy array must be of the same type. You can check the type .dtype of a NumPy array using the following methods:

In[4]:A.dtype
Out[4]:
dtype('int64')

Since all the elements of A are integers, the array is an int64 created with type. If one of the elements is float, the array float64 will be created using type:

In[5]:A=np.array([[1.0,2],[3,4],[5,6]])
...:A
Out[5]:
array([[1.,2.],
[3.,4.],
[5.,6.]])

In[6]:A.dtype
Out[6]:
dtype('float64')

To check the size of an ndarray object, you can use .shape. For example, to check the size A, you can use A.shape:

In[7]:A.shape
Out[7]:
(3,2)

As expected, the dimension of the A matrix is ​​3×2 because A has three rows and two columns.

When dealing with problems involving matrices, you usually need to use the transpose operation, which swaps the columns and rows of the matrix.

To transpose a vector or matrix represented by an ndarray object, you can use .transpose() or .T. For example, you can get the conversion of A to AT:

In[8]:A.T
Out[8]:
array([[1.,3.,5.],
[2.,4.,6.]])

By transposition, column A becomes a row, and row AT becomes a column.

To create a vector, you can use np.array(), providing a list containing the elements of the vector:

In[9]:v=np.array([1,2,3])
...:v
Out[9]:
array([1,2,3])

To check the dimensions of the vector, you can use .shape as before:

In[10]:v.shape
Out[10]:
(3,)

Note that the shape of this vector is (3,)andnot(3,1)or(1,3). This is a NumPy function, suitable for those who are accustomed to using MATLAB. In NumPy, one-dimensional arrays can be created, such as v, which may cause problems when performing operations between matrices and vectors. For example, the transpose operation has no effect on one-dimensional arrays.

Whenever you provide a parameter like a one-dimensional array to np.array(), the resulting array will be a one-dimensional array. To create a two-dimensional array, you must provide a parameter similar to a two-dimensional array, such as a nested list:

In[11]:v=np.array([[1,2,3]])
...:v.shape
Out[11]:
(1,3)

In the above example, the size v is 1×3, which corresponds to the size of a two-dimensional line vector. To create a column vector, you can use nested lists:

In[12]:v=np.array([[1],[2],[3]])
...:v.shape
Out[12]:
(3,1)

In this case, the size v is 3×1, which corresponds to the size of a two-dimensional column vector.

Using nested lists to create vectors can be laborious, especially for the most used column vectors. As an alternative, you can create a one-dimensional vector, provide a flat list np.array, and use .reshape() to change the dimension of the ndarray object:

In[13]:v=np.array([1,2,3]).reshape(3,1)
...:v.shape
Out[13]:
(3,1)

In the above example, you use a one-dimensional vector of shape (3,1) from .reshape() to get the column vector (3,) of shape. It is worth mentioning that .reshape() expects that the number of elements in the new array is compatible with the number of elements in the original array. In other words, the number of elements in the array with the new shape must be equal to the number of elements in the original array.

In this example, you can also use .reshape() when the number of rows in the array is not clearly defined:

In[14]:v=np.array([1,2,3]).reshape(-1,1)
...:v.shape
Out[14]:
(3,1)

Here, the parameter .reshape() provided by -1 means that the new array only has the required number of rows for one column, as specified in the second parameter. In this case, since the original array has three elements, the number of rows in the new array will be 3.

In practical applications, you often need to create a matrix of zero, one, or random elements. For this purpose, NumPy provides some convenient functions, which you will see next.

Use convenience functions to create arrays

NumPy also provides some convenient functions to create arrays. For example, to create an array filled with zeros, you can use np.zeros():

In[15]:A=np.zeros((3,2))
...:A
Out[15]:
array([[0.,0.],
[0.,0.],
[0.,0.]])

As its first parameter, np.zeros() requires a tuple to indicate the shape of the array you want to create, and it returns an array of type float64.

Similarly, to create a filled array, you can use np.ones():

In[16]:A=np.ones((2,3))
...:A
Out[16]:
array([[1.,1.,1.],
[1.,1.,1.]])

It is worth noting that np.ones() also returns an array of type float64.

To create an array with random elements, you can use np.random.rand():

In[17]:A=np.random.rand(3,2)
...:A
Out[17]:
array([[0.8206045,0.54470809],
[0.9490381,0.05677859],
[0.71148476,0.4709059]])

np.random.rand() returns an array containing random elements from 0 to 1, taken from a uniform distribution. Please note that unlike np.zeros() and np.ones(), np.random.rand() does not expect tuples as its parameters.

Similarly, to get an array of random elements from a normal distribution with zero mean and unit variance, you can use np.random.randn():

In[18]:A=np.random.randn(3,2)
...:A
Out[18]:
array([[-1.20019512,-1.78337814],
[-0.22135221,-0.38805899],
[0.17620202,-2.05176764]])

Now that you have created the arrays, you will see how to use them to perform operations.

Perform operations on NumPy arrays

Common Python operations that use addition (+), subtraction (-), multiplication ( ), division (/), and exponent (16128634962bf2 *) operators on If one of the operands is a scalar, the operation will be performed between the scalar and each element of the array.

For example, in order to create a matrix with filled elements equal to 10, you can use np.ones() to multiply the output 10 by using *:

In[19]:A=10*np.ones((2,2))
...:A
Out[19]:
array([[10.,10.],
[10.,10.]])

If both operands are arrays of the same shape, the operation will be performed between the corresponding elements of the array:

In[20]:A=10*np.ones((2,2))
...:B=np.array([[2,2],[5,5]])
...:C=A*B
...:C
Out[20]:
array([[20.,20.],
[50.,50.]])

Here, you multiply each element of matrixA by the corresponding element B of matrix.

To perform matrix multiplication according to the rules of linear algebra, you can use np.dot():

In[21]:A=np.array([[1,2],[3,4]])
...:v=np.array([[5],[6]])
...:x=np.dot(A,v)
...:x
Out[21]:
array([[17],
[39]])

Here, you multiply a 2×2 matrix A, which is named v by a 2×1 vector named v.

You can use the @ operator to get the same result. Starting from PEP465 and Python3.5, both NumPy and native Python support this operator:

In[22]:A=np.array([[1,2],[3,4]])
...:v=np.array([[5],[6]])
...:x=A@v
...:x
Out[22]:
array([[17],
[39]])

In addition to the basic operations of processing matrices and vectors, NumPy also provides some specific functions to process numpy.linalg. However, for these applications, scipy.linalg has some advantages, as you will see below.

Compare scipy.linalg with numpy.linalg

NumPy contains some tools for processing linear algebra applications in the numpy.linalg module. However, unless you don't want to add SciPy as a dependency to your project, it is usually best to use scipy.linalg for the following reasons:

  • As the official document is explained, scipy.linalg contains all the functions of numpy.linalg plus some additional advanced functions, which are not included in numpy.linalg.
  • scipy.linalg always supports BLAS and LAPACK at compile time. These libraries include routines for performing numerical operations in an optimized manner. For numpy.linalg, the use of BLAS and LAPACK is optional. Therefore, depending on how you installed NumPy, the scipy.linalg function may be better than numpy.linalg.

In short, considering that scientific and technical applications generally have no dependency on restrictions, it is usually a good idea to install SciPy and use scipy.linalg instead of numpy.linalg.

In the next section, you will use the scipy.linalg tool to deal with linear systems. You will first understand the basics through a simple example, and then apply these concepts to real problems.

Use scipy.linalg.solve() to solve linear systems

Linear systems can be useful tools for solving several practical and important problems, including those related to vehicle traffic, balanced chemical equations, electrical circuits, and polynomial interpolation.

In this section, you will learn how to use scipy.linalg.solve() to solve linear systems. But before you start writing code, it is important to understand the basics.

Understand linear systems

A linear system , or more precisely, a linear equation system, is a set of straight lines and a set of variable equations. The following is an example of a linear system related to the variables x₁, x₂, and x₃:
image.png

There are three equations involving three variables. In order to have a linear system, the values ​​ķ₁...ķ₉ and b₁...b₃ must be constants.

When there are only two or three equations and variables, you can manually perform calculations, combine equations and find the values ​​of the variables. However, for four or more variables, solving a linear system manually takes a considerable amount of time, and errors often occur.

Practical applications usually involve a large number of variables, which makes it infeasible to solve linear systems manually. Fortunately, there are some tools that can accomplish this difficult task, such as scipy.linalg.solve().

Use scipy.linalg.solve()

SciPy provides scipy.linalg.solve() in a fast and reliable way to solve linear systems. To understand how it works, consider the following system:
image.png

In order to use scipy.linalg.solve(), you first need to write the linear system as matrix product , as shown in the following equation:
image.png

Note that you will get the original equation of the system after calculating the matrix product. The expected input of scipy.linalg.solve() is matrixA and vectorb, and you can define them using NumPy arrays. In this way, you can use the following code to solve system problems:

1In[1]:importnumpyasnp
2...:fromscipy.linalgimportsolve
3
4In[2]:A=np.array(
5...:[
6...:[3,2],
7...:[2,-1],
8...:]
9...:)
10
11In[3]:b=np.array([12,1]).reshape((2,1))
12
13In[4]:x=solve(A,b)
14...:x
15Out[4]:
16array([[2.],
17[3.]])

Here is a breakdown of what is happening:

  • line 1 and line 2 import NumPynp and solve()fromscipy.linalg.
  • Lines 4 to 9 uses the named NumPy array to create the coefficient matrix A.
  • line 11 uses the named NumPy array to create an independent item vector b. To make it a column vector with two rows, use .reshape((2,1)).
  • The 13th and 14th lines call solve() to solve the linear system b represented by A and the result is stored in x and printed out. Please note that solve(), even if all the elements of the original array are integers, will return the solution with floating point components.

If you replace x₁=2 and x₂=3 in the original equation, you can verify that this is the solution of the system.

Now that you have understood the basics of using scipy.linalg.solve(), it is time to understand the practical application of linear systems.

Solving practical problems: BuildingaMealPlan

One type of problem that is usually solved with linear systems is when you need to find the ratio of components needed to obtain a certain mixture. Below, you will use this idea to develop a meal plan and mix different foods to get a balanced diet.

To this end, please consider that a balanced diet should include the following:

  • 170 units of vitamin A
  • 180 units of vitamin B
  • 140 units of vitamin C
  • 180 units of vitamin D
  • 350 units of vitamin E

Your task is to find out the amount of each different food in order to obtain the specified amount of vitamins. In the table below, you can analyze the results of 1 gram of each food according to the unit of each vitamin:

Your task is to find out the amount of each different food in order to obtain the specified amount of vitamins. In the table below, you can analyze the results of 1 gram of each food according to the unit of each vitamin:
image.png

By denoting food 1 as x₁, etc., and considering that you will mix x₁ units of food 1, x2 units of food 2, etc., you can write the expression d for the amount of vitamin A you consume into the combination. Considering that a balanced diet should contain 170 units of vitamin A, you can use the data in the vitamin A column to write the following equation:
image.png

Repeat the same process for vitamins B, C, D, and E, and you will get the following linear system:
image.png

To use scipy.linalg.solve(), you must obtain the coefficient matrix A and the independent term vector b, which are given by:
image.png

Now you just need to use scipy.linalg.solve() to find the quantity x₁,…,x₅:

In[1]:importnumpyasnp
...:fromscipy.linalgimportsolve

In[2]:A=np.array(
...:[
...:[1,9,2,1,1],
...:[10,1,2,1,1],
...:[1,0,5,1,1],
...:[2,1,1,2,9],
...:[2,1,2,13,2],
...:]
...:)

In[3]:b=np.array([170,180,140,180,350]).reshape((5,1))

In[4]:x=solve(A,b)
...:x
Out[4]:
array([[10.],
[10.],
[20.],
[20.],
[10.]])

This means that a balanced diet should include 10 food units 1, 10 food units 2, 20 food units 3, food units 4, and 10 food units 5.

in conclusion

Congratulations! You have learned how to use some linear algebra concepts and how to use scipy.linalg to solve problems involving linear systems. You have seen that vectors and matrices can be used to represent data, and by using linear algebra concepts, you can model real problems and solve them in an efficient way.

In this tutorial, you learned how to:

  • Apply linear algebra concepts to practical problems scipy.linalg
  • Use Python and NumPy to process vectors and matrices
  • Use linear systems to simulate real problems
  • Solve linear systems using scipy.linalg

Click and follow to learn about Huawei Cloud's fresh technology for the first time~


华为云开发者联盟
1.4k 声望1.8k 粉丝

生于云,长于云,让开发者成为决定性力量