4

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?

5
  • Unlike Java, you can overload operators in Python. Not familiar about numpy in particular, but maybe you can extend the data type you're working with and inject operator overloading methods to it. Commented Jul 3, 2014 at 18:27
  • Unrelated to the syntax issues, it's probably worth computing X*theta - y separately and using the value twice instead of recomputing it. Commented Jul 3, 2014 at 18:39
  • @user2357112 yes. good point. Commented Jul 3, 2014 at 18:40
  • FWIW: In contrast to matlab, numpy is just a package in a general use programming language. So it's no surprise that its syntax is not as "mathematical" as matlab... Commented Jul 3, 2014 at 19:31
  • @Santa: numpy already does overload operators to allow elementwise operations, but it doesn't provide an operator for matrix multiplication, basically because there aren't enough operators to go around. (* has to mean either elementwise multiplication or matrix multiplication, and for numpy arrays they decided to make it mean the former.) Commented Jul 3, 2014 at 20:10

3 Answers 3

5

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.