23 lines
667 B
Python
23 lines
667 B
Python
import numpy as np
|
|
|
|
|
|
def bhattarcharyya_coefficient(distribution_1, distribution_2):
|
|
"""
|
|
Computes the difference between two normalized statistical distributions.
|
|
E.g. of two color histograms.
|
|
Smaller values indicate higher similarity.
|
|
"""
|
|
|
|
value1 = distribution_1 / np.sum(distribution_1)
|
|
value2 = distribution_2 / np.sum(distribution_2)
|
|
|
|
return np.sum(np.sqrt(value1 * value2))
|
|
|
|
|
|
def cosine_similarity(vector_1, vector_2):
|
|
"""
|
|
Calculates the cosine similarity of two vectors.
|
|
Higher values indicate more alike vectors.
|
|
"""
|
|
return np.dot(vector_1, vector_2) / (np.linalg.norm(vector_1) * np.linalg.norm(vector_2))
|