Python numpy Packet---matrix operation __python

Source: Internet
Author: User
Tags mathematical functions
The following is a brief introduction to several differences between Python and Matlab to deal with mathematical problems. The basic type of the 1.MATLAB is the matrix, and the NumPy is a multiple array, and the matrix is considered to be a subclass of array. The 2.MATLAB index starts at 1, and NumPy starts at 0. 1. Establish a matrixA1=np.array ([1,2,3],dtype=int)
#建立一个一维数组, the data type is int. You can also use default without specifying a data type. Almost all array-building functions can specify the data type, that is, the value of the Dtype. A2=np.array ([[[1,2,3],[2,3,4]])
#建立一个二维数组. There is a big difference between the two-dimensional array (matrix) of Matlab. Similarly, there are many built-in special matrices in NumPy: B1=np.zeros ((2,3))
#生成一个2行3列的全0矩阵. Note that the parameter is a tuple: (2,3), so there are two parentheses. The complete form is: Zeros (shape,dtype=). The same structure, there are ones () to establish full 1 matrices. Empty () creates an empty matrix, using random values in memory to populate the matrix. B2=identity (n) #建立n the unit array of *n, which can only be a phalanx. B3=eye (n,m=none,k=0)
#建立一个对角线是1其余值为0的矩阵, specify the position of the diagonal with K. M defaults to none. In addition, several like functions are provided in NumPy, that is, a special array of the same size based on the size of a known array (rows and columns). Such functions have Zeros_like (), Empty_like (), Ones_like (), and their arguments are in this form: Zeros_like (a,dtype=), where A is a known array. C1=np.arange (2,3,0.1)
#起点, end point, step value. Contains the starting point value and does not contain an endpoint value. C2=np.linspace (1,4,10)
#起点, end point, within the interval points. The beginning and end points are included. Similarly, there is a logspace () function d1=np.linalg.companion (a)
#伴随矩阵 D2=np.linalg.triu ()/tril ()
#作用同MATLAB中的同名函数 E1=np.random.rand (3,2)
#产生一个3行2列的随机数组. Under the same space, there are several random functions such as RANDN ()/randint () FLIPLR ()/flipud ()/rot90 ()
#功能类似MATLAB同名函数. Xx=np.roll (x,2)
#roll () is a cyclic shift function. This call represents a 2-bit circular movement to the right. 2. The characteristic information of the arrayAssuming that an n-dimensional array x is already present, you can get some of the properties of X, which are available after you enter x and one. Press the TAB key to view the prompts. The Python object-oriented features are clearly visible here. X.flags #数组的存储情况信息. X.shape
#结果是一个tuple, returns the number of rows in this array, the number of columns 、...... X.ndim #数组的维数, the result is a number of x.size #数组中元素的数量 x.itemsize
#数组中的数据项的所占内存空间大小 x.dtype #数据类型 x.t #如果X是矩阵, which is played by X's transpose Matrix X.trace () #计算X的迹 Np.linalg.det (a) #返回的是矩阵a的行列式 np.linalg. Norm (A,ord=none)
#计算矩阵a的范数 Np.linalg.eig (a)
#矩阵a的特征值和特征向量 Np.linalg.cond (A,p=none)
#矩阵a的条件数 NP.LINALG.INV (a)
#矩阵a的逆矩阵 3. Matrix DecompositionCommon matrix decomposition functions, numpy.linalg have been provided. such as Cholesky ()/qr ()/SVD ()/lu ()/schur (). Some algorithms give a variety of calling forms for the convenience of computation or for different special cases, in order to obtain the best results. 4. Matrix OperationNp.dot (A,B) is used to compute the dot product of an array; VDOT (A,B) specializes in calculating the dot product of vectors, and dot () is different from the processing of complex data types; Innner (a,b) is used to compute the inner product; outer (a,b) calculates the outer product. The mathematical functions that specialize in dealing with matrices are defined in NumPy linalg. For example, NP.LINALG.LOGM (a) calculates the logarithm of a matrix A. Visible, this processing and MATLAB is similar, using a M suffix representation is the operation of the Matrix. In this space can be used COSM ()/sinm ()/SIGNM ()/sqrtm () and so on. There are three kinds of matrix forms for general exp (): EXPM () uses the Pade approximation algorithm, EXPM2 () uses the eigenvalue analysis algorithm, the EXPM3 () uses the Taylor series algorithm. In NumPy, there is also a function to compute the matrix: FUNM (A,func). 5. IndexThe array index form in NumPy is consistent with Python. such as: X=np.arange (a) print x[2]
#单个元素, from the back to the forward index. Note that the subscript starts at 0. Print X[-2]
#从后往前索引. The last element's subscript is -1 print X[2:5]
#多个元素, left turn right, default step value is 1 print x[:-7]
#多个元素, from the back forward, draw the end of the position, using the default step value print X[1:7:2] #指定步长值 x.shape= (2,5)
#x的shape属性被重新赋值, the requirement is that the number of elements is unchanged. 2*5=10 Print x[1,3]
#二维数组索引单个元素, that element in column 4th of line 2nd, print x[0] #第一行所有的元素 y=np.arange. Reshape (5,7)
#reshape () function to change the dimension of an array, print Y[1:5:2,::2]
#选择二维数组中的某些符合条件的元素
Related Article

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.