Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Norm of a arrays of vectors in python

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.

like image 871
Brian Avatar asked May 20 '12 14:05

Brian


People also ask

How do you find a vector norm in Python?

To find a matrix or vector norm we use function numpy. linalg. norm() of Python library Numpy. This function returns one of the seven matrix norms or one of the infinite vector norms depending upon the value of its parameters.

What is the norm of an array?

norm() is called on an array-like input without any additional arguments, the default behavior is to compute the L2 norm on a flattened view of the array. This is the square root of the sum of squared elements and can be interpreted as the length of the vector in Euclidean space.

How do you find the norm of a NumPy array in Python?

Use ord Parameter We can also compute the matrix norm of a NumPy array along with a specified ord Parameter and axis . # use ord Parameter arr2 = np. linalg. norm(arr, ord=1, axis=1) print(arr2) # Output # [19.

What is the norm of a vector matrix?

A norm is a kind of function that measures the length of real vectors and matrices. The notion of length is extremely useful as it enables us to define distance - or similarity - between any two vectors (or matrices) living in the same space.


2 Answers

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.

like image 180
DSM Avatar answered Sep 30 '22 13:09

DSM


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)
like image 31
Shade Avatar answered Sep 30 '22 13:09

Shade