Python matrix operations are constantly collected and collated

Source: Internet
Author: User
Tags mathematical functions

Python matrix operationsTurn from:Http://blog.sina.com.cn/s/blog_5f234d4701012p64.htmlPython uses the NumPy package to complete the quick and easy operation of n-dimensional arrays. To use this package, you need to import numpy. The SCIPY package is based on the NumPy package, which greatly expands the numpy capability. For ease of use, the SCIPY package includes all the numpy content in the outermost namespace, soas soon as you import the scipy, you don't have to import numpy separately! But in order to make clear which ones are implemented in NumPy and which are implemented in scipy, this paper makes a distinction. The following defaults are: Import
The NumPy as NP and the import scipy as SP briefly describe several differences between Python and matlab in dealing with mathematical problems. The basic of the 1.MATLAB is the matrix, and the basic type of numpy is the number of arrays, the matrix is considered to be the subclass of the array. The index of 2.MATLAB starts at 1, while NumPy starts from 0. 1. Create a matrixA1=np.array ([1,2,3],dtype=int)
#建立一个一维数组, the data type is int. You can also use the default without specifying a data type. Almost all array-building functions can specify the data type, which is the value of the Dtype. A2=np.array ([[1,2,3],[2,3,4]])
#建立一个二维数组. There is a big difference between the two-dimensional arrays (matrices) 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, with ones () establishes a full 1 matrix. Empty () creates an empty matrix, using random values in memory to populate the matrix. B2=identity (n) #建立n the unit array of the *N, which can only be a square. B3=eye (n,m=none,k=0)
#建立一个对角线是1其余值为0的矩阵, specify the position of the diagonal with K. M default None. In addition, there are several like functions in numpy that create special arrays of the same size according to the size of a known array (several 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 start value, without the end value. C2=np.linspace (1,4,10)
#起点, end point, number of points within the interval. 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列的随机数组. In the same space, there are multiple random functions such as RANDN ()/randint () FLIPLR ()/flipud ()/rot90 ()
#功能类似MATLAB同名函数. Xx=np.roll (x,2)
#roll () is a cyclic shift function. This call represents the right loop to move 2 bits. 2. Feature Information for arraysAssuming that an n-dimensional array x is already present, you can get some properties of x, which can be entered in X and one after, pressing the TAB key to see the prompt. The object-oriented features of Python are clearly seen here. X.flags #数组的存储情况信息. X.shape
#结果是一个tuple, returns the number of rows and columns of this array 、...... X.ndim #数组的维数, the result is a number x.size #数组中元素的数量X. itemsize
#数组中的数据项的所占内存空间大小X. Dtype #数据类型X. T #如果X是矩阵, played by the transpose matrix of x 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 DecompositionA common matrix decomposition function, NUMPY.LINALG, has been provided. such as Cholesky ()/qr ()/SVD ()/lu ()/schur () and so on. Somein order to facilitate the calculation or for different special cases, the algorithm gives a variety of invocation forms, in order to obtain the best results. 4. Matrix OperationsNp.dot (A, A, b) is used to calculate the dot product of an array; VDOT (A, A, a, b) specifically calculates the dot product of a vector, and dot () is different from the processing of complex data types, and Innner (A, b) is used to calculate the inner product; The mathematical functions that specialize in the matrix are defined in the NumPy linalg of the sub-package. For example, NP.LINALG.LOGM (a) calculates the logarithm of matrix A. As can be seen, this processing is similar to MATLAB, using an M suffix to represent the operation of the Matrix. In this space can be used COSM ()/sinm ()/SIGNM ()/sqrtm () and so on. The conventional exp () corresponds to three matrix forms: EXPM () uses the Pade approximation algorithm, EXPM2 () using the eigenvalue analysis algorithm, EXPM3 () using 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. Example: X=np.arange () print x[2]
#单个元素, from the forward index to the back. Note that the subscript is starting from 0. Print X[-2]
#从后往前索引. The subscript for the last element is -1print X[2:5]
#多个元素, left closed right, default step value is 1print x[:-7]
#多个元素, from the back forward, developed the end position, using the default step value of print X[1:7:2] #指定步长值x. shape= (2,5)
#x的shape属性被重新赋值, the requirement is that the number of elements is constant. 2*5=10print x[1,3]
#二维数组索引单个元素, the element in the 4th column of row 2nd, print x[0] #第一行所有的元素y =np.arange (5,7). Reshape
#reshape () function to change the dimension of an array print y[1:5:2,::2]
#选择二维数组中的某些符合条件的元素

Python matrix operations are constantly collected and collated

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.