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?
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)
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 .dot
s. Here are the docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With