Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Euclidean distance to range 0 and 1 like Cosine Similarity?

Want to map Euclidean distance to the range [0, 1], somewhat like the cosine similarity of vectors.

For instance

input  output
  0      1.0
  1      0.9  approximate
  2      0.8 to 0.9 somewhere
 inf     0.0

I tried the formula 1/(1+d), but that falls away from 1.0 too quickly.

like image 544
atimetoremember Avatar asked Apr 28 '17 20:04

atimetoremember


People also ask

How do you convert Euclidean distance to similarity score?

To convert distance measure to similarity measure, we need to first normalize d to [0 1], by using d_norm = d/max(d). Then the similarity measure is given by: s = 1 - d_norm.

Why cosine distance is always in the range between 0 and 1?

Cosine similarity can be seen as a method of normalizing document length during comparison. In the case of information retrieval, the cosine similarity of two documents will range from 0 to 1, since the term frequencies cannot be negative.

Is there a relationship between Euclidean distance and cosine similarity?

The Euclidean distance corresponds to the L2-norm of a difference between vectors. The cosine similarity is proportional to the dot product of two vectors and inversely proportional to the product of their magnitudes.


1 Answers

It seems that you want the fraction's denominator to grow more slowly (the denominator is the bottom part, which you have as (d+1) so far). There are various ways to handle this. For instance, try a lower power for d, such as

1 / (1 + d**(0.25))

... or an exponential decay in the denominator, such as

1 / (1.1 ** d)

... or using a trig function to temper your mapping, such as

1 - tanh(d)

Would something in one of these families work for you?

like image 96
Prune Avatar answered Oct 17 '22 08:10

Prune