两个数组之间的余弦距离计算 - Python

时间:2017-04-19 10:34:54

标签: python arrays performance numpy vectorization

我想应用一个函数fn,它基本上cosine distance计算两个大的numpy形状数组(10000,100)和(5000,100)行,即我计算一个这些数组中每个行组合的值。

我的实施:

import math
def fn(v1,v2):
    sumxx, sumxy, sumyy = 0, 0, 0
    for i in range(len(v1)):
        x = v1[i]; y = v2[i]
        sumxx += x*x
        sumyy += y*y
        sumxy += x*y
    return sumxy/math.sqrt(sumxx*sumyy)
val = []
for i in range(array1.shape[0]):
    for j in range(array2.shape[0]):
        val.append(fn(array1[i, :], array2[j, :]))

该功能非常快,只需几毫秒:

CPU times: user 4 ms, sys: 0 ns, total: 4 ms
Wall time: 1.24 ms

有没有有效的方法可以做到这一点?

1 个答案:

答案 0 :(得分:2)

方法#1:我们可以简单地使用Scipy's cdist及其cosine距离功能 -

from scipy.spatial.distance import cdist

val_out = 1 - cdist(array1, array2, 'cosine')

方法#2:使用matrix-multiplication -

的另一种方法
def cosine_vectorized(array1, array2):
    sumyy = (array2**2).sum(1)
    sumxx = (array1**2).sum(1, keepdims=1)
    sumxy = array1.dot(array2.T)
    return (sumxy/np.sqrt(sumxx))/np.sqrt(sumyy)

方法#3:使用np.einsum计算另一个的自平方和 -

def cosine_vectorized_v2(array1, array2):
    sumyy = np.einsum('ij,ij->i',array2,array2)
    sumxx = np.einsum('ij,ij->i',array1,array1)[:,None]
    sumxy = array1.dot(array2.T)
    return (sumxy/np.sqrt(sumxx))/np.sqrt(sumyy)

方法#4:引入numexpr module卸载另一种方法的square-root次计算 -

import numexpr as ne

def cosine_vectorized_v3(array1, array2):
    sumyy = np.einsum('ij,ij->i',array2,array2)
    sumxx = np.einsum('ij,ij->i',array1,array1)[:,None]
    sumxy = array1.dot(array2.T)
    sqrt_sumxx = ne.evaluate('sqrt(sumxx)')
    sqrt_sumyy = ne.evaluate('sqrt(sumyy)')
    return ne.evaluate('(sumxy/sqrt_sumxx)/sqrt_sumyy')

运行时测试

# Using same sizes as stated in the question
In [185]: array1 = np.random.rand(10000,100)
     ...: array2 = np.random.rand(5000,100)
     ...: 

In [194]: %timeit 1 - cdist(array1, array2, 'cosine')
1 loops, best of 3: 366 ms per loop

In [195]: %timeit cosine_vectorized(array1, array2)
1 loops, best of 3: 287 ms per loop

In [196]: %timeit cosine_vectorized_v2(array1, array2)
1 loops, best of 3: 283 ms per loop

In [197]: %timeit cosine_vectorized_v3(array1, array2)
1 loops, best of 3: 217 ms per loop