current position:Home>Using linear systems in python with scipy.linalg

Using linear systems in python with scipy.linalg

2021-08-22 07:48:06 Huawei cloud developer community

Abstract : Apply the concept of linear algebra to practical problems scipy.linalg Use Python and NumPy Dealing with vectors and matrices Use linear systems to simulate practical problems Use to solve linear systems scipy.linalg

This article is shared from Huawei cloud community 《 Use scipy.linalg stay Python Linear systems are used in 》, author :Yuchuan.

linear algebra It is widely used in various disciplines , Once you use vector and linear equation And other concepts to organize information , You can use it to solve many problems . stay Python in , Most routines related to this topic are implemented in scipy.linalg, It provides very fast linear algebraic functions .

In especial , Linear systems It plays an important role in simulating various real-world problems , and scipy.linalg It provides tools to study and solve these problems in an effective way .

In this tutorial , You will learn how to :

  • Apply the concept of linear algebra to practical problems scipy.linalg
  • Use Python and NumPy Dealing with vectors and matrices
  • Use linear systems to simulate practical problems
  • How to use linear systems to solve scipy.linalg

Let's get started !

introduction scipy.linalg

SciPy Is an open source for scientific computing Python library , It includes several modules for common tasks in science and Engineering , For example, linear algebra 、 Optimize 、 integral 、 Interpolation and signal processing . It is SciPy Part of the stack , This includes several other packages for Scientific Computing , for example NumPy、Matplotlib、SymPy、IPython and pandas.

Linear algebra is a branch of mathematics , Involving linear equations and their representations using vectors and matrices . It is a basic discipline used in many engineering fields , It is also a prerequisite for an in-depth understanding of machine learning .

scipy.linalg Including a variety of tools for dealing with linear algebra problems , Includes functions for performing matrix calculations , For example, determinant 、 Inverse matrix 、 The eigenvalue 、 Eigenvectors and singular value decomposition .

In this tutorial , You will use from Some functions of scipy.linalg To solve practical problems involving linear systems . In order to use scipy.linalg, You must install and set up SciPy library , You can use AnacondaPython Distribution and conda Package and environmental management system to complete .

Be careful : To learn about Anaconda and conda For more information , Please check in Windows Set up Python Machine learning .

First , Create a conda Environment and activate it :

$condacreate--namelinalg
$condaactivatelinalg

Activate conda After environment , Your prompt will show its name linalg. Then you can install the necessary packages in the environment :

(linalg)$condainstallscipyjupyter

After executing this order , It should take some time for the system to determine the dependencies and continue the installation .

Be careful : except SciPy, You will also use JupyterNotebook Running code in an interactive environment . This is not mandatory , But it helps to deal with numerical and scientific applications .

About using JupyterNotebooks Review , Please check out JupyterNotebook: brief introduction .

If you prefer to use 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 Show hidden

In the open JupyterNotebook Before , You need to register condalinalg Environmental Science , So that you can use it as a kernel to create Notebook. So , stay linalg When the environment is activated , Run the following command :

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

Now you can open... By running the following command JupyterNotebook:

$jupyternotebook

Load... In browser Jupyter after , Create a new laptop by clicking →linalg, As shown in the figure below :

Laptop in , You can test whether the installation is successful through the imported scipy package :

>>>

In[1]:importscipy

Now you have finished setting up the environment , You'll see how in Python Use vectors and matrices in , This is the use of scipy.linalg Fundamentals of linear algebra applications .

Use NumPy Dealing with vectors and matrices

A vector is a mathematical entity used to represent physical quantities with both size and direction . It is a basic tool for solving engineering and machine learning problems , Just Like a matrix , Used to represent applications such as vector transformation .

NumPy yes Python The most commonly used library for dealing with matrices and vectors in , Used for processing scipy.linalg Linear algebra applications . In this section , You'll learn the basics of using it to create and manipulate matrices and vectors .

To start working with matrices and vectors , You need to JupyterNotebook The first thing to do in is to import numpy. The usual approach is to use aliases np:

>>>

In[2]:importnumpyasnp

To represent matrices and vectors ,NumPy Use one called ndarray.

To create a ndarray object , You can use np.array(), It requires an array like object , For example, lists or nested lists .

for example , Suppose you need to create the following matrix :

To use NumPy Create it , You can use np.array(), Provide a nested list of elements in 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 might notice ,NumPy Provides a visual representation of the matrix , You can identify its columns and rows .

It is worth noting that ,NumPy The elements of the array must be of the same type . You can use the following methods to check NumPy The type of array .dtype:

>>>

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

Because of all the elements A Are integers. , So the array is used type Created int64. If one of the elements is float, Will use type Create array float64:

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 ndarray The size of the object , You can use .shape. for example , Dimensions to be checked A, You can use A.shape:

>>>

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

As expected ,A The dimension of a matrix is 3×2 because A There are three rows and two columns .

When dealing with problems involving matrices , You usually need to use transpose , It exchanges the columns and rows of the matrix .

To transpose by ndarray A vector or matrix represented by an object , You can use .transpose() or .T. for example , You can get the turn A use A.T:

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

Through transposition , Column A Into a line ,A.T The row becomes a column .

To create a vector , You can use np.array(), Provide a list of vector elements :

>>>

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

To check the dimension of the vector , You can .shape Use as before :

>>>

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

Please note that , The shape of this vector is (3,)andnot(3,1)or(1,3). This is a NumPy function , For those who are used to MATLAB People who . stay NumPy in , You can create a one-dimensional array , for example v, This can cause problems when performing operations between matrices and vectors . for example , Transpose operation has no effect on one-dimensional array .

Whenever you provide a parameter similar to a one-dimensional array np.array(), The resulting array will be a one-dimensional array . To create a two-dimensional array , You must provide parameters similar to a two-dimensional array , For example, nested lists :

>>>

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

In the above example , Size v yes 1×3, It 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)

under these circumstances , Size v by 3×1, It corresponds to the size of a two-dimensional column vector .

Creating vectors using nested lists can be laborious , Especially for the most used column vectors . As an alternative , You can create a one-dimensional vector , Provide a list of planes for np.array, And for .reshape() change ndarray The dimensions of objects :

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

In the example above , You use from .reshape() shape (3,1) Get the column vector of the shape for the one-dimensional vector (3,). It is worth mentioning that ,.reshape() The number of elements of the new array is expected to be compatible with the number of elements of the original array . let me put it another way , 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 case , You can also be in .reshape() Use... Without explicitly defining the number of rows of the array :

>>>

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

ad locum ,-1 The parameters you provided .reshape() Indicates that the new array has only one column and the required number of rows , As specified by the second parameter . under these circumstances , Because the original array has three elements , Therefore, the number of rows in the new array will be 3.

in application , You often need to create zero 、 A matrix of one or random elements . So ,NumPy Provides some convenient functions , Next you'll see these functions .

Use the convenience function to create an array

NumPy It 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() You need a tuple to indicate the shape of the array you want to create , It returns an array of type float64.

Again , 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() It 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 a containing from 0 An array of random elements to 1, Taken from a uniform distribution . Please note that , And np.zeros()and Different np.ones(),np.random.rand() It does not expect tuples as its parameters .

Again , 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 you have created the array , You'll see how to use them to perform operations .

Yes NumPy The array performs operations

Use addition on an array (+)、 Subtraction (-)、 Multiplication (*)、 division (/) And the index (**) Common of operators Python Operations are always performed by element . If one of the operands is scalar , The operation will be performed between the scalar and each element of the array .

for example , In order to create a matrix of filled elements, it is equal to 10, You can use np.ones() The output of and multiplication 10 Use *:

>>>

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.]])

ad locum , You will matrixA Multiply each element of matrix The corresponding elements of B.

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

ad locum , You multiply by one 2×2 matrix A, The matrix consists of a named 2×1 Vector naming v.

You can use @ Operator gets the same result , from PEP465 and Python3.5 Start ,NumPy And this machine Python All support this operator :

>>>

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

In addition to the basic operations of dealing with matrices and vectors ,NumPy Some specific functions are also provided to handle numpy.linalg. however , For these applications , it scipy.linalg It has some advantages , You will see below .

Compare scipy.linalg use numpy.linalg

NumPy stay numpy.linalg The module contains some tools for dealing with linear algebra applications . however , Unless you don't want to SciPy Add to the project as a dependency , Otherwise, it is usually best to use scipy.linalg, Here's why :

  • Due to the interpretation of official documents ,scipy.linalg Contains all the functions numpy.linalg Plus some additional advanced features , Not included in numpy.linalg.
  • scipy.linalg Compile time always supports BLAS and LAPACK, These libraries include routines for performing numerical operations in an optimized manner . about numpy.linalg,BLAS and LAPACK Is optional . therefore , According to your installation NumPy The way ,scipy.linalg Functions may be better than numpy.linalg.

All in all , Considering that the application of science and technology generally does not rely on restrictions , It's usually a good idea , install SciPy And use scipy.linalg Instead of numpy.linalg.

In the next section , You will use scipy.linalg Tools to deal with linear systems . You'll start with a simple example to understand the basics , Then apply these concepts to practical problems .

Use scipy.linalg.solve() Solving linear systems

Linear systems can be useful tools for solving several practical and important problems , Including traffic with vehicles 、 Equilibrium chemical equation 、 Problems related to circuit 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's important to know the basics .

Understand linear systems

nail Linear systems , Or more precisely , A system of linear equations , Is a set of straight lines and a set of variable equations . Here are the variables associated with x₁、x₂ and x₃ Examples of related linear systems :

Here are three equations involving three variables . In order to have a linear system , value ķ₁...ķ₉ and b₁...b₃ Must be constant .

When there are only two or three equations and variables , Calculations can be performed manually 、 Combine the equations and find the values of the variables . however , For four or more variables , It takes quite a long time to solve the linear system manually , And often make mistakes .

Practical applications usually involve a large number of variables , This makes it infeasible to solve the linear system manually . Fortunately, , There are some tools that can do this difficult job , for example scipy.linalg.solve().

Use scipy.linalg.solve()

SciPy Provide scipy.linalg.solve() Solve linear systems in a fast and reliable way . To understand how it works , Please consider the following systems :

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

Please note that , You will get the original equation of the system after calculating the matrix product .scipy.linalg.solve() The expected input to the solution is matrixA and vectorb, You can use NumPy Arrays define them . such , 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's a breakdown of what's happening :

  • The first 1 Xing He 2 That's ok Import NumPynp as well as solve()fromscipy.linalg.
  • The first 4 To 9 That's ok Use... Named NumPy Array to create a coefficient matrix A.
  • The first 11 That's ok Use... Named NumPy Array creates an independent item vector b. To make it a column vector with two rows , Please use .reshape((2,1)).
  • The first 13 Xing He 14 That's ok call solve() Solution by A And characterization of linear systems b, The results are stored in x, And print it out . Please note that solve(), Even if all elements of the original array are integers , It also returns a solution with floating-point components .

If you take the in the original equation x₁=2 and x₂=3 Replace , Then it can be verified that this is the solution of the system .

Now you know the basics of using scipy.linalg.solve(), It's time to understand the practical application of linear systems .

Solve practical problems :BuildingaMealPlan

One type of problem usually solved using linear systems is when you need to find the proportion of components needed to obtain a mixture . below , You will use this idea to make a meal plan , Mix different foods to get a balanced diet .

So , Please consider that a balanced diet should include the following :

  • 170 Unit vitamins A
  • 180 Unit vitamins B
  • 140 Unit vitamins C
  • 180 Unit vitamins D
  • 350 Unit vitamins E

Your task is to find out the quantity of each different food , To get the specified amount of vitamins . In the table below , You can analyze by the unit of each vitamin 1 The result of grams of each food :

Your task is to find out the quantity of each different food , To get the specified amount of vitamins . In the table below , You can analyze by the unit of each vitamin 1 The result of grams of each food :

By putting food 1 Expressed as x₁ etc. , And consider that you will mix x₁ Unit food 1、x2 Unit food 2 wait , You can write down the vitamins you take A Expression of quantity d Enter the portfolio . Consider that a balanced diet should include 170 A unit of vitamin A, you ​​ You can use vitamins A Write the following equation for the data in the column :

Yes, vitamins B、C、D and E Repeat the same process , You will get the following linear system :

To use scipy.linalg.solve(), You must obtain the coefficient matrix A And independent term vectors b, They are given by :

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 suggests that a balanced diet should include 10 Food unit 1、 food 10 Company 2、20 food 20 Company 3、 Food unit 4 and 10 Food unit 5.

Conclusion

Congratulations ! You have learned how to use some concepts of linear algebra and how to use scipy.linalg To solve problems involving linear systems . You've seen that vectors and matrices can be used to represent data , And by using the concept of Linear Algebra , You can model real problems and solve them in an effective way .

In this tutorial , You learned how to :

  • Apply the concept of linear algebra to practical problems scipy.linalg
  • Use Python and NumPy Dealing with vectors and matrices
  • Use linear systems to simulate practical problems
  • Use to solve linear systems scipy.linalg

 

Click to follow , The first time to learn about Huawei's new cloud technology ~

copyright notice
author[Huawei cloud developer community],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2021/08/20210822074727789w.html

The sidebar is recommended

guess what you like

Random recommended