Posted on 25 September 2010.
Colleagues have asked me why I changed from Matlab to Python, and what makes Python so great. For example, a friend recently asked the following:
I noticed that the use of python to do signal processing tasks is slowly increasing (saw it mentioned in some papers). I am curious to know what are the advantages/disadvantages of python vis-a-vis C or Matlab. What do you think are the main pros and cons that have led you to use python?
Because this precise question — why Python over Matlab? — has become so popular, I thought I would offer my reasons for switching.
That may seem a frivolous reason to choose one language over another. But it is not so frivolous as it sounds: when you program, you spend more time reading code than writing it. You push blobs of source code around the way a sculptor does blobs of clay. So a language that makes source code ugly is maddening to an exacting programmer, as clay full of lumps would be to a sculptor.
Disadvantages? Python is probably easier to learn from scratch than C++, but it still takes time. If you are already comfortable with Matlab, and you have Matlab, then you should keep using Matlab. But if you want a language that is free, flexible, popular, and may make you more marketable, then give Python a try. Believe me, I like Matlab. I just like Python better.
1. Comment by Malcolm Slaney
I agree with you, except for two things.
1) the graphics for Numpy suck. It’s still much easier to make a useful graph in Matlab.
2) The indentation in Python makes it hard to try out little pieces of code as I develop something new.
3) Oh, yes, the debugging environment in Python leaves a lot to be desired.
Matlab is far from perfect (it’s just Fortran after all :-) ). So I use both.
My latest system uses Python for lots of stuff (mostly because of the text and web tools). But I use Matlab for debugging. My Python application has a web API that can output HTML, JSON or Matlab code (for the results). urlopen() works really well to talk to a web application and all is well!
January 21, 2011 at 3:49 pm
2. Comment by Steve
Thank you for the comment. I loved the Matlab profiler and relied on it heavily. Python’s profiler and debugger are usable, but as you pointed out, it’s just a bit easier with Matlab’s interface.
January 21, 2011 at 8:10 pm
3. Comment by steveha
The best debugging environment I have seen for Python is the Wing IDE. It lets you single-step in your Python code.
February 15, 2011 at 2:43 pm
4. Comment by Colin
Re: Malcom’s comments, for graphing, I’ve found matplotlib able to do everything that Matlab can do and then some – and often the graphics look significantly better. With the obvious glaring exception of 3d graphs! In terms of indentation making things hard to test, I’m guessing you’re referring to the fact that if you have a bunch of source code (example code or code you’ve written) and you want to try to run a small part of it, then you can’t directly copy-paste it into the python prompt. A very simple fix for this (albeit a tiny bit kludgy) is just to enter if 1: into the prompt prior to pasting. This allows you to start at any arbitrary indentation amount in your code, and since python doesn’t care if the indentation amount is uniform, you can copy/paste directly. Hope that’s what you were referring to…
May 17, 2011 at 7:02 pm
5. Comment by Siyi Deng
The syntactic aesthetics is something totally subjective, and I generally like matlab’s syntax better. Python’s indentation is awkward in some cases.
a simple example:
a(end); % Matlab syntax, straitforward, learners can easily guess what this is.
a[-1] # Python syntax. Not very new user friendly imho.
July 7, 2011 at 8:50 pm
6. Comment by Yoda
Regarding 3D graphics in Python: an excellent for this is MayaVi2. It includes a package called ‘mlab’ which provides a simple way to produce 3D plots — using commands that are often identical to matlab.
July 22, 2011 at 7:48 am
7. Comment by Matt
I’ve used Matlab for years but have recently been primarily using Python/Numpy and Cython with the WingIde. The two primary drivers have been cost and speed. Python numpy code can be coaxed to run much faster than Matlab via profiling and compiling to C via Cython. Moreover I now have an extensive CUDA library hooked into Python so it generally runs faster than vanilla C.
However Numpy is not quite as productive as Matlab for scientific computing, though it could easily have been as productive as Matlab if not for a few key design mistakes in numpy. The problem boils down to one major issue, the fact that array slicing reduces/changes the dimension of the array. Thus if M is a 3 x 2 matrix, M[0,:] is no longer a matrix but a 2-D vector. It should instead be a 1 x 2 matrix and assignment should have sensible broadcast rules to handle singleton dimensions.
This will generate endless bugs, makes the interface into the well written matplotlib library persnickety, riddle your code with reshape calls and generate many other annoyances. In theory the matrix subclass of the array class is better behaved, but most functions in the library don’t use that class so you are forced to constantly convert.
There are also some other gotcha’s like the fact that numpy hates Fortran column ordering even though that format is assumed for most external Fortran and even C math libraries. Also numpy arrays don’t overload any operators to handle standard matrix multiplies. There is no decent \ or inv() function without jumping with a bunch of syntactic hooks etc.
August 22, 2011 at 8:35 pm
8. Comment by Steve
1. Yeah, the change in dimension via array slicing is sometimes annoying. Here’s a common example: reading audio signals. A stereo signal has shape (N, 2), but a mono signal has shape (N,), not (N, 1). So then if you try to check X.shape, it gives an IndexError.
2. Operator overloading is an obvious advantage in Matlab, but I’ve gotten so used to scipy.linalg that it’s not a big deal anymore.
3. You can adjust the ordering through scipy.array(…, order=’F'). You’re right — it does make a huge difference in execution time.
August 22, 2011 at 9:19 pm
9. Comment by joon
“a(end); % Matlab syntax, straitforward, learners can easily guess what this is.”
Actually, one does not know if a is a variable or a function. Using () for both function argument and indexing is very confusing. Moreover, you can call a function without any () in matlab, which makes it more confusing.
January 18, 2012 at 9:46 am
10. Comment by ZyX
> a[-1] # Python syntax. Not very new user friendly imho.
But programmers friendly: there are lots of modern languages that use negative indexes for accessing list items from their end. What is not common (at least, for me, with background of Perl, VimL and zsh that all have different behavior here) is that for a=[0, 1, 2] "a[0:1]" returns just "", and same is range: "range(0, 3)" is "[0, 1, 2]".
> 2. Operator overloading is an obvious advantage in Matlab, but I've gotten so used to scipy.linalg that it's not a big deal anymore.
Operator overloading is present in python, though names of functions that are to be defined in order to overload them are not so obvious as in C++. Even less is overloading in python modules written in C.
May 25, 2012 at 12:46 pm