introduce
MATLAB ® And NumPy / SciPy have a lot in common. But there are many differences. NumPy and SciPy were created to perform numerical and scientific calculations in the most natural way in Python, not MATLAB ® clone. This page aims to collect wisdom about differences, mainly to help skilled MATLAB ® Users become skilled NumPy and SciPy users.
Some key differences
MATLAB NumPy
In MATLAB ® In, the basic data type is a multidimensional array of double precision floating-point numbers. Most expressions take such arrays and return such numbers. The operation of 2-D instances of these arrays is designed to be more or less like matrix operation in linear algebra. In NumPy, the basic type is multidimensional array. Operations on these arrays in all dimensions, including 2D, are element by element. People need to use specific functions of Linear Algebra (although operators can be @ used in python 3.5 and later for matrix multiplication).
MATLAB ® Use a 1 (one) based index. Use (1) to find the initial element of the sequence. See note Python uses a 0 (zero) based index. Use [0] to find the initial element of the sequence.
MATLAB ® The scripting language was created to execute linear algebra. The syntax for basic matrix operations is good and clean, but the API for adding GUI s and making complete applications is more or less an afterthought. NumPy is based on Python and has been designed as an excellent general-purpose programming language from the beginning. Although the syntax of some array operations in MATLAB is more compact than NumPy, NumPy (because it is an add-on to Python) can do many things that MATLAB cannot do, such as correctly handling the matrix stack.
In MATLAB ® In, the array has the semantics of passing by value and has an inert copy on write scheme to prevent the actual creation of a copy before it is actually needed. The slice operation copies part of an array. There is pass reference semantics in NumPy array. Slicing is a view of an array.
'array' or 'matrix'? Which should I use?
Historically, NumPy provides a special matrix type np.matrix, which is a subclass of ndarray. It makes binary operations become linear algebraic operations. You might see it in some existing code instead of np.array. So, which one to use?
Brief answer
Use arrays.
They are numpy Standard vector of/matrix/Tensor type. many numpy Function returns an array instead of a matrix. There are obvious differences between element operations and linear algebra operations. You can use standard vectors or rows if you like/Column vector.
Before Python 3.5, the only disadvantage of using array types was that you had to use dot instead of * multiply (reduce) two tensors (scalar product, matrix vector multiplication, etc.). Starting with Python 3.5, you can use the matrix multiplication @ operator.
In view of the above, we intend to eventually discard the matrix.
Long answer
NumPy contains array class and matrix class. Described
The array class is intended to be a general n-dimensional array for many kinds of numerical calculations, while the matrix is intended to specifically promote linear algebraic calculations. In practice, there are only a few key differences between the two.
operator*and@function dot(),as well as multiply(): For arrays,*Represents element by element multiplication, and @ Representation matrix multiplication; They have related functions multiply() and dot() . (stay python 3.5 Before,@ Does not exist and must be used dot() Perform matrix multiplication). For matrices,* Represents matrix multiplication, which must be used for element by element multiplication multiply() Function. Vector processing (one dimensional array) For arrays, vector shape 1 xN,Nx1 and N It's all different things. image A[:, 1] Such an operation returns the shape N A one-dimensional array, not a shape Nx1 A two-dimensional array of. Transpose on a one-dimensional array does nothing. For matrices, one-dimensional arrays are always up converted to 1 xN or Nx1 Matrix (row or column vector). A[:, 1] Return shape is Nx1 Two dimensional matrix. Processing higher dimensional arrays( ndim> 2) The dimension of the array object can be > 2 ; Matrix objects always have two dimensions. Convenience attribute array There is one.T Property that returns the transpose of the data. matrix also.H,.I and.A Property to return conjugate transpose, inversion, and asarray()Matrix. convenience constructors Should array Construct (nested) Python Sequence initialization. For example: array([[1,2,3],[4,5,6]]). Should matrix Construction also requires a convenient string initialization, such as: matrix("[1 2 3; 4 5 6]").
Using both has advantages and disadvantages:
array :) Element multiplication is easy: A*B. :( You must remember that matrix multiplication has its own operators@. :) You can think of a set of dimensions as a row vector or a column vector. A @ v take v As a column vector, and v @ A take v As a row vector. This saves you typing a lot of transposes. :) array Is "default" NumPy Type, so it gets the most tests and is used NumPy The type most likely to be returned by the third-party code of. :) Very good at handling data of any dimension. :) If you are familiar with it, then semantics is closer to tensor algebra. :) All operations(*,/,+,-Etc.) element by element. :( sparse matrix scipy.sparse Does not interact with arrays. matrix :\\ Act more like MATLAB®Matrix. <:( Maximum 2D. To save the 3D data you need, array Or maybe Python list matrix. <:( Minimum two dimensions. You cannot have vectors. They must be converted as a single column or single row matrix. <:( because array yes NumPy The default value in, so array Even if you put them matrix Given as arguments, some functions may also return. This should not happen in NumPy Function (if it is indeed an error), but based on NumPy Your third-party code may not be like NumPy In that way, type preservation is observed. :) A*B It's matrix multiplication, so it looks like what you wrote in Linear Algebra (for Python> = 3.5 Normal array and@Operators have the same convenience). <:( Element multiplication requires calling functions, multiply(A,B). <:( The use of operator overloading is somewhat illogical:* Cannot operate by element, but / Such is the case. Interact with it scipy.sparse A little clean.
Therefore, it is much wiser to use array s. In fact, we intend to eventually abolish matrices.
Rough function correspondence table of MATLAB and NumPy
The following table shows some common MATLAB ® Rough equivalents of expressions. These are not exact equivalents, but should be used as hints to move you in the right direction. For more details, please read the built-in documentation of NumPy functions.
For more information, see: http://www.mark-to-win.com/tutorial/52205.html