如何比较两个numpy向量列表?

时间:2017-04-02 17:38:29

标签: python-3.x numpy python-unittest

我有两个listnumpy个向量,并希望确定它们是否代表大致相同的点(但可能的顺序不同)。

我找到了诸如numpy.testing.assert_allclose之类的方法,但它并没有允许可能不同的订单。我还发现了unittest.TestCase.assertCountEqual,但这不适用于numpy数组!

我最好的方法是什么?

import unittest

import numpy as np

first = [np.array([20, 40]), np.array([20, 60])]
second = [np.array([19.8, 59.7]), np.array([20.1, 40.5])]

np.testing.assert_all_close(first, second, atol=2)  # Fails because the orders are different
unittest.TestCase.assertCountEqual(None, first, second)  # Fails because numpy comparisons evaluate element-wise; and because it doesn't allow a tolerance

3 个答案:

答案 0 :(得分:0)

一个很好的列表迭代方法

In [1047]: res = []
In [1048]: for i in first:
      ...:     for j in second:
      ...:         diff = np.abs(i-j)
      ...:         if np.all(diff<2):
      ...:             res.append((i,j))            
In [1049]: res
Out[1049]: 
[(array([20, 40]), array([ 20.1,  40.5])),
 (array([20, 60]), array([ 19.8,  59.7]))]

res的长度是匹配数。

或者作为列表理解:

def match(i,j):
    diff = np.abs(i-j)
    return np.all(diff<2)

In [1051]: [(i,j) for i in first for j in second if match(i,j)]
Out[1051]: 
[(array([20, 40]), array([ 20.1,  40.5])),
 (array([20, 60]), array([ 19.8,  59.7]))]

或使用现有的阵列测试:

[(i,j) for i in first for j in second if np.allclose(i,j, atol=2)]

答案 1 :(得分:0)

你在这里:)

(基于的想法 Euclidean distance between points in two different Numpy arrays, not within

import numpy as np
import scipy.spatial
first  = [np.array([20  , 60  ]), np.array([  20,   40])]
second = [np.array([19.8, 59.7]), np.array([20.1, 40.5])]

def pointsProximityCheck(firstListOfPoints, secondListOfPoints, distanceTolerance): 
    pointIndex  = 0
    maxDistance = 0
    lstIndices  = []
    for item in scipy.spatial.distance.cdist( firstListOfPoints, secondListOfPoints ):
        currMinDist = min(item)
        if currMinDist > maxDistance: 
            maxDistance = currMinDist
        if currMinDist < distanceTolerance :
            pass
        else:
            lstIndices.append(pointIndex)
            # print("point with pointIndex [", pointIndex, "] in the first list outside of Tolerance")
        pointIndex+=1
    return (maxDistance, lstIndices)

maxDistance, lstIndicesOfPointsOutOfTolerance = pointsProximityCheck(first, second, distanceTolerance=0.5)
print("maxDistance:", maxDistance, "indicesOfOutOfTolerancePoints", lstIndicesOfPointsOutOfTolerance )  

给出带有distanceTolerance = 0.5的输出:

maxDistance: 0.509901951359 indicesOfOutOfTolerancePoints [1]

答案 2 :(得分:0)

  

但可能采用不同的顺序

这是关键要求。这个问题可以作为图论中的经典问题 - 在未加权bipartite graph中找到完美匹配。 Hungarian Algorithm是解决此问题的经典算法。

我在这里实施了一个。

import numpy as np

def is_matched(first, second):
    checked = np.empty((len(first),), dtype=bool)
    first_matching = [-1] * len(first)
    second_matching = [-1] * len(second)

    def find(i):
        for j, point in enumerate(second):
            if np.allclose(first[i], point, atol=2):
                if not checked[j]:
                    checked[j] = True
                    if second_matching[j] == -1 or find(second_matching[j]):
                        second_matching[j] = i
                        first_matching[i] = j
                        return True

    def get_max_matching():
        count = 0
        for i in range(len(first)):
            if first_matching[i] == -1:
                checked.fill(False)
                if find(i):
                    count += 1

        return count

    return len(first) == len(second) and get_max_matching() == len(first)

first = [np.array([20, 40]), np.array([20, 60])]
second = [np.array([19.8, 59.7]), np.array([20.1, 40.5])]
print(is_matched(first, second)) 
# True

first = [np.array([20, 40]), np.array([20, 60])]
second = [np.array([19.8, 59.7]), np.array([20.1, 43.5])]
print(is_matched(first, second))
# False