Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:并集上的交集_Python_Numpy_Scipy - Fatal编程技术网

Python:并集上的交集

Python:并集上的交集,python,numpy,scipy,Python,Numpy,Scipy,我有以下问题。我试图计算并集上的交点,即两个组件的重叠除以两个组件的并集。假设component1是第一个对象所在的矩阵,component2是第二个对象所在的矩阵。我可以用np.logical\u和(component==1,component2==1)计算重叠。但是我怎样才能说服工会呢?我只对连接的对象感兴趣 import numpy as np component1 = np.array([[0,1,1],[0,1,1],[0,1,1]]) component2 = np.array([[

我有以下问题。我试图计算并集上的交点,即两个组件的重叠除以两个组件的并集。假设component1是第一个对象所在的矩阵,component2是第二个对象所在的矩阵。我可以用
np.logical\u和(component==1,component2==1)
计算重叠。但是我怎样才能说服工会呢?我只对连接的对象感兴趣

import numpy as np
component1 = np.array([[0,1,1],[0,1,1],[0,1,1]])
component2 = np.array([[1,1,0],[1,1,0],[1,1,0]])
overlap = np.logical_and(component == 1, component2 == 1)
union = ?
IOU = len(overlap)/len(union)

如果只处理
0
1
,则使用布尔数组更容易:

import numpy as np
component1 = np.array([[0,1,1],[0,1,1],[0,1,1]], dtype=bool)
component2 = np.array([[1,1,0],[1,1,0],[1,1,0]], dtype=bool)

overlap = component1*component2 # Logical AND
union = component1 + component2 # Logical OR

IOU = overlap.sum()/float(union.sum()) # Treats "True" as 1,
                                       # sums number of Trues
                                       # in overlap and union
                                       # and divides

>>> 1*overlap
array([[0, 1, 0],
       [0, 1, 0],
       [0, 1, 0]])
>>> 1*union
array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]])
>>> IOU
0.3333333333333333

Oleksii为您的问题提供了答案

简言之:

intersection = numpy.logical_and(result1, result2)

union = numpy.logical_or(result1, result2)

iou_score = numpy.sum(intersection) / numpy.sum(union)

print(‘IoU is %s’ % iou_score)

而且,他对这一点做了很好的解释。请看上面的链接。

从您的帖子中输入的内容中,您想要的输入是什么?您是指我想要的输出吗?这将是:IOU=重叠/Union对不起,是的,期望的输出..不清楚在所有这些结束时想要什么。我想要的是Scour或数字,即重叠点的数量除以unioin点的数量。根据这个分数,我进行后处理计算,把数组当作布尔值就是诀窍,非常感谢!我会在这里使用np.count\u nonzero而不是sum,因为它要快得多