Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python numpy easier syntax?

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?

like image 619
yayu Avatar asked Mar 19 '23 03:03

yayu


2 Answers

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)
like image 147
BrenBarn Avatar answered Mar 27 '23 19:03

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.

like image 27
Praveen Avatar answered Mar 27 '23 21:03

Praveen