python numpy pairwise edit-distance

时间:2014-06-06 20:15:55

标签: python numpy scipy lambda pdist

所以,我有一个numpy字符串数组,我想用这个函数计算每对元素之间的成对编辑距离:来自http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.spatial.distance.pdist.html的scipy.spatial.distance.pdist

我的数组样本如下:

 >>> d[0:10]
 array(['TTTTT', 'ATTTT', 'CTTTT', 'GTTTT', 'TATTT', 'AATTT', 'CATTT',
   'GATTT', 'TCTTT', 'ACTTT'], 
  dtype='|S5')

但是,因为它没有“编辑距离”。因此,我想提供一个定制的距离函数。我试过这个,我遇到了以下错误:

 >>> import editdist
 >>> import scipy
 >>> import scipy.spatial
 >>> scipy.spatial.distance.pdist(d[0:10], lambda u,v: editdist.distance(u,v))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/epd-7.3.2/lib/python2.7/site-packages/scipy/spatial/distance.py", line 1150, in pdist
    [X] = _copy_arrays_if_base_present([_convert_to_double(X)])
  File "/usr/local/epd-7.3.2/lib/python2.7/site-packages/scipy/spatial/distance.py", line 153, in _convert_to_double
    X = np.double(X)
ValueError: could not convert string to float: TTTTT

2 个答案:

答案 0 :(得分:4)

如果您真的必须使用pdist,首先需要将字符串转换为数字格式。如果您知道所有字符串的长度相同,则可以相当容易地完成此操作:

numeric_d = d.view(np.uint8).reshape((len(d),-1))

这只是将您的字符串数组视为一个长uint8个字节的数组,然后重新整形它,使每个原始字符串本身在一行上。在您的示例中,这看起来像:

In [18]: d.view(np.uint8).reshape((len(d),-1))
Out[18]:
array([[84, 84, 84, 84, 84],
       [65, 84, 84, 84, 84],
       [67, 84, 84, 84, 84],
       [71, 84, 84, 84, 84],
       [84, 65, 84, 84, 84],
       [65, 65, 84, 84, 84],
       [67, 65, 84, 84, 84],
       [71, 65, 84, 84, 84],
       [84, 67, 84, 84, 84],
       [65, 67, 84, 84, 84]], dtype=uint8)

然后,您可以像往常一样使用pdist。只需确保您的editdist函数需要整数数组,而不是字符串。您可以通过调用.tostring()快速转换新输入:

def editdist(x, y):
  s1 = x.tostring()
  s2 = y.tostring()
  ... rest of function as before ...

答案 1 :(得分:-4)

&#13;
&#13;
def my_pdist(data,f):
    N=len(data)
    matrix=np.empty([N*(N-1)/2])
    ind=0
    for i in range(N):
        for j in range(i+1,N):
            matrix[ind]=f(data[i],data[j])
            ind+=1
    return matrix
&#13;
&#13;
&#13;