我想计算两个数值范围的重叠分数。让我用一个例子说明我的问题,因为我相信它会更容易理解。
假设我有两个数值范围:
A = [1,100]
B = [25,100]
我想知道的(和代码)是B重叠多少A反之亦然(A重叠B多少)
在这种情况下, A 重叠 B (作为B的一部分)100%, B 重叠 A (作为A的一小部分)减少75%。
我试过在python中尝试编写这个代码,但是我很挣扎,我无法找到计算两个分数的正确解决方案
到目前为止我能够取得的成就如下:
考虑到两个数值范围的开始和结束,我已经能够确定两个数值范围是否重叠(from other stackoverflow post)
我已使用以下代码完成此操作
def is_overlapping(x1,x2,y1,y2):
return max(x1,y1) <= min(x2,y2)
谢谢!
答案 0 :(得分:2)
这是一个没有for循环的快速解决方案:
def overlapping(x1,x2,y1,y2):
#A = [x1,x2]
#B = [y1,y1]
# Compute the B over A
if(x1 <= y1 and x2 >= y2): # Total overlapping
return 1
elif(x2 < y1 or y2 < x1):
return 0
elif(x2 == y1 or x1 == y2):
return 1/float(y2 - y1 + 1)
return (min(x2,y2) - max(x1,y1))/float(y2 - y1)
答案 1 :(得分:2)
一种(效率较低)的方法是使用集合。 如果设置范围
A = range(1,101)
B = range(25, 101)
然后你可以按如下方式找到你的分数:
len(set(A)&set(B))/float(len(set(B)))
和
len(set(A)&set(B))/float(len(set(A)))
给出1.0和0.76。
B中有76个点也在A中(因为你的范围似乎包括在内)。
正如其他答案所示,有更有效的方法可以使用某些数学来做到这一点,但这是通用目的。
答案 2 :(得分:0)
我相信有无数种方法可以解决这个问题。我想到的第一个是充分利用sum
函数,它也可以总结一个迭代:
a = range(1,100)
b = range(25,100)
sum_a = sum(1 for i in b if i in a)
sum_b = sum(1 for i in a if i in b)
share_a = sum_a*100 / len(b)
share_b = sum_b*100 / len(a)
print(share_a, share_b)
>>> 100 75
这可能会有点麻烦,例如如果您不使用范围但使用未排序的列表。
答案 3 :(得分:0)
这是我的解决方案,使用numpy&amp; python3
import numpy as np
def my_example(A,B):
# Convert to numpy arrays
A = np.array(A)
B = np.array(B)
# determine which elements are overlapping
overlapping_elements=np.intersect1d(A, B)
# determine how many there are
coe=overlapping_elements.size
#return the ratios
return coe/A.size , coe/B.size
# Generate two test lists
a=[*range(1,101)]
b=[*range(25,101)]
# Call the example & print the results
x,y = my_example(a,b) # returns ratios, multiply by 100 for percentage
print(x,y)
答案 4 :(得分:0)
我假设下限和上限都包含在范围内。这是我计算重叠距离的方法:
@Test(priority = 2)
@Parameters({"browser"})
public void browsers(@Optional("chrome") String browsername){
System.out.println(browsername);
if(browsername.equalsIgnoreCase("chrome")) {
System.out.println("You have selected " + browsername);
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir") + "\\Drivers\\Windows\\chromedriver.exe");
driver = new ChromeDriver();
}
else {
System.out.println("You have not selected chrome");
}
driver.manage().window().maximize();
driver.get("www.yahoo.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
<强>输出:强>
def is_valid(x):
try:
valid = (len(x) == 2) and (x[0] <= x[1])
except:
valid = False
finally:
return valid
def is_overlapping(x,y):
return max(x[0],y[0]) <= min(x[1],y[1])
def overlapping_percent(x,y):
if(is_valid(x) and is_valid(y)) == False:
raise ValueError("Invalid range")
if is_overlapping(x,y):
overlapping_distance = min(x[1],y[1]) - max(x[0],y[0]) + 1
width_x = x[1] - x[0] + 1
width_y = y[1] - y[0] + 1
overlap_x = overlapping_distance * 100.0/width_y
overlap_y = overlapping_distance *100.0/width_x
return (overlap_x, overlap_y)
return (0,0);
if __name__ == '__main__':
try:
print(overlapping_percent((1,100),(26,100)))
print(overlapping_percent((26,100),(1,100)))
print(overlapping_percent((26,50),(1,100)))
print(overlapping_percent((1,100),(26,50)))
print(overlapping_percent((1,100),(200,300)))
print(overlapping_percent((26,150),(1,100)))
print(overlapping_percent((126,50),(1,100)))
except Exception as e:
print(e)
我希望它有所帮助。