Tips and precautions when porting MATLAB programs to Python

There are times when I have to port a MATLAB program to Python about once a year, but I will summarize the tips and precautions I noticed at that time. Will be added at any time. (The following includes NumPy / SciPy official documentation on differences from MATLAB And Numpy for MATLAB users contains many duplicates.)

Transpose

The transpose of the matrix M is M'in MATLAB and M.T in NumPy.

[A B] for arrays A, B

The operation [A B] on arrays A, B in MATLAB is equivalent to numpy.hstack ([A, B]) in NumPy. (A and B must be the same size except in the last dimension.)

size() The size of each dimension of array A is calculated by size (A) in MATLAB, but by numpy.shape (A) in NumPy. Note that numpy.size (A) will result in the number of elements contained in array A.

Array index

Everyone should be careful when porting that the index of an array element starts from 1 in MATLAB and 0 in NumPy, but there is also a difference in the index for the ** dimension ** of the array. You need to be careful. That is, sum (A, 2) in MATLAB sums along the second dimension of array A, which must be numpy.sum (A, axis = 1) in NumPy.

Functions that perform operations along a specific dimension (sum (), mean (), std (), var (), max (), min (), ...)

Sum (A) in MATLAB sums along the first dimension of array A, while NumPy sums all the elements of array A (unless you specify axis). Therefore, ** sum (A) in MATLAB is equivalent to numpy.sum (A, axis = 0) in NumPy **. This behavior is the same for other functions that perform operations along a specific dimension, such as mean (), std (), var (), max (), min ().

Size / shape of array after applying function

In relation to the above, in MATLAB and NumPy about what dimension the resulting array has when performing operations along a specific dimension with sum (), mean (), std (), var (), etc. There are differences and you need to be careful. For example, if you want to calculate "take an average along the second dimension of a three-dimensional array A and subtract it from the original array", you can find it with A --mean (A, 2) in MATLAB, but with NumPy. A --I get an error when I try numpy.mean (A, 1). This is ** the result of mean (A, 2) in MATLAB is a 3D array (the size of the averaged dimension is 1), whereas the result of numpy.mean (A, 1) in NumPy This is because the result is a two-dimensional array (the averaged dimension is erased) **, so subtraction from the three-dimensional array is not possible (generally). In this case, in NumPy, it is necessary to explicitly supplement the erased dimension with numpy.newaxis as A --numpy.mean (A, 1) [:, numpy.newaxis,:].

reshape() If you want to change only the shape without changing the elements of the array, use reshape (). For example, when you want to rearrange the array A as [1, 2, 3, 4] into a 2x2 2D array, reshape (in MATLAB). A, 2, 2) produces [1 3; 2 4], while NumPy produces numpy.reshape (A, (2, 2)) [[1, 2], [3, 4]. ]] Is generated. In other words, ** MATLAB fills elements along the first dimension, but NumPy first fills elements along the last dimension **. If you want NumPy to fill the elements in a MATLAB-like order, you need to give'F'to the keyword argument order in numpy.reshape ().

A (:) for array A

Related to the above, the operation A (:) on a multidimensional array A in MATLAB returns a column vector that rearranges all the elements of A into one dimension, in which ** MATLAB starts from the first element. First, take out the elements and arrange them according to the first dimension **. On the other hand, NumPy's numpy.ravel (A) also rearranges the multidimensional array into a one-dimensional array, but ** NumPy first extracts the elements from the first element along the last dimension and arranges them. ** Therefore, the order of the elements is different from the result of A (:) in MATLAB. If you want NumPy to arrange the elements in a MATLAB-like order, you need to give'F'to the keyword argument order in numpy.ravel ().

zeros(), ones() Zeros (n) in MATLAB produces a two-dimensional array of size n x n and all elements have zero values, while NumPy's numpy.zeros (n) produces a one-dimensional array of size n. So ** zeros (n) in MATLAB is equivalent to numpy.zeros ((n, n)) in NumPy ** (note that you need to tuple the shape of the generated array). ). If there are two or more arguments, both MATLAB and NumPy behave in the same way. That is, both zeros (m, n) in MATLAB and numpy.zeros ((m, n)) in NumPy produce a two-dimensional array of size m x n. This behavior is the same for ones ().

rank() In MATLAB, the rank of the matrix M is calculated by rank (M), but in NumPy, it is calculated by numpy.linalg.matrix_rank (M). Note that numpy.rank (M) results in the number of dimensions of the array M (that is, M.ndim, that is, len (M.shape)).

eig() The eigenvalues of the matrix M are found by eig (M) in MATLAB, but by numpy.linalg.eigvals (M) in NumPy. Note that if you set numpy.eig (M), in addition to the vector that has the eigenvalue as the element, you will also receive the matrix that has the eigenvector as the column at the same time (w, V = numpy.eig (M), the vector w is the eigenvalue, Matrix V is the eigenvector). Even in MATLAB, if you receive two return values from eig (M), you can get the eigenvalue and the eigenvector. In this case, the matrix that has the eigenvector in the column and the diagonal ** matrix that has the eigenvalue in the diagonal component ** Note that it will be received in the form of ([V, D] = eig (M), matrix V is the eigenvector and matrix D is the eigenvalue).

svd() For the singular value decomposition of the matrix M, MATLAB uses svd (M) and NumPy uses numpy.linalg.svd (M). If you make MATLAB receive three return values, you will get matrices U, S, and V, where S is a matrix with singular values as diagonal elements and U and V are matrices with left and right singular vectors in columns, respectively. .. Arrays u, d, v are obtained from NumPy's svd (M), where u is a matrix with left singular vectors in columns, d is a ** one-dimensional array with singular values as elements **, and v is a right singular vector. Is a ** matrix with ** rows. In other words, U in MATLAB and u in NumPy match, but the others do not, and S corresponds to numpy.diag (d) and V corresponds to the transpose of v (Hermitian transpose in the case of a complex matrix). ..

cov() A function that calculates the covariance between variables from a multivariable data matrix, but in MATLAB and NumPy the rows and columns of the data matrix are interpreted in reverse. That is, in MATLAB, each ** column ** corresponds to each variable, but in NumPy, each ** row ** corresponds to each variable. Therefore, MATLAB cov (X) is equivalent to NumPy's numpy.cov (X.T).

linspace(a, b, n) It behaves almost the same in MATLAB and NumPy, with the difference that when n is 1, MATLAB returns b, but NumPy returns a.

Structure

There are several ways to represent a MATLAB structure in Python, but I think it is often convenient to use a NumPy structured array. Note, however, that MATLAB structs can add fields dynamically, while NumPy's structured array must define all fields when declaring.

[・] For the structure

The operation [S.f] for the field f of the structure S in MATLAB corresponds to S ['f'] for the field f of the structured array S of NumPy. However, if S ['f'] is a multidimensional array, then it should be numpy.hstack (S ['f']) (that is, the array is decomposed for the first dimension and in the direction of the last dimension. Rejoin).

Recommended Posts

Tips and precautions when porting MATLAB programs to Python
Precautions when passing def to sorted and groupby functions in Python? ??
Porting and modifying doublet-solver from python2 to python3.
[Python] Precautions when assigning values to multidimensional arrays
Python and numpy tips
Precautions when upgrading TensorFlow (to 1.3)
~ Tips for beginners to Python ③ ~
Python regular expression basics and tips to learn from scratch
Tips for coding short and easy to read in Python
Precautions when inputting from CSV with Python and outputting to json to make it an exe
Call Matlab from Python to optimize
Python 3.6 on Windows ... and to Xamarin.
Precautions when using pit in Python
[Introduction to Python3 Day 1] Programming and Python
Precautions when using codecs and pandas
Tips when Vimmer switches to Pycharm
Tips to know when programming competitively with Python2 (Other language specifications)
How to deal with errors when installing Python and pip with choco
Precautions when creating a Python generator
Python logging and dump to json
Selenium and python to open google
Precautions when using phantomjs from python
Precautions when giving default values to arguments in Python function definitions
Precautions when using six with Python 2.5
[VS Code] ~ Tips when using python ~
Try to write python code to generate go code --Try porting JSON-to-Go and so on
Precautions and error handling when calling .NET DLL from python using pythonnet
From Python to using MeCab (and CaboCha)
[Python] Use and and or when creating variables
Tips to make Python here-documents easier to read
How to install and use pandas_datareader [Python]
Precautions when pickling a function in python
Bind methods to Python classes and instances
Precautions when solving DP problems with Python
Fractal to make and play with Python
[Beginner] Installing Python and running programs (Windows)
Read Python csv and export to txt
python: How to use locals () and globals ()
[Python] How to calculate MAE and RMSE
How to use Python zip and enumerate
Compress python data and write to sqlite
How to use is and == in Python
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 1)
[Python] Precautions when retrieving data by scraping and putting it in the list
python tips
Tips for those who are wondering how to use is and == in Python
python tips
Three things I was addicted to when using Python and MySQL with Docker
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 3)
To avoid seeing hell when installing django-toolbelt on windows, heroku and python3.4 (64bit) ...
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 2)
Things to keep in mind when using Python for those who use MATLAB
Python Tips
Python tips
[Tips] Easy-to-read writing when connecting functions in Python
How to start Python (Flask) when EC2 starts
MessagePack-Try to link Java and Python with RPC
Recommended environment and usage when developing with Python
I want to use MATLAB feval with python
How to generate permutations in Python and C ++
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)