原文地址:http://www.scipy.org/NumPy_for_Matlab_Users/
Short answer
Use arrays.
- They are the standard vector/matrix/tensor type of numpy. Many numpy function return arrays, not matrices.
- There is a clear distinction between element-wise operations and linear algebra operations.
- You can have standard vectors or row/column vectors if you like.
The only disadvantage of using the array type is that you will have to use dot instead of * to multiply (reduce) two tensors (scalar product, matrix vector multiplication etc.).
Long answer
Numpy contains both an array class and a matrix class. The array class is intended to be a general-purpose n-dimensional array for many kinds of numerical computing, while matrixis intended to facilitate linear algebra computations specifically. In practice there are only a handful of key differences between the two.
-
Operator *, dot(), and multiply():
-
For array, '*' means element-wise multiplication, and the dot() function is used for matrix multiplication.
-
For matrix, '*' means matrix multiplication, and the multiply() function is used for element-wise multiplication.
-
- Handling of vectors (rank-1 arrays)
-
For array, the vector shapes 1xN, Nx1, and N are all different things. Operations like A[:,1] return a rank-1 array of shape N, not a rank-2 of shape Nx1. Transpose on a rank-1 array does nothing.
-
For matrix, rank-1 arrays are always upconverted to 1xN or Nx1 matrices (row or column vectors). A[:,1] returns a rank-2 matrix of shape Nx1.
-
-
Handling of higher-rank arrays (rank > 2)
-
array objects can have rank > 2.
-
matrix objects always have exactly rank 2.
-
- Convenience attributes
-
array has a .T attribute, which returns the transpose of the data.
-
matrix also has .H, .I, and .A attributes, which return the conjugate transpose, inverse, and asarray() of the matrix, respectively.
-
- Convenience constructor
-
The array constructor takes (nested) Python sequences as initializers. As in, array([[1,2,3],[4,5,6]]).
-
The matrix constructor additionally takes a convenient string initializer. As in matrix("[1 2 3; 4 5 6]").
-
There are pros and cons to using both:
-
array
-
You can treat rank-1 arrays as either row or column vectors. dot(A,v) treats v as a column vector, while dot(v,A) treats v as a row vector. This can save you having to type a lot of transposes.
-
Having to use the dot() function for matrix-multiply is messy -- dot(dot(A,B),C) vs. A*B*C.
-
Element-wise multiplication is easy: A*B.
-
array is the "default" NumPy type, so it gets the most testing, and is the type most likely to be returned by 3rd party code that uses NumPy.
-
Is quite at home handling data of any rank.
-
Closer in semantics to tensor algebra, if you are familiar with that.
-
All operations (*, /, +, ** etc.) are elementwise
-
-
matrix
-
Behavior is more like that of MATLAB® matrices.
-
Maximum of rank-2. To hold rank-3 data you need array or perhaps a Python list of matrix.
-
Minimum of rank-2. You cannot have vectors. They must be cast as single-column or single-row matrices.
-
Since array is the default in NumPy, some functions may return an array even if you give them a matrix as an argument. This shouldn't happen with NumPy functions (if it does it's a bug), but 3rd party code based on NumPy may not honor type preservation like NumPy does.
-
A*B is matrix multiplication, so more convenient for linear algebra.
-
Element-wise multiplication requires calling a function, multipy(A,B).
-
The use of operator overloading is a bit illogical: * does not work elementwise but / does.
-
The array is thus much more advisable to use, but in the end, you don't really have to choose one or the other. You can mix-and-match. You can use array for the bulk of your code, and switch over to matrix in the sections where you have nitty-gritty linear algebra with lots of matrix-matrix multiplications.