python - 从两个可变长度字符串数组返回相似度矩阵(scipy 选项?)

标签 python matrix scipy distance levenshtein-distance

假设我有两个数组:

import numpy as np
arr1 = np.array(['faucet', 'faucets', 'bath', 'parts', 'bathroom'])
arr2 = np.array(['faucett', 'faucetd', 'bth', 'kichen'])

我想计算 arr2 中的字符串与 arr1 中的字符串的相似度。

arr1 是拼写正确的单词的数组。

arr2 是单词字典中无法识别的单词数组。

我想返回一个矩阵,然后将其转换为 pandas DataFrame。

我当前的解决方案(credit):

from scipy.spatial.distance import pdist, squareform
from Levenshtein import ratio
arr3 = np.concatenate((arr1, arr2)).reshape(-1,1)
matrix = squareform(pdist(arr3, lambda x,y: ratio(x[0], y[0])))
df = pd.DataFrame(matrix, index=arr3.ravel(), columns=arr3.ravel())

输出:

            faucet   faucets      bath     parts  bathroom   faucett  \
faucet    0.000000  0.923077  0.400000  0.363636  0.285714  0.923077   
faucets   0.923077  0.000000  0.363636  0.500000  0.266667  0.857143   
bath      0.400000  0.363636  0.000000  0.444444  0.666667  0.363636   
parts     0.363636  0.500000  0.444444  0.000000  0.307692  0.333333   
bathroom  0.285714  0.266667  0.666667  0.307692  0.000000  0.266667   
faucett   0.923077  0.857143  0.363636  0.333333  0.266667  0.000000   
faucetd   0.923077  0.857143  0.363636  0.333333  0.266667  0.857143   
bth       0.222222  0.200000  0.857143  0.250000  0.545455  0.200000   
kichen    0.333333  0.307692  0.200000  0.000000  0.142857  0.307692   

           faucetd       bth    kichen  
faucet    0.923077  0.222222  0.333333  
faucets   0.857143  0.200000  0.307692  
bath      0.363636  0.857143  0.200000  
parts     0.333333  0.250000  0.000000  
bathroom  0.266667  0.545455  0.142857  
faucett   0.857143  0.200000  0.307692  
faucetd   0.000000  0.200000  0.307692  
bth       0.200000  0.000000  0.222222  
kichen    0.307692  0.222222  0.000000

此解决方案的问题: 我浪费时间计算我已经知道拼写正确的单词的成对距离比。

我想要的是传递一个函数arr1arr2(它们可以是不同的长度!)并输出一个具有比率的矩阵(不一定是正方形) .

结果将如下所示(没有计算开销):

>>> df.drop(index=arr1, columns=arr2)

           faucet   faucets      bath     parts  bathroom
faucett  0.923077  0.857143  0.363636  0.333333  0.266667
faucetd  0.923077  0.857143  0.363636  0.333333  0.266667
bth      0.222222  0.200000  0.857143  0.250000  0.545455
kichen   0.333333  0.307692  0.200000  0.000000  0.142857

最佳答案

我认为您正在寻找 cdist :

import pandas as pd
import numpy as np
from scipy.spatial.distance import cdist
from Levenshtein import ratio

arr1 = np.array(['faucet', 'faucets', 'bath', 'parts', 'bathroom'])
arr2 = np.array(['faucett', 'faucetd', 'bth', 'kichen'])

matrix = cdist(arr2.reshape(-1, 1), arr1.reshape(-1, 1), lambda x, y: ratio(x[0], y[0]))
df = pd.DataFrame(data=matrix, index=arr2, columns=arr1)

结果:

           faucet   faucets      bath     parts  bathroom
faucett  0.923077  0.857143  0.363636  0.333333  0.266667
faucetd  0.923077  0.857143  0.363636  0.333333  0.266667
bth      0.222222  0.200000  0.857143  0.250000  0.545455
kichen   0.333333  0.307692  0.200000  0.000000  0.142857

关于python - 从两个可变长度字符串数组返回相似度矩阵(scipy 选项?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50648860/

相关文章:

python - Django:传输/访问表单完整错误消息

r - R中的矩阵和向量乘法运算

python - 生成 3D 高斯数据

python - 为什么 psycopg2 对我这么慢?

python - 高效构建 FEM/FVM 矩阵

c - 随机矩阵结构创建

c++ - 如何从文件中一个一个地读取矩阵元素

python - 使用 sklearn 通过类对字符串进行分类

python - 为什么 scipy.minimize 忽略我的约束?

python - 使用 hex 和 ascii 混合接收的 UDP 字节;如何解码?