Python numpy easier syntax?

Question:

I am new to numpy, and I’m already a little sick of its syntax.

Something which could be written like this in Octave/matlab

1/(2*m) * (X * theta - y)' * (X*theta -y)

Becomes this in numpy

np.true_divide(((X.dot(theta)-y).transpose()).dot((X.dot(theta)-y)),2*m)

This is much harder for me to write and debug. Is there any better way to write matrix operations like above so as to make life easier?

Asked By: yayu

||

Answers:

There is a better way, but you will have to consult the numpy documentation to find it.

This page lists a bunch of equivalencies between matlab and numpy with simpler syntax. For example, a.transpose() can be written as a.T.

You can also look at the individual documentation for these functions, such as the one for true_divide which explains that the Python 3 / method works to do the same.

Answered By: merlin2011

You can make some simplifications. By using from __future__ import division at the beginning of your program, all division will automatically be “true” division, so you won’t need to use true_divide. (In Python 3 you don’t even need to do this, since true division is automatically the default.) Also, you can use .T instead of .transpose(). Your code then becomes

1/(2*m) * ((X.dot(theta) - y).T).dot((X.dot(theta) - y))

which is a bit better.

In Python 3.5, a new matrix multiplication operator @ is being added for basically this exact reason. This is not out yet, but when it is (and when numpy is updated to make use of it), your code will become very similar to the Octave version:

1/(2*m) * (X@theta - y).T @ (X@theta - y)
Answered By: BrenBarn

You could try using np.matrix instead of np.ndarray for 2-dimensional arrays. It overloads the * operator so that it means matrix multiplication, so you can do away with all the .dots. Here are the docs.

Answered By: Praveen
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.