Abstract: Applying linear algebra concepts to practical problems scipy.linalg uses Python and NumPy to handle vectors and matrices using linear systems to simulate practical problems using solving linear systems scipy.linalg

This article is from the huawei cloud community using scipy.linalg using linear systems in Python by Yuchuan.

Linear algebra is widely used in a variety of disciplines, and once you organize information using concepts like vectors and linear equations, you can use it to solve many problems. In Python, most routines related to this topic implement scipy.linalg in Python, which provides very fast linear algebra functionality.

In particular, linear systems play an important role in simulating various real-world problems, and Scipy.Linalg provides tools to study and solve these problems in an effective manner.

In this tutorial, you will learn how to:

  • Applying linear algebra concepts to practical problems scipy.linalg

  • Use Python and NumPy to handle vectors and matrices

  • Use linear systems to simulate real problems

  • How to solve Scipy.linalg using linear systems

Let’s get started!

Introductory 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 deals with linear equations and their representations using vectors and matrices. It is a foundational discipline for 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 from, scipy.linalg, to solve practical problems involving linear systems. In order to use scipy.linalg, you must install and set up the Scipy library, which you can do using the AnacondaPython distribution and the Conda package and environment management system.

** Note: ** To learn more about Anaconda and Conda, see Setting Up Python for Machine Learning on Windows.

First, create a Conda environment and activate it:

$ conda create --name linalg
$ conda activate linalg
Copy the code

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

(linalg) $ conda install scipy jupyter
Copy the code

After executing this command, it should take some time for the system to determine the dependencies and proceed with 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 with numerical and scientific applications.

For a refresher on using JupyterNotebooks, see JupyterNotebook: introduction.

If you prefer to read this article using a different Python distribution and PIP package manager, expand the collapsible section below to see how to set up your environment.

Set up the environment to show hide using PIP

Before opening JupyterNotebook, you need to register the Condalinalg environment so that you can use it as a kernel to create notebooks. To do this, run the following command with the linalg activated environment:

(linalg) $ python -m ipykernel install --user --name linalg
Copy the code

You can now open JupyterNotebook by running the following command:

$ jupyter notebook
Copy the code

After loading Jupyter in your browser, create a new laptop by clicking new → Linalg, as shown below:

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

In [1]: import scipy
Copy the code

Now that you have finished setting up your environment, you will see how to use vectors and matrices in Python, which are the foundation of linear algebra applications using Scipy.Linalg.

Use NumPy to handle vectors and matrices

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

NumPy is the most common library in Python for working with matrices and vectors and is used to work with scipy.linalg linear algebra applications. In this section, you’ll 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 import Numpy. The usual method is to use the alias NP: >>>

In [2]: import numpy as np
Copy the code

To represent matrices and vectors, NumPy uses something 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:

To create it using NumPy, you can use np.array(), providing 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]])Copy the code

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

It is important to note that the elements of the NumPy array must be of the same type. You can check the type of the NumPy array using the following method: dtype: >>>

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

Since all elements A of are integers, the array is int64 created with type. If one of the elements is float, the array float64 is 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')Copy the code

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

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

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

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

To transpose a vector or matrix represented by an NDARray object, you can use.transpose() or. T. For example, you can get the transfer A using A.T:

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

By transposing, column A becomes A row, and row A.T becomes A column.

To create a vector, you can use np.array(), which provides a list of vector elements: >>>

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

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

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

Notice that this vector has the shape (3,)andnot(3,1)or(1,3). This is a NumPy feature for those who are used to using MATLAB. In NumPy, it is possible to create one-dimensional arrays such as v, which can cause problems when performing operations between matrices and vectors. For example, transpose has no effect on a one-dimensional array.

Whenever you supply np.array() as an argument like a one-dimensional array, the resulting array will be a one-dimensional array. To create a two-dimensional array, you must provide parameters like a two-dimensional array, such as a nested list: >>>

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

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

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

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

Creating vectors using nested lists can be taxing, especially for the most-used column vectors. 0 As an alternative, you could create a one dimensional vector for providing a flat list of Np.array and 0 for.0 () To change the dimension of the ndarray object: 0

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

0 In the example above, you’re using the column vector (3,) that takes shape from a 1 dimensional vector of 0 0 shape(3,1). It’s worth noting that.0 () expects the number of elements in the new array to be 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 equal the number of elements in the original array.

In this example, you can also use 0 in case.0 () doesn’t explicitly define the number of array lines: >>>

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

0 In this case, -1 The argument you supplied.0 () indicates that the new array has only one column of required rows, as specified in the second argument In this case, since the original array had three elements, the new array will have three rows.

In practice, you often need to create matrices of zero, one, or random elements. NumPy provides some handy functions for this purpose, which you’ll see next.

Create arrays using convenience functions

NumPy also provides some handy 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.]])Copy the code

As its first argument, np.zeros() takes a tuple to indicate the shape of the array you want to create, and returns an array of type FLOAT64.

Again, to create a populated array, you can use np.ones() : >>>

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

Notably, 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) ... : Out[17]: Array ([[0.8206045, 0.54470809], [0.9490381, 0.05677859], [0.71148476, 0.4709059]])Copy the code

Np.random.rand () returns an array 1 containing random elements from 0 to 1, taken from the uniform distribution. Please note that with

Np.zeros ()and different np.ones(), np.random.rand() it does not expect tuples as its arguments.

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]])Copy the code

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

Perform operations on NumPy arrays

Common Python operations on arrays using the addition (+), subtraction (-), multiplication (*), division (/), and exponent (**) operators are always executed on an element. If one of the operands is a scalar, the operation is performed between the scalar and each element of the array.

For example, to create a matrix of fill elements equal to 10, you can use np.ones() from the output of the multiplication and 10 using * : >>>

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

If both operands are arrays of the same shape, the operation is 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.]])Copy the code

Here, you multiply each element of the matrixA by the corresponding element B of the 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]])Copy the code

Here, you multiply by A 2 by 2 matrix A named by A 2 by 1 vector named V.

You can get the same result using the @ operator, which is supported by both NumPy and native Python starting with PEP465 and Python3.5:

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

In addition to handling the basic operations of matrices and vectors, NumPy provides specific functions to handle numpy.linalg. However, for these applications, it scipy.linalg has some advantages, as you’ll see below.

Compare scipy. Linalg with numpy. Linalg

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

  • As explained in official documentation, scipy.linalg includes all the features of numpy.linalg, plus some additional advanced features not included in numpy.linalg.

  • Scipy. linalg has always been compiled to support BLAS and LAPACK, libraries that include routines for performing numerical operations in an optimized manner. The use of BLAS and LAPACK is optional for numpy.linalg. Therefore, depending on how you install NumPy, the scipy.linalg function may be better than numpy.linalg

In conclusion, 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 work with linear systems. You’ll first learn the basics with a simple example, then apply these concepts to a real-world problem.

Use scipy.linalg.solve() to solve the linear system

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

In this section, you will learn how to solve linear systems using scipy.linalg.solve(). But before you start writing code, it’s important to know the basics.

Understanding linear systems

A A linear system, or more precisely, a linear equation system, is a set of lines and a set of equations of variables. The following are examples of linear systems associated with variables X ₁, X ₂, and X ₃ :

There are three equations involving three variables. In order to have a linear system, value k ₁… ₁ ķ ₉ and b… B ₃ must be a constant.

When there are only two or three equations and variables, you can manually perform the calculation, combine the equations, and find the values of the variables. However, for four or more variables, solving a linear system manually takes quite a long time and is often error-prone.

Practical applications usually involve a large number of variables, making it impossible to solve linear systems manually. Fortunately, there are tools that can do the hard work, such as scipy.linalg.solve().

Use scipy. Linalg. Solve ()

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

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

Note that you will derive the original equation for the system after calculating matrix products. The inputs scipy.linalg.solve() expects to solve are matrixA and Vectorb, which you can define using the NumPy array. In this way, you can solve system problems with the following code: >>>

1In [1]: import numpy as np 2 ... : from scipy.linalg import solve 3 4In [2]: A = np.array( 5 ... : [6...: [3, 2], 7...: [2, -1], 8...:] : ) 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.]])Copy the code

Here’s a breakdown of what’s happening:

  • Lines 1 and 2 import NumPy NP and solve() fromScipy.linalg.

  • Lines 4 through 9 create the coefficient matrix A with A NumPy array named.

  • Line 11 creates the independent item vector b with the NumPy array named. 0 to make it a column vector with two rows, use.0 ((2,1))

  • Lines 13 and 14 call solve() to solve the linear system B represented by A and, with the results stored in x and printed out. Note that solve(), even if all elements of the original array are integers, returns a solution with floating point components.

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

Now that you know the basics of use, scipy.linalg.solve(), it’s time to see linear systems in action.

Solve practical problems: BuildingaMealPlan

One kind of problem that is usually solved with linear systems is when you need to find the proportion of components needed to get a mixture. Below, you’ll use this idea to create a meal plan, mixing different foods to get a balanced diet.

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

  • 170 units of vitamin A

  • 180 units of B vitamins

  • 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 to get the specified amount of vitamins. In the table below, you can analyze the results of 1 gram of each food by unit of each vitamin:

By expressing food 1 as X ₁, etc., and taking into account that you will be mixing food 1 in units X ₁, food 2 in units X2, etc., you can write the expression D for the amount of vitamin A you are consuming into the combination. Given that A balanced diet should contain 170 units of vitamin A, you can write the following equation using the data in the vitamin A column:

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

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

Now you simply use scipy.linalg.solve() to find the number x₁,… , x ₅ : > > >

In [1]: import numpy as np ... : from scipy.linalg import solve 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.]])Copy the code

This suggests that a balanced diet should consist of 10 food units 1, 10 food units 2, 20 food units 20 3, food units 4 and 10 food units 5.

conclusion

A: congratulations! You’ve learned how to use some linear algebra concepts 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 linear algebra concepts, you can model real problems and solve them in an efficient way.

In this tutorial, you learned how to:

  • Applying linear algebra concepts to practical problems scipy.linalg

  • Use Python and NumPy to handle vectors and matrices

  • Use linear systems to simulate real problems

  • Using the solution of linear system scipy. Linalg

Click to follow, the first time to learn about Huawei cloud fresh technology ~