Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of NumPy linear algebra?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "what is the use of NumPy linear algebra". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

NumPy is a scientific computing library of Python and is the basis of many libraries, such as Pandas.

Object types in Linear Algebra

The type of object (or data structure) in linear algebra:

Scalars: single number

Vectors: numeric array

Matrices: two-dimensional digital array

Tensor: n-dimensional sequence of N > 2

A scalar is a number. As we will see in the following example, it can be used for vectorization operations.

A vector is a set of numbers. For example, a vector of five elements:

We can use scalars in vectorization operations. Performs the specified action on each element of the vector. For example

The matrix is a two-dimensional vector

It looks like an pandas data box with rows and columns. In fact, pandas data frames are converted into matrices and then input into the machine learning model.

The tensor is an array of N dimensions, where N is greater than 2. Tensor is mainly used in deep learning model whose input data is three-dimensional.

It's hard to express it numerically, but you can think of T as three 3x2-shaped matrices.

The shape method can be used to check the shape of the numpy array.

The size of the array is calculated by multiplying the size of each dimension.

Common matrix terms

If the number of rows is equal to the number of columns, the matrix is called a square matrix. Therefore, the matrix An above is a square matrix.

The unit matrix, expressed as I, is a square matrix, yes on the diagonal, and all the other positions are zeros. The identity function of NumPy can be used to create a unit matrix of any size.

What is special about a unit matrix is that the matrix multiplied by it does not change. In this sense, it is similar to the number 1 in a real number. We will use the unit matrix as an example in the matrix multiplication section of this article.

The inverse matrix of the matrix is the matrix of the unit matrix obtained by multiplying the original matrix.

Not every matrix has an inverse matrix. If a matrix A has an inverse matrix, then it is called invertible or nonsingular.

Point multiplication and matrix multiplication

Point multiplication and matrix multiplication are part of complex machine learning and deep learning models, so it is very valuable to have a comprehensive understanding of them.

The dot product of two vectors is the sum of the product of elements relative to their positions. Multiply the first element of the first vector by the first element of the second vector, and so on. The sum of these products is the dot product. The function that calculates the dot product in NumPy is dot ().

Let's first create two simple vectors in the form of an numpy array and calculate the dot product.

The dot product is calculated as (1: 2) + (2: 4) + (3: 6), that is, 28.

Because we multiply each other in the same position, the length of these two vectors must be the same to get the dot product.

In the field of data science, we mainly deal with matrices. A matrix is a set of row and column vectors that are combined in a structured manner. Therefore, the multiplication of two matrices involves many dot product operations of vectors. It will be clearer if we look at some more examples. Let's first create two 2x2 matrices with NumPy.

The 2x2 matrix has 2 rows and 2 columns. The index of rows and columns starts with 0. For example, the first row of A (a row with an index of 0) is an array of [4jue 2]. The first column of An is the array of [4pc0]. The element of the first row and first column is 4.

We can access a single row, column, or element, as follows:

These are important concepts for understanding matrix multiplication.

The multiplication of two matrices involves the point multiplication between the rows of the first matrix and the columns of the second matrix. The first step is the dot product between the first row of An and the first column of B. The result of this dot product is the element of the matrix obtained at the position [0minute 0] (that is, the first row, the first column).

Therefore, the resulting matrix C will have a (4-0) + (2-4) in the first row and first column. C [0B0] = 18.

The next step is the dot product of the first row of An and the second column of B.

C has a (4-0) + (2-4) in the first row and the second column. C [0BZ 1] = 8.

The first line An is done, so let's start with the second line of An and follow the same steps.

C has a (0,4) + (3x1) in the second row and first column. C [1J 0] = 3.

The last step is the dot product between the second row of An and the second column of B.

C has a (0x 0) + (3x 4) in the second row and second column. C [1] = 12.

We have seen how it is done step by step. All of these operations are done with np.dot:

As you may recall, we have already mentioned that the unit matrix multiplied by any matrix does not change the matrix. Let's give an example.

We also mentioned that when a matrix is multiplied by its inverse matrix, the result is a unit matrix. Let's first create a matrix and then find its inverse matrix. We can use the NumPy function * * linalg.inv () * * to find the inverse of the matrix.

Multiply the inverse matrix C of B by B:

We get the unit matrix.

As we recall in the vector dot product, two vectors must have the same length in order to have a dot product. Every dot product operation in matrix multiplication must follow this rule. The dot product is between the rows of the first matrix and the columns of the second matrix. Therefore, the rows of the first matrix and the columns of the second matrix must be the same length.

The requirement of matrix multiplication is that the number of columns of the first matrix must be equal to the number of rows of the second matrix.

For example, we can multiply an 3x2 matrix by an 2x3 matrix.

The shape of the result matrix will be 3x3, because we do a 3-dot product on each row of A, and A has 3 rows. A simple way to determine the shape of the result matrix is to extract the number of rows from the first matrix and the number of columns from the second matrix:

Multiply 3x2 and 2x3 to return 3x3

Multiply 3x2 and 2x2 to return 3x2

Multiply 2x4 and 4x3 to return 2x3

This is the end of the content of "what is the use of NumPy linear algebra". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report