Search code examples
pythonmathmachine-learningeuclidean-distancecosine-similarity

How to calculate weighted similarity with scipy.spatial.distance.cosine?


From the function definition: https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cosine.html

scipy.spatial.distance.cosine(u, v, w=None)

but my codes got some errors:

from scipy import spatial
d1 = [3,5,5,3,3,2]
d2 = [1,1,3,1,3,2]
weight_of_importance = [0.1,0.1,0.2,0.2,0.1,0.3]

result = spatial.distance.cosine(d1, d2, weight_of_importance)
print(result)

TypeError: cosine() takes 2 positional arguments but 3 were given

It works when I only input 2 parameters. But those features got different weighting of importance. How could I calculate the similarity with weighted importance for d1 and d2?


Solution

  • It looks like this parameter has been added in SciPy v1.0.0.

    This parameter is not there in the previous version 0.19.1

    An excerpt from SciPy v1.0.0 release notes:

    scipy.spatial improvements

    Many distance metrics in scipy.spatial.distance gained support for weights.