Python Scientific Computing Library numpy-matrix operations

Source: Internet
Author: User
Tags sin

The core of the NumPy library is the matrix and its operations.

Use the array () function to transform Python's array_like data into an array form, using the matrix () function to transform into a matrix.

Based on the habit, in the actual use of more commonly used arrays and less matrix to represent matrices.

Then you can use the relevant matrix to transport it.

Import= [[1,2,3],[4,5,5],[4,5,5# number of rows in a multidimensional array print#  Output element type # Alternatively, you can also use the slice method to work with the array

Then comes the Ufunc (Universal function) operation, which is a function that can operate on each element of an array. Many of the Ufunc functions built into the NumPy are implemented at the C language level, so they are computationally fast.

1 >>> x = np.linspace (0, 2*np.pi,2 )The sine calculation of each element in the array x, Returns a new array of the same size 3 >>> y = np.sin (x)4 >>> y5 Array ([  0.00000000e+00,   6.42787610e-01,   9.84807753e-01,6          8.66025404e-01,   3.42020143e-01,  -3.42020143e-01,7         -8.66025404e-01,  -9.84807753e-01,  -6.42787610e-01,8         -2.44921271e-16])

Some general-purpose operational functions are:

Np.sin ()

Np.cos ()

Np.add (A, B)

A.sum (axis=0,1) #axis为0计算全部数据的和, 1 calculates the data by row and

And so on other matrices can participate in the calculation of data.

A = Array ([[[1,2,3],[2,3,4]])

Array (list): Creates a matrix or high-dimensional vector, such as a = array ([[[0,1,2,3],[4,5,6,7]]), and the passed-in parameter can also be a tuple

Shape: A tuple that represents a vector size, such as a a.shape result of a tuple, as a shape (2,3)

Ndim: A dimension that represents a matrix or a high-dimensional vector, such as a a.ndim of 2 for matrix A

Size: Indicates the total number of vector elements

ItemSize: Indicates the number of bytes that the element occupies

Nbytes: Indicates the number of bytes that the vector occupies

Real: The real part of all elements, returned or matrix form

Imag: The imaginary part of all elements, returned or matrix form

Flat: A one-dimensional array representing matrices or high-dimensional vectors (commonly used for sequential traversal)

T: Represents the Matrix's transpose matrix (also applicable to high-dimensional vectors), for example: A. T

Zeros (SHAPE): Creates a full 0 matrix or high-dimensional vector, such as a = zeros ((2,3))

Ones (Shape): Creates a full 1 matrix or high-dimensional vector, such as a = Ones ((2,3))

Add (Matrix): Add the corresponding elements of the matrix, the result is equivalent to directly with the plus sign

Dot (Matrix)
Matrix multiplication, note that the "can multiply" requirement must be met
If it is *, you should note:

1AAA = Array ([[[10,9,8],[7,6,5],[42,33,2]])2A = aaa.min (0)#take the minimum value of each column and return an array3Aaa*aaa#equivalent to aaa**2:4 5 #[[+], Bayi,6 #[+, + ],7 #[1764, 1089, 4]]8 9Aaa*a#AAA Each line element is multiplied by a, and the result isTen  One #[[+] , A #[A] , - #[294, 198, 4]] -  theA*aaa#The result is the same as -3*aaa#multiply each element in AAA by 3

Reshape (SHAPE)
Get a matrix that changes shape, such as a = array ([[[1,2,3],[4,5,6]]). The result of reshape ((3,2)) is [[1,2],[3,4],[5,6]]. Note that the size of the matrix cannot be changed, i.e. the number of matrix elements represented by the reshape parameter must be equal to the number of elements of the original matrix.
Transpose ()
To get the matrix transpose matrix, A.transpose () is equivalent to a. T
Swapaxes (D1,D2)
Swap a given two dimensions
Flatten ()
Returns the corresponding one-dimensional vector, for example:

1 AAA = Array ([[[10,9,8],[7,6,5],[42,33,2]])2aaa.flatten ()3 # The return value is:  4 Array ([Ten,  9,  8,  7,  6,  5, A, 2  ])

ToList ()
Get the result of converting matrix object to list

1 aa = aaa.tolist ()2AA returns to the list type (each row is also a sub list):3 [[10, 9, 8], [7, 6, 5], [42, 2]]4aa[0  ]5# return:6 [10, 9, 8]

MIN (axis)
Gets the minimum value in all elements. When given an axis value (min (0) or min (axis=0)), the minimum value (get array) is calculated at that coordinate.
For example:

1 AAA = Array ([[[10,9,8],[7,6,5],[42,33,2]])  2    3 returns: Minimum value for all elements in the AAA matrix  4 Result: 2 5    6 returns: The minimum value of the element in all columns in the AAA matrix 7 results are: Array ([7, 6, 2 ])  8 aaa.min (1)  9 returns: The minimum value of the element in all rows in the AAA matrix  The result is: Array ([8, 5, 2])

Max (axis)
Gets the minimum value in all elements. Default parameter axis action is the same as min ()
SUM ()
Get the sum of the elements of the array and get a number.

You can also aaa.sum (axis) for each row or the sum of the elements of each column, respectively.

Cumsum ()
A one-dimensional array that is cumulative and, in turn, adds an element sum.
For example:

1 aaa.cumsum () 2 Result: Array ([, 120, 87, +, +,  122])

Prod ()
Get the product of all the elements of the array, which is a number. It is also possible to aaa.sum (axis) for the product of each row or of the elements of each column, respectively.

Cumprod ()
The cumulative product, the example form and the above cumsum () the same, the two functions can also be divided into the cumulative sum of coordinates and cumulative multiplication.
Mean ()
Get the average of the elements
All ()
Returns true if all elements are true;
Any ()
Returns true if all elements are true as long as there is one, otherwise false.
characteristic value
Linalg.eigvals ()
Returns the characteristic value of a
Linalg.eig (A)
Returns the eigenvalues and eigenvectors of a, for example (eval, Evec) = Linalg.eig (a), where the diagonal element of eval is the individual eigenvalues of a, and Evec corresponds to each column as the corresponding eigenvector.

1>>> A = Array ([[ -1,1], 0],2[ -4,3, 0],3[1,0,2]])4>>> eval, Evec =Linalg.eig (a)5>>>Eval6Array ([2., 1., 1.])7>>>Evec8Array ([[0]. , 0.40824829, 0.40824829],9[0., 0.81649658, 0.81649658],Ten[1.,-0.40824829,-0.40824829]])

That is, the eigenvectors are λ1=2 (0,0,1) and Λ2=λ3=1 (0.4,0.8,-0.4)

Python Scientific Computing Library numpy-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.