Calculate the Euclidean norm at 100 points of n-measures?

I have a list of 100 values ​​in python, where each value in the list corresponds to an n-dimensional list.

For instance,

x=[[1 2],[2 3]] is a 2d list 

I want to calculate the Euclidean norm at all such points. Is there a standard method for doing this?

+4
source share
4 answers

I found this on scipy and it works. scipy

+1
source

If I interpreted the question correctly, then you have a list of 100 n-dimensional vectors, and you need a list of their (Euclidean) norms.

I think using numpy is easier (and faster!) Here,

 import numpy as np a = np.array(x) np.sqrt((a*a).sum(axis=1)) 

If the vectors do not have the same dimension or if you want to avoid numpy, then perhaps

 [sum([i*i for i in vec])**0.5 for vec in x] 

or,

 import math [math.sqrt(sum([i*i for i in vec])) for vec in x] 

Change Not quite sure what you requested. So, alternatively: it looks like you have a list, each element of which is an n-dimensional vector, and you want the Euclidean distance between each consecutive pair. With numpy (assuming n is fixed)

 x = [ [1,2,3], [4,5,6], [8,9,10], [13,14,15] ] # 3D example. import numpy as np a = np.array(x) sqrDiff = (a[:-1] - a[1:])**2 np.sqrt(sqrDiff.sum(axis=1)) 

where the last line is returned,

 array([ 5.19615242, 6.92820323, 8.66025404]) 
+1
source

Try this code:

 from math import sqrt valueList = [[[1,2], [2,3]], [[2,2], [3,3]]] def distance(valueList): resultList = [] for (point1, point2) in valueList: resultList.append(sqrt(sum(map(lambda (x1, x2): (x1 - x2) * (x1 - x2), zip(point1, point2))))) return resultList print distance(valueList) 

output [1.4142135623730951, 1.4142135623730951]

Here the valuelist contains 2 values, but not a problem with 100 values.

0
source

You can do this to calculate the Euclidean norm of each row:

 >>> a = np.arange(200.).reshape((100,2)) >>> a array([[ 0., 1.], [ 2., 3.], [ 4., 5.], [ 6., 7.], [ 8., 9.], [ 10., 11.], ... >>> np.sum(a**2,axis=-1) ** .5 array([ 1. , 3.60555128, 6.40312424, 9.21954446, 12.04159458, 14.86606875, 17.69180601, 20.51828453, 23.34523506, 26.17250466, 29. , 31.82766093, 34.6554469 , 37.48332963, 40.31128874, 43.13930922, 45.96737974, 48.7954916 , 51.623638 , 54.45181356, ... 
0
source

All Articles