Preface
In the implementation of the relevant clustering algorithm, in Python language implementation, will often appear in the array and matrix confusion, here to do a summary.
Array arrays
The most basic (default) type of NumPy is an array, and his related operations are used to manipulate the elements as a numerical calculation (with the action of the element (with +,-,,/,* , etc.). Multiplication Examples:
from numpy import * >>> a=array([1,2]) >>> a array([12]) >>> b=array([2,3]) >>> b array([23]) >>> c=a*b >>> c array([26]) >>> dot(a,b) 8
Multiplication of two arrays * refers to the multiplication of corresponding elements, and the dot of two arrays indicates the multiplication of matrices.
- If a is an array, then a. T indicates transpose.
Convert array to matrix with Asmatrix ()
Most numpy functions return an array type, not a matrix type.
Matrix matrices
The special type in NumPy, which appears as a subclass of array, inherits all the attributes of the array and has its own special place, which is specifically designed to handle linear algebra operations (* denotes multiplication of matrices, but for the two matrix, the division of the corresponding element is represented. )。 Multiplication examples include:
“
' >>> M=mat ([2,3]) >>> m matrix ([[2, 3]]) >>> n= ([1,2]) >>> N [1,2] >>> p=m*n Traceback (most recent): File"<stdin>", line1,inch<Module> File"C:\Python27\lib\site-packages\numpy\matrixlib\defmatrix.py", line341, I n __mul__returnN.dot (self, Asmatrix (other)) Valueerror:objects is notAligned >>> dot (m,n) matrix ([[8]]) >>> Multiply (m,n) matrix ([[2,6]])
** two matrix multiplication * Error reason is that the column of M is not equal to N row, That is, misalignment (aligned), if aligned, is the multiplication of the corresponding element, returns a matrix, the two matrix of the * representation is the multiplication of two matrices. The two matrix dot represents the multiplication of matrices. The multiply of the two matrix represents the multiplication of corresponding elements. * * -in matrix. H. A. I means conjugate, transpose, inverse matrix. -Convert matrix to array with Asarray ()-Asanyarray () in accordance with the type of your input. ## array and a hard-to-understand point of the matrix # # here will be concerned with the concept of rank, in linear algebra (math) rank, But it must be made clear that rank is not the concept of rank in NumPy, it is the concept of the dimension, and this understanding needs to look at this article: for multidimensional arrays data structure interpretation: [ Multidimensional arrays data structure understanding ][1 ] here for the time being the rank, although this understanding is wrong, but can be said to pass some things. (in the actual array and matrix, the English language about rank is used to understand the rank of linear algebra, but the English will appear dimensions equals how much, requires the matrix of the dimesions must be 2, here actually refers to the rank, Dimensions is the true form of understanding in NumPy. Array requires rank 1 (n*1,1* n, etc.) or greater than 2matrix requires rank must be 2 (rank must be 2)-The following is an understanding of Ndim and size:
>>> A array ([1, 2]) >>> b Array ([[[1, 2], [2, 3]]) >>> c MA Trix ([[1, 2, 3, 4], [2, 3, 4, 5]]) >>> d Array ([[1, 2, 3, 4], [2, 3, 4, 5]]) >&G t;> e Matrix ([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]) >>> a.size 2 >&G t;> A.ndim 1 >>> b.size 4 >>> B.ndim 2 >>> c.size 8 >>> C.N Dim 2 >>> D.size 8 >>> D.ndim 2 >>> E.ndim 2 >>>e.size 12
**这里ndim就是求的是rank,所以会发现matrix的都是2,但是array的就会存在差异,需要计算等。size返回的是元素的个数** - 关于dim, shape, rank, dimension and axis in numpy的细节的问题理解:[stackoverflow地址][2]## 补充 ##如何让
M = Matrix ([1, 2, 3, [4]])
How to change to
Array ([1, 2, 3, 4])
比较优雅的办法:
X=matrix (Arange) reshape ((3,4))
X
Matrix ([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]])
X.geta1 ()
Array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
"'
Summarize
In use, or with array, the only drawback of array is to use dot instead of * when representing the multiplication of matrices.
For the rest of the array and matrix operation functions in NumPy, see also wiki address: http://wiki.scipy.org/NumPy_for_Matlab_Users
Matrix and array in the NumPy