similarity()

audmath.similarity(u, v)[source]

Cosine similarity between two arrays.

If the incoming arrays are of size (k,)(k,), a single similarity value is returned. If one of the incoming arrays is of size (n,k)(n, k), an array of size (n,)(n,) with similarities is returned. If the arrays are of size (n,k)(n, k) and (m,k)(m, k) an array of size (n,m)(n, m) with similarities is returned.

The input arrays can also be provided as pandas.DataFrame or pandas.Series.

The cosine similarity is given by uvu2v2\frac{u \cdot v}{\lVert u\rVert_2 \lVert v\rVert_2}.

Parameters
Return type

Union[floating, ndarray]

Returns

similarity between arrays

Example

>>> similarity([1, 0], [1, 0])
np.float64(1.0)
>>> similarity([1, 0], [0, 1])
np.float64(0.0)
>>> similarity([1, 0], [-1, 0])
np.float64(-1.0)
>>> similarity([[1, 0]], [1, 0])
array([1.])
>>> similarity([1, 0], [[1, 0], [0, 1]])
array([1., 0.])
>>> similarity([[1, 0], [0, 1]], [[1, 0]])
array([[1.],
       [0.]])
>>> similarity([[1, 0], [0, 1]], [[1, 0], [0, 1], [-1, 0]])
array([[ 1.,  0., -1.],
       [ 0.,  1.,  0.]])