numpy-fast processing of data--matrix operations

Source: Internet
Author: User

This digest is from the "scientific calculation using Python", the copyright belongs to the original author.

1. numpy-fast processing of data--ndarray objects--array creation and access

2. numpy-Fast processing data--ndarray objects--access to multidimensional arrays, structure array access, memory alignment, NUMPY memory structure

3. numpy-Fast processing Data--ufunc operation--broadcast--ufunc method

Next, we introduce matrix operations

NumPy does not use matrix operations by default, and if you want to perform a matrix operation on an array, you need to call the appropriate function

Matrix Object

The NumPy library provides the matrix classes, which are created using the Matrices class, and their subtraction operations are computed by default in a matrix way, so the usage is very similar to MATLAB. But because Ndarray and matrix objects exist simultaneously in NumPy, it is easy for users to confuse the two. This is against Python's "explicit better than implicit" principle, so it is not recommended to use the matrix in more complex programs. Here is an example of using the matrix:

1>>>ImportNumPy as NP2>>> a = Np.matrix ([[1,2,3],[5,5,6],[7,9,9]])3>>> a**-1#inverse matrix of a4Matrix ([[-0.6, 0.6,-0.2       ],5[-0.2,-0.8, 0.6       ],6[0.66666667, 0.33333333,-0.33333333]])7>>> A * a**-1#The product of the inverse matrix of A and a, the result is the unit array8Matrix ([[[1.00000000e+00, 0.00000000e+00, 0.00000000e+00],9[4.44089210e-16, 1.00000000e+00, 4.44089210e-16],Ten[0.00000000e+00, -4.44089210e-16, 1.00000000e+00]])

If you do not use a matrix object and you think of a two-dimensional array as a matrix, you need to use the DOT function for the calculation. For a two-dimensional array, it calculates the product of a matrix, and for a one-dimensional array, it calculates its dot product. When you need to use a one-dimensional array as a column vector or a row vector for matrix operations, it is recommended to convert one-dimensional arrays to two-dimensional arrays using the reshape or shape function:

1>>> a = Np.array ([1, 2, 3])2>>> A.shape#A is a one-dimensional array3(3,)4>>> A.shape = (-1, 1)#use shape to directly modify the dimensions of a5>>>a6Array ([[1],7[2],8[3]])9>>> A.reshape (1,-1)#using reshape is also possible, but his return value changes the shape of a, and a does not change itself .TenArray ([[[1, 2, 3]]) One>>>a AArray ([[1], -[2], -[3]])
    • dot : For two one-dimensional arrays, the two arrays are calculated to correspond to the product of the subscript element (mathematically called the inner product); For a two-dimensional array, the matrix product of two arrays is computed; for multidimensional arrays, its general formula is as follows. That is, each element in the result array is the product of all the elements on the last dimension of array A and all the elements on the penultimate second of array B:

Dot (A, b) [i,j,k,m] = SUM (A[I,J,:] * b[k,:,m])

Two three-dimensional array multiplication

1>>> a = Np.arange (a). Reshape (2,3,2)2>>>a3Array ([[[[[0], 1],4[2, 3],5[4, 5]],6 7[[6, 7],8[8, 9],9[10, 11]]])Ten>>> B = Np.arange (12,24). Reshape (2,2,3) One>>>b AArray ([[[[[[12], 13, 14], -[15, 16, 17]], -  the[[18, 19, 20], -[21, 22, 23]]]) ->>> C =Np.dot (A, b) ->>>C +Array ([[[[[[] [[15], 16, 17], -[21, 22, 23]], +  A[[69, 74, 79], at[99, 104, 109]], -  -[[123, 132, 141], -[177, 186, 195]]], -  -  in[[[177, 190, 203], -[255, 268, 281]], to  +[[231, 248, 265], -[333, 350, 367]], the  *[[285, 306, 327], $[411, 432, 453]]]])Panax Notoginseng>>>C.shape -(2, 3, 2, 3)

The result of dot product C can be seen as the product of multiple sub-matrices of array A, B:

 1  >>> np.alltrue (c[0,:,0,:] == Np.dot (a[0],b[0])  true  3  >>> np.alltrue (c[1,:,0,:] = = Np.dot (A[1],b[0])  4  true  5  >>> np.alltrue (c[0,:,1,:] = = Np.dot (A[0],b[1 6  7  >>> np.alltrue (c[1,:,1,:] = = Np.dot (A[1],b[1 8  True 
    • inner : As with the dot product, for two one-dimensional arrays, the sum of the two arrays corresponding to the subscript element is computed, and for a multidimensional array, each element in the resulting array that it evaluates is: the inner product of the last dimension of the array A and B, So the last dimension of arrays A and B must be the same length:

1 Inner (A, b) [i,j,k,m] = SUM (a[i,j,:]*b[k,m,:])
1>>> a = Np.arange (a). Reshape (2,3,2)2>>> B = Np.arange (12,24). Reshape (2,3,2)3>>> C =Np.inner (A, b)4>>>C.shape5(2, 3, 2, 3)6>>> c[0,0,0,0] = =Np.inner (a[0,0],b[0,0])7 True8>>> c[0,1,1,0] = = Np.inner (a[0,1],b[1, 0])9 TrueTen>>> c[1,2,1,2] = = Np.inner (a[1,2],b[1,2]) OneTrue
    • outer : Only one-dimensional arrays are evaluated, and if the passed-in parameter is a multidimensional array, the array is flattened into a one-dimensional array before the operation. The matrix product of a column vector and a line vector computed by the outer product:

1 >>> np.outer ([1,2,3],[4,5,6,7])2 Array ([[4,  5,  6,  7], 3        [8, ten, +], 4        [12, 15, 18, 21]])

Some of the more advanced operations in the matrix can be found in the linear algebraic sub-library linalg of NumPy. For example, the INV function calculates the inverse matrix, and the solve function can solve the multivariate one-time equation group. Here is an example of the solve function:

1 >>> a = Np.random.rand (10,10)2 >>> b = Np.random.rand (Ten)  3 >>> x = Np.linalg.solve (A, B)

The solve function has two parameters A and B. A is a two-dimensional array of n*n, and B is a one-dimensional array of length n, and the solve function finds a one-dimensional array x of length n, so that the matrix product of a and x is exactly equal to B, and the array x is the solution of the multivariate one-time equation group.

numpy-fast processing of data--matrix operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.