Python Scientific Libraries vs. MATLAB

From Chris Riederer’s answer via Quora:

I first learned to program using MATLAB. Now, as a computer science Ph.D. student, I try to use Python for everything.

(For the rest of this post, I’m going to say “SciPy” to collectively refer to NumPy, Matplotlib, and SciPy.)

MATLAB and SciPy both seem to be particularly good at:

  • Rapid prototyping
  • Research code
  • Large numerical or matrix computations
  • Data analysis

The main differentiators are:

  • Use of Python language
  • Placement in Python ecosystem
  • Cost

Use of Python Language
Where SciPy really wins is in the Python language. MATLAB has a low learning curve. However, once you progress beyond smaller functions and calculations, the syntax makes it hard to neatly put together large pieces of code. A big concept here is “composability”. I find it much easier to make small, modular code in Python (with or without using object-oriented design) and compose these modules together than in MATLAB. Whenever I write MATLAB code, everything tends to end up in monolithic functions.

Python has much better string handling, which makes it much easier to print out information about your code when you’re testing something. This may seem minor but can have a big impact on keeping things straight when you’re trying to test something rapidly.

The Python dictionary and the ability to put different data types into lists and tuples makes it much faster to write prototyping or research code.

Python Ecosystem
Python is free, open source, general purpose, and popular. This means that there is a very diverse set of tools written in Python that it will be easy to use with your project. For example, it’s easy to plug your code into a webserver. Many companies provide Python APIs to access their data or code. Even codebases not written in Python are easy to use– someone will soon write a Python wrapper around the codebase. All of this makes it easy for you to benefit from the work of others. There are a number of great articles online about how Python is becoming the go-to language in all aspects of scientific computing.

Additionally, this means that if you want to share or popularize your code, most anyone will be able to run it. For example, a version of Python comes preinstalled on all Macs, and IPython notebooks make it easy to beautifully showcase your code. In contrast, MATLAB requires someone to have an active install and license.

As of writing, Python is much more popular than MATLAB on the TIOBE Index. Note that this isn’t a totally fair comparison, as we’re talking about SciPy vs. MATLAB, not Python vs. MATLAB.

Cost
As others have mentioned… Python is free! MATLAB… is not.

Overall
MATLAB and Scipy are both good for the tasks I mentioned above: rapid prototyping, research code, matrix computations, data analysis. Whichever one you use will not be a bad choice. SciPy and Python may have a little higher learning curve, but I think the benefits of using the language and it’s free and open ecosystem are strong advantages over MATLAB.

Comments