计算两个一维数组的成对元素

计算两个一维数组的成对元素

本文介绍了计算两个一维数组的成对元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:

假设我的两个数组是:

import numpy as np
first = np.array(["hello", "hello", "hellllo"])
second = np.array(["hlo", "halo", "alle"])

现在我想得到两个数组的每个元素之间的距离矩阵

Now I want to get the matrix of distance between each element of the two arrays

例如我的距离函数是:

def diff_len(string1, string2):
    return abs(len(string1) - len(string2))

所以我想得到矩阵:

        hello       hello    hellllo

hlo    result1     result2   result3
halo   result4     result5   result6
alle   result7     result8   result9

所以我所做的是使用 Numpy 的矢量化函数逐行计算:

So what I did was to compute row by row using vectorize function of Numpy :

vectorize_dist = np.vectorize(diff_len)

first = np.array(["hello", "hello", "hellllo"])
second = np.array(["hlo", "halo", "alle"])

vectorize_dist(first , "hlo")
vectorize_dist(first , "halo")
vectorize_dist(first , "alle")

matrix = np.array([vectorize_dist(first , "hlo"), vectorize_dist(first , "halo"), vectorize_dist(first , "alle")])
matrix

array([[2, 2, 4],
       [1, 1, 3],
       [1, 1, 3]])

但是为了得到我的矩阵,我需要执行一个循环来逐行计算,但我想一次得到矩阵.事实上,我的两个数组可能非常大,执行循环可能需要太多时间.我还有多个距离要计算,所以我必须多次执行该过程,这将更加耗时.

But in order to get my matrix I need to execute a loop to compute row after row, but I would like to get the matrix at once.Indeed my two arrays could be very large and executing a loop could take too much time.also I have multiple distance to compute so I would have to execute the procedure multiple time which will be even more time consuming.

推荐答案

您可以使用 SciPy 的 cdist :

You can use SciPy's cdist for that:

import numpy as np
from scipy.spatial.distance import cdist

def diff_len(string1, string2):
    return abs(len(string1) - len(string2))

first = np.array(["hello", "hello", "hellllo"])
second = np.array(["hlo", "halo", "alle"])
d = cdist(first[:, np.newaxis], second[:, np.newaxis], lambda a, b: diff_len(a[0], b[0]))
print(d.T)
# [[2. 2. 4.]
#  [1. 1. 3.]
#  [1. 1. 3.]]

请注意,您需要转换输出矩阵类型以使其成为整数.

Note that you would need to cast the output matrix type to make it integer though.

这篇关于计算两个一维数组的成对元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 09:57