我现在正在做一些文本分析,作为其中的一部分,我需要获得特定列表中所有单词之间的 Jaro 距离矩阵(成对距离矩阵),如下所示:
│CHEESE CHORES GEESE GLOVES
───────┼───────────────────────────
CHEESE │ 0 0.222 0.177 0.444
CHORES │0.222 0 0.422 0.333
GEESE │0.177 0.422 0 0.300
GLOVES │0.444 0.333 0.300 0
因此,我尝试使用 numpy.fromfunction
构建它。根据文档和示例,它将坐标传递给函数,获取结果,构建结果矩阵。
我尝试了以下方法:
from jellyfish import jaro_distance
def distance(i, j):
return 1 - jaro_distance(feature_dict[i], feature_dict[j])
feature_dict = 'CHEESE CHORES GEESE GLOVES'.split()
distance_matrix = np.fromfunction(distance, shape=(len(feature_dict),len(feature_dict)))
注意:jaro_distance 只接受 2 个字符串并返回一个 float 。
我得到了一个错误:
File "<pyshell#26>", line 4, in distance
return 1 - jaro_distance(feature_dict[i], feature_dict[j])
TypeError: only integer arrays with one element can be converted to an index
我在函数的开头添加了 print(i)
、print(j)
,我发现传递的不是真实坐标,而是一些奇怪的东西:
[[ 0. 0. 0. 0.]
[ 1. 1. 1. 1.]
[ 2. 2. 2. 2.]
[ 3. 3. 3. 3.]]
[[ 0. 1. 2. 3.]
[ 0. 1. 2. 3.]
[ 0. 1. 2. 3.]
[ 0. 1. 2. 3.]]
为什么? examples在 numpy 网站上清楚地表明只传递了两个整数,没有别的。
我尝试使用 lambda
函数准确重现他们的示例,但我得到完全相同的错误:
distance_matrix = np.fromfunction(lambda i, j: 1 - jaro_distance(feature_dict[i], feature_dict[j]), shape=(len(feature_dict),len(feature_dict)))
感谢任何帮助 - 我想我以某种方式误解了它。
最佳答案
根据@xnx 的建议,我调查了 question发现 fromfunc 不是一个一个地传递坐标,而是同时传递所有的索引。这意味着如果数组的形状是 (2,2) numpy 将不会执行 f(0,0), f(0,1), f(1,0), f(1,1)
,而是会执行:
f([[0., 0.], [1., 1.]], [[0., 1.], [0., 1.]])
但看起来我的特定函数可以矢量化并会产生所需的结果。所以实现所需的代码如下:
from jellyfish import jaro_distance
import numpy
def distance(i, j):
return 1 - jaro_distance(feature_dict[i], feature_dict[j])
feature_dict = 'CHEESE CHORES GEESE GLOVES'.split()
funcProxy = np.vectorize(distance)
distance_matrix = np.fromfunction(funcProxy, shape=(len(feature_dict),len(feature_dict)))
而且效果很好。
关于python - Numpy - 使用 numpy.fromfunction 构建 Jaro(或 Levenshtein)距离矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29806080/