Reputation: 71
I have two sets of three-dimensional unit-vectors that I would like to get a measure of how similar they are. My current method is to manually calculate the euclidean norm of their difference.
An example (without considering unit-vectors) would look like
import numpy as np
N = 4000
a = np.random.rand(3,N)
b = np.random.rand(3,N)
dist = np.sum((a-b)**2,axis=0)
dist = np.sum(dist)/len(dist)
Are there better ways to do this; i.e. a numpy function or a measure I haven't thought of?
Upvotes: 1
Views: 3450
Reputation: 161
As discussed in the question comments, this should be more appropriate for what you're trying to do:
from scipy import spatial
import numpy as np
N = 10
a = np.random.rand(3, N)
b = np.random.rand(3, N)
mean_a = np.mean(a, axis=1)
mean_b = np.mean(b, axis=1)
similarity = spatial.distance.cosine(mean_a, mean_b)
Values closer to 1 are more similar, values closer to 0 are less similar.
Upvotes: 0
Reputation: 703
To calculate Euclidean distance in numpy you can use
numpy.linalg.norm(a-b)
Also there are other types of distances, measuring other types of similarities. SciPy has a lot of them implemented and described in docs: https://docs.scipy.org/doc/scipy/reference/spatial.distance.html
Upvotes: 3
Reputation: 786
You can use distance in scipy package.
from scipy.spatial import distance
distance.euclidean(a, b)
Upvotes: 0