Hi,
I have a 2D matrix comprising a sequence of vectors, and I want to compute the norm of each vector. np.linalg.norm seems to be the best bet, but it does not support axis. Wouldn't this be a nice feature?
Greetings, Hans
On Thursday 20 November 2008 11:11:14 Hans Meine wrote:
I have a 2D matrix comprising a sequence of vectors, and I want to compute the norm of each vector. np.linalg.norm seems to be the best bet, but it does not support axis. Wouldn't this be a nice feature?
Here's a basic implementation. docstring + tests not updated yet, also I wonder whether axis should be the first argument, but that could create compatibility problems.
Ciao, Hans
On Thursday 20 November 2008 11:54:52 Hans Meine wrote:
On Thursday 20 November 2008 11:11:14 Hans Meine wrote:
I have a 2D matrix comprising a sequence of vectors, and I want to compute the norm of each vector. np.linalg.norm seems to be the best bet, but it does not support axis. Wouldn't this be a nice feature?
Here's a basic implementation. docstring + tests not updated yet, also I wonder whether axis should be the first argument, but that could create compatibility problems.
AFAICS, I never received an answer, but IMHO this should be integrated into NumPy. Any objections?
Greetings, Hans
2009/2/9 Hans Meine meine@informatik.uni-hamburg.de:
Here's a basic implementation. docstring + tests not updated yet, also I wonder whether axis should be the first argument, but that could create compatibility problems.
AFAICS, I never received an answer, but IMHO this should be integrated into NumPy. Any objections?
I often work with vectors inside an array, so I would find it useful (even if it is just a two-line wrapper). I'd also add an "out" argument, so that the signature is similar to that of max, min, etc.
norm(a, ord=None, axis=None, out=None)
Cheers Stéfan
On 11/20/2008 5:11 AM Hans Meine apparently wrote:
I have a 2D matrix comprising a sequence of vectors, and I want to compute the norm of each vector. np.linalg.norm seems to be the best bet, but it does not support axis. Wouldn't this be a nice feature?
Of possible use until then: http://docs.scipy.org/doc/numpy/reference/generated/numpy.apply_along_axis.h... Alan Isaac
On Donnerstag 20 November 2008, Alan G Isaac wrote:
On 11/20/2008 5:11 AM Hans Meine apparently wrote:
I have a 2D matrix comprising a sequence of vectors, and I want to compute the norm of each vector. np.linalg.norm seems to be the best bet, but it does not support axis. Wouldn't this be a nice feature?
Of possible use until then: http://docs.scipy.org/doc/numpy/reference/generated/numpy.apply_along_axis.h...
Thanks for the hint, yes as you see I have already patched norm() in the meantime.
BTW: Wow, this is an exceptionally nice doc page, sphinx + scipy's doc system really rocks! :-)
Ciao, / / .o. /--/ ..o / / ANS ooo
Am 20.11.2008 um 11:11 schrieb Hans Meine:
Hi,
I have a 2D matrix comprising a sequence of vectors, and I want to compute the norm of each vector. np.linalg.norm seems to be the best bet, but it does not support axis. Wouldn't this be a nice feature?
Hi, i usually do something like this:
a = random.rand(3000) a.resize((1000,3)) vec_norms = sqrt(sum(a**2,axis=1))
It is much faster than apply_along_axis:
%timeit apply_along_axis(linalg.norm,1,a) 10 loops, best of 3: 45.3 ms per loop
%timeit sqrt(sum(a**2,axis=1)) 10000 loops, best of 3: 108 µs per loop
The results are the same:
sum(apply_along_axis(linalg.norm,1,a)- sqrt(sum(a**2,axis=1))) 0.0
Regards, Markus
On Tuesday 10 February 2009 11:11:38 Markus Rosenstihl wrote:
i usually do something like this:
a = random.rand(3000) a.resize((1000,3)) vec_norms = sqrt(sum(a**2,axis=1))
If you look at the patch I posted (OK, that was some weeks ago, so I'll attach it again for your convenience), that's (more or less) exactly what I proposed.
Have a nice day, Hans
Hi Hans
2009/2/10 Hans Meine meine@informatik.uni-hamburg.de:
If you look at the patch I posted (OK, that was some weeks ago, so I'll attach it again for your convenience), that's (more or less) exactly what I proposed.
Would you mind adding some tests to the patch?
Cheers Stéfan