Matrix processing in NumPy

Source: Internet
Author: User

The matrix objects in the NumPy module are Numpy.matrix, including the processing of matrix data, calculation of matrices, and basic statistical functions, transpose, reversibility, etc., including the processing of complex numbers, in the Matrix object. Class Numpy.matrix (Data,dtype,copy): Returns a matrix where data is Ndarray object or character form; Dtype: type;copy for data: bool type.

>>> a = Np.matrix (' 1 2 7; 3 4 8; 5 6 9 ') >>> a #矩阵的换行必须是用分号 (;) separated, the internal data must be in the form of a string ("), Moment Matrix ([[ 1, 2, 7], #阵的元素之间必须以空格隔开. [3, 4, 8],[5, 6, 9]]) >>> B=np.array ([[[1,5],[3,2]]) >>> X=np.matrix (b) #矩阵中的data可以为数组对象. >>> Xmatrix ([[1, 5],[3, 2]])

Properties of the Matrix object:

Matrix. T transpose: Returns matrix of transpose matrices matrix. H Hermitian (conjugate) Transpose: Returns a matrix of conjugate elements of a complex matrix. I inverse: Returns the inverse matrix matrix of matrices. A Base array: Returns arrays based on matrices method of the Matrix object:All ([axis, out]): Determines whether all elements of the matrix are true along a given axis (not 0 is true) any ([axis, out]): Determines whether a matrix element is true along the direction of a given axis, as long as one element is true. Argmax ([axis, out]): Returns the index of the largest element (the position of the largest element) along the direction of the given axis. Argmin ([axis, out]): Returns the index of the smallest element (the position of the smallest element) along the direction of the given axis argsort ([axis, kind, Order]): Returns the sorted index matrix Astype (dtype[, order, casting, Subok, copy]): Copies the matrix data and the data type to the specified data type Byteswap (inplace) Swap the bytes of the array elementschoose (choices[, out, mode]): Gets a new data matrix (index from choices given) clip (A_min, a_max[, out]) based on the given index: Returns a new matrix, The element larger than the given element is A_max, the smaller is a_mincompress (condition[, axis, out]): Returns the matrix that satisfies the condition Conj (): Returns the complex conjugate complex conjugate (): Returns the conjugate complex element copy of all complex numbers ( [Order]) : Copies a matrix and assigns it to another object, B=a.copy () Cumprod ([axis, Dtype, out]): Returns the cumulative matrix of elements along the specified axis cumsum ([axis, Dtype, out]): Returns the element accumulation and matrix diagonal along the specified axis ([offset, axis1, Axis2]): Returns the data of the diagonal in The matrix dot (b[, out]): two points of a matrix dump (file): The matrix is stored as a specified file, can be pickle.loads () or numpy.loads () such as: A.dump (' D:\\a.txt ') dumps (): The data of the matrix is dumped into a string. Fill (value): Fills all elements in the matrix with the specified Valueflatten ([order]): Transforms the matrix into a one-dimensional form, but still a matrix object Geta (): Returns itself, but returns getA1 as Ndarray () : Returns a flattened (one-dimensional) array (Ndarray) Geth (): Returns its own conjugate complex transpose matrix Geti () : Returns the inverse matrix of itself Gett (): Returns its own transpose matrix Max ([axis, out]): Returns the maximum value of the specified axis mean ([axis, Dtype, out]): Returns its mean min ([axis, out]) along the given axis direction : Returns the minimum value of the specified axis nonzero (): Returns the index matrix of a non-0 element prod ([axis, Dtype, out]): Returns the product of the matrix element on the specified axis square. PTP ([axis, out]): Returns the maximum value minus the minimum value for the specified axis direction. put ( Indices, values[, mode]): Replaces the value of the given index (indices) position of the matrix with the given value Ravel ([order]): Returns an array that is a one-dimensional array or a flat array repeat (repeats[, axis] ): Repeats the elements in the matrix, can repeat the matrix element along the specified axis, repeats the number of repetitions reshape (shape[, order]): Change the size of the matrix, such as: reshape ([2,3]) resize (new_shape[, Refcheck]): Change the size of the data round ([decimals, out]): Returns the matrix after the specified precision, the specified number of digits is rounded, and if 1, a decimal is reserved searchsorted (v[, side, Sorter]): Search for the index position of V in the matrix sort ([axis, kind, order]): Sort the Matrix or sort by axis squeeze ([axis]): Remove the axis std ([axis, Dtype, out, ddof] with length 1): Returns the standard deviation of the element along the direction of the specified axis. sum ([axis, Dtype, out]): Returns the sum of its elements in the direction of the specified axis swapaxes (axis1, AXIS2): Swaps data on two axes. Take (indices[, axis, Out, Mode]): Extracts the data at the specified index and returns it as a one-dimensional array or matrix (mainly depending on axis) tofile (fid[, Sep, format]): Writes the data in the matrix to the file ToList () as a binary: Convert matrix to List form ToString ([order]): Converts a matrix into a python string. Trace ([Offset, axis1, Axis2, Dtype, out]): Returns the sum of the diagonal elements transpose (*axes):Returns the Matrix's transpose matrix without changing the original matrix Var ([axis, Dtype, out, Ddof]): Returns the matrix element's variance view ([Dtype, type]) in the direction of the specified axis: produces the same data, but the type is a matrix of the specified new type.

ü  All method

>>> a = Np.asmatrix (' 0 2 7; 3 4 8; 5 0 9 ') >>> A.all () false>>> a.all (axis=0) matrix ([[False, Fals e,  True], Dtype=bool) >>> a.all (Axis=1) matrix ([[[false],[True],[false]], Dtype=bool) ü  Astype method >>> A.astype (float) matrix ([[12.,   3.,   5.],[32.,  23.,   9.],[10.,- 14.,  78.]) ü  Argsort methods >>> A=np.matrix (' 3 5; 9; 10-14) >>> a.argsort () matrix ([[1, 2, 0],[2, 1, 0] , [1, 0, 2]] ü  clip method >>> Amatrix ([[12,   3,   5],[32,  23,   9],[10,- 14, ]) >>> a.clip (12,32) matrix ([[[12, 12, 12],[32, 23, 12],[12, 12, 32]] ü  Cumprod method >>> A . Cumprod (Axis=1) matrix ([[    12,     36,    180],[     32,    736,   6624],[    10,  -140,-10920]]) ü  Cumsum methods >>> a.cumsum (Axis=1) MatriX ([[[12, 15, 20],[32, 55, 64],[10,-4, 74]]) ü  ToList method >>> b.tolist () [[12, 3, 5], [32, 23, 9], [10,-14, 78]] ü  ToFile Method >>> b.tofile (' d:\\b.txt ') ü  compress () method >>> from NumPy import *>>> a = Array ([ten, B, Max]) >>> condition = (A > +) & (a < +) >>> Conditionarray ([False, True, Tru E, False], Dtype=bool) >>> a.compress (condition) array ([+]) >>> a[condition]                                        # Same Effectarray ([+]) >>> compress (a >=, a)                                 # This form aso Existsarray ([+]) >>> B = Array ([[10,20,30],[40,50,]) >>> b.compress (B.ravel () >=) array ([X, Max, Max]) >>> × = Array ([3,1,2]) >>> y = arr AY ([101]) >>> b.compress (x >= 2, Axis=1)                         # illustrates the use of the axis Keywordarray ([[[60], 30],[40, X]]) >>> b.compress (y >=, axis=0) Array ([[[40, 50,]])

Matrix processing in NumPy

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.