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