Python matrix division like matlab
The symbol Show
If The NumPy package for Python contains a routine
To use Note: while NumPy doesn't make any distinction between a 1-by-N or N-by-1 array, MATLAB certainly does, and will yell at you if you don't use the proper one. If your first foray into Machine Learning was with Andrew Ng’s popular Coursera course (which is where I started back in 2012!), then you learned the fundamentals of Machine Learning using example code in “Octave” (the open-source version of Matlab). Octave is great for expressing linear algebra operations cleanly, and (as I hear it) for being easier for non-programmers to get going with. It’s also likely that
you have since switched from Octave to Python. Coding in Python obviously means learning a whole new programming language, with many important differences, but those aren’t the subject of this post. Instead, I wanted to highlight some false assumptions that you may have brought with you from Matlab about how vector and matrix operations should work. Side Note: The NumPy documentation has a very nice “quick reference” type guide on migrating from Matlab to NumPy here. Once you have the basics of Python down, you’ll find that, in the machine learning field, we use NumPy Based on these similarities, you’ll be tempted to think of the In fact, did you know that NumPy actually has a separate class named Here are the two big differences.
Don’t use numpy.matrixBefore we explore these further, you should know that using However, don’t actually do this! The community (and libraries) don’t use In the rest of the post we’ll do just that. Issue #1: ndarray operations are element-wiseI think there’s a good reason that numpy.ndarray uses the term “array”. “Array” is a computer science term–in Python we call these “lists”, but in more formal languages like C or Java we have “arrays”. If you’re teaching a software engineer the basics of machine learning, a good way to explain what a vector is, is that’s it’s “just an array of floating point values”. So an array is a computer science concept, and not a linear algebra one. If you’re NOT working in the context of Linear Algebra or Machine Learning, then interpreting “a * b” as an element-wise multiplication seems perflectly reasonable to me. The linear algebra interpretation is really the more bizarre one. The solution is simple–stay away from the
Outputs:
Issue #2: ndarray treats vectors as 1-dimensionalThis difference has probably caused me the most grief. In Matlab (and in numpy.matrix), a vector is a 2-dimensional object–it’s either a column vector (e.g., [5 x 1]) or a row vector (e.g., [1 x 5]). In a NumPy Having only one dimension means that the vector has a length, but not an orientation (row vector vs. column vector). Check out the simple example below. The array shape is Note: The syntax
Why would vector orientation matter?The distinction between a row vector and a column vector is important in linear algebra, because if you have a matrix \( W \) that’s [5 x 3] and a column vector \( x \) that’s [5 x 1], then there are restrictions on how they can legally be multiplied together:
Note that the output vectors of the last two (the two valid operations) not only have different orientations but also contain different values. When implementing an algorithm in code based on its equation, I find the matrix and vector dimensions very helpful both for interpreting the equation and for ensuring that I am coding it correctly. I typically print out the dimensions of my matrices as a sanity check. With the above definitions of NumPy, however, will simply assume that
Outputs:
The solutionOption A: Reshape the |