3 ms·
Just to illustrate: Octave: m = [0 1 0; 0 0 1; 1 0 0] v = [2 3 1]' m\v ans = 1 2 3 Python: import numpy a
by sampo 10y ago
Just to illustrate:
Octave:
m = [0 1 0; 0 0 1; 1 0 0]
v = [2 3 1]'
m\v
ans =
1
2
3
Python:
import numpy as np
m = np.array([[0,1,0],[0,0,1],[1,0,0]])
v = np.transpose(np.array([2,3,1]))
# wtf? oh, I guess then
v = np.transpose(np.array([[2,3,1]]))
np.linalg.solve(m,v)
array([[ 1.],
[ 2.],
[ 3.]])
If you just want to play around, Python has more clutter and gotchas.
Of course, when you start to write real software, then the tools that Python provides for organizing your code start to weight more and the clumsiness of np.linalg.solve(m,v) over m\v starts to weight less.
But I can see how a 50-line testing script in Octave organically grows to a 2000-line application, and then it's already a lot of work to rewrite in Python.
Edit: a reply below makes a good point simplifying the Python code a little bit.
- tombone12 10y agoWhy not just: v = np.array([2,3,1]).T is there something wrong with numpys convenience method for transpose but not matlabs postfix unary convenience operator?
- sampo 10y agoYou are right, T would be nicer than np.tranpose. But the first google hit on 'numpy transpose' [1] doesn't give that as as example. Also you too made the same mistake: np.array([2,3,1]).T doesn't do anything, you need np.array([[2,3,1]]).T [1] https://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html https://docs.scipy.org/doc/numpy/reference/generated/numpy.t...
- brudgers 10y agoI think that's kindasorta of reason for Ng's preference: students will focus on the subject of machine learning and not spend time on the ins and outs of Python and Numpy (or these days TensorFlow). It's the same sort of pedagogical approach that keeps SML as an active teaching language. When someone googles Matlab/Octave there won't be the same mixture of contexts as there is for Python. With Python there's "2 or 3?" before even starting. Than Cython or Jython or PyPy? That's nothing against Python except that in one particular time limited context, the questions are a distraction {without even considering the complaints of students when the choice of Python implementation is contrary to their opinion}.
- akavel 10y agoHow about Julia and R?
- em500 10y agoR: m = matrix( c(0,0,1,1,0,0,0,1,0), nrow=3 ) v = c(2,3,1) solve(m,v) [1] 1 2 3 Julia: for this code snippet the syntax is identical to Matlab: m = [0 1 0; 0 0 1; 1 0 0] v = [2 3 1]' m\v 3×1 Array{Float64,2}: 1.0 2.0 3.0
- messe 10y agoWhat's wrong with: >>> import numpy as np >>> m = np.array([[0,1,0],[0,0,1],[1,0,0]]) >>> v = np.array([2,3,1]) >>> np.linalg.solve(m,v) array([ 1., 2., 3.]) No need to transpose, as numpy handles row and column vectors identically. Using the above variables `m` and `v`: >>> v @ m # treated as row vector array([1, 2, 3]) >>> m @ v # treated as column vector array([3, 1, 2]) Where `@`, for those who don't know, is the matrix multiplication operator (`np.dot`) introduced in recent versions of python 3.
- noobermin 10y agoSounds like a case of familiarity being confused for ease of use.
- TheSpiceIsLife 10y agoWhat's wrong with .... introduced in recent versions of python 3. Exactly.
- foota 10y agoPython3 is ancient, can we stop pretending otherwise?
- TAForObvReasons 10y agoPEP 465 introduced the infix operator @: https://www.python.org/dev/peps/pep-0465/ https://www.python.org/dev/peps/pep-0465/ Python 3.5 was the first version of python3 to implement the PEP: https://docs.python.org/3.5/whatsnew/3.5.html https://docs.python.org/3.5/whatsnew/3.5.html If I am reading the release notes correctly, Python 3.5.0 was released last year (2015-09-13): https://www.python.org/downloads/release/python-350/ https://www.python.org/downloads/release/python-350/ It's fair to say that this is a relatively new development.
- foota 10y agoIt's true that infix multiply is new, but infix multiply if I'm not mistaken is not a breaking change.
- hasch 10y agohere is sagemath m = matrix([[0, 1, 0], [0, 0, 1], [1, 0, 0]]) v = vector([2, 3, 1]) m \ v (1, 2, 3) and then sol = m \ v m * sol (2, 3, 1)