Norm of a arrays of vectors in python
Asked Answered
C

4

7

I have this array

   A = array([[-0.49740509, -0.48618909, -0.49145315],
   [-0.48959259, -0.48618909, -0.49145315],
   [-0.49740509, -0.47837659, -0.49145315],
   ..., 
   [ 0.03079315, -0.01194593, -0.06872366],
   [ 0.03054901, -0.01170179, -0.06872366],
   [ 0.03079315, -0.01170179, -0.06872366]])

which is a collection of 3D vector. I was wondering if I could use a vectorial operation to get an array with the norm of each of my vector.

I tried with norm(A) but it didn't work.

Claud answered 20/5, 2012 at 14:53 Comment(3)
yes, is it relevant to what I've asked?Claud
I think it is yes. numpy.array is different from the array class in Python. I assume that you want answers related to numpy.array?Capillaceous
FYI: In the next release of numpy (1.8.0), you will be able to use the axis argument: norm(A, axis=1).Imbalance
H
9

Doing it manually might be fastest (although there's always some neat trick someone posts I didn't think of):

In [75]: from numpy import random, array

In [76]: from numpy.linalg import norm

In [77]: 

In [77]: A = random.rand(1000,3)

In [78]: timeit normedA_0 = array([norm(v) for v in A])
100 loops, best of 3: 16.5 ms per loop

In [79]: timeit normedA_1 = array(map(norm, A))
100 loops, best of 3: 16.9 ms per loop

In [80]: timeit normedA_2 = map(norm, A)
100 loops, best of 3: 16.7 ms per loop

In [81]: timeit normedA_4 = (A*A).sum(axis=1)**0.5
10000 loops, best of 3: 46.2 us per loop

This assumes everything's real. Could multiply by the conjugate instead if that's not true.

Update: Eric's suggestion of using math.sqrt won't work -- it doesn't handle numpy arrays -- but the idea of using sqrt instead of **0.5 is a good one, so let's test it.

In [114]: timeit normedA_4 = (A*A).sum(axis=1)**0.5
10000 loops, best of 3: 46.2 us per loop

In [115]: from numpy import sqrt

In [116]: timeit normedA_4 = sqrt((A*A).sum(axis=1))
10000 loops, best of 3: 45.8 us per loop

I tried it a few times, and this was the largest difference I saw.

Heuristic answered 20/5, 2012 at 15:9 Comment(1)
**0.5 will always be slower than math.sqrtPelvic
M
2

just had the same prob, maybe late to answer but this should help others. You can use the axis argument in the norm function

norm(A, axis=1)
Millerite answered 13/5, 2020 at 9:35 Comment(0)
S
0

How about this method? Also you might want to add a [numpy] tag to the post.

Sewellel answered 20/5, 2012 at 14:55 Comment(3)
Initially I thought you couldn't find the linalg.norm method as you said norm(A) "didn't work", but after reading Eric's answer I guess it's more likely that you did try linalg.norm but it didn't give you what you wanted.Sewellel
@JoelCornett: As a result of the OP being vague with "it didn't work". He might have explained what he tried and how it didn't work. Like I explained in my comment above.Sewellel
You're right. This is more of a case of OP being obscure. Redacted.Heptane
P
0

Having never used numpy, I'm going to guess:

normedA = array(norm(v) for v in A)
Pelvic answered 20/5, 2012 at 14:57 Comment(5)
map(norm, A) might be faster in this situation.Heptane
@Joel: Yeah, but it doesn't return a numpy array, so you'd at least have to wrap it in np.array()Drawing
@Matteo What about if I use it as a generator?Pelvic
@Matteo: what size is your dataset and how slow is it?Heptane
Unfortunately this won't work as written: it will give something like array(<generator object <genexpr> at 0x102d88f50>, dtype=object). You'd need to use fromiter, I think.Heuristic

© 2022 - 2024 — McMap. All rights reserved.