如何计算Python 2范围之间重叠的百分比

发布于 2025-01-20 16:52:55 字数 276 浏览 0 评论 0原文

我想计算两个范围之间重叠的百分比。

例如:我有两个列表 x 和 y x['567.630','592.927'] y ['593.000', '618.297'] 在这种情况下,我希望输出为 0,因为没有重叠。

x['793.843','802.244'] y ['794.843','803.244'] 在这种情况下,我想要 87% 的输出。

x 和 y 的总范围并不总是相同。可能 x 是 10s,y 只是 8s。

难道有一些包可以执行这些计算吗?

先感谢您!

I would like to calculate the percentage of overlap between 2 ranges.

for example: I have two lists x and y
x ['567.630', '592.927']
y ['593.000', '618.297']
In this case I would like an output of 0 as there is no overlap.

x ['793.843', '802.244']
y ['794.843', '803.244']
In this case I would like an output of 87%.

The total range of x and y are not always the same. It could be that x is 10s and y just 8s.

Could it be that there are some packages that perform these calculations?

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙温凉少女 2025-01-27 16:52:55

这是更短的解决方案,您可以在其中选择要比较重叠的列表。如果您希望例如[0,1]的重叠和[0,2]为100%,则可以从输出中进行最大(x,y)。我更改了y列表以显示这一点。

x = [793.843, 802.244]

y = [794.843, 900.244] 


def overlap_percentage(xlist,ylist):
    min1 = min(xlist)
    max1 = max(xlist)
    min2 = min(ylist)
    max2 = max(ylist)

    overlap = max(0, min(max1, max2) - max(min1, min2))
    length = max1-min1 + max2-min2
    lengthx = max1-min1
    lengthy = max2-min2

    return 2*overlap/length , overlap/lengthx  , overlap/lengthy 

 average, x,y = overlap_percentage(x,y)
    
 print("average: ", average*100,"%    x: ", x*100,"%    y: ",y*100,"%")

输出

average:  13.0 %    x:  88.0%    y:  7.0 %

This is a bit shorter solution, where you can choose from which list you want to compare the overlap. If you want the overlap of e.g. [0,1] and [0,2] to be 100% you can just do max(x,y) from the output. I changed y list to show this.

x = [793.843, 802.244]

y = [794.843, 900.244] 


def overlap_percentage(xlist,ylist):
    min1 = min(xlist)
    max1 = max(xlist)
    min2 = min(ylist)
    max2 = max(ylist)

    overlap = max(0, min(max1, max2) - max(min1, min2))
    length = max1-min1 + max2-min2
    lengthx = max1-min1
    lengthy = max2-min2

    return 2*overlap/length , overlap/lengthx  , overlap/lengthy 

 average, x,y = overlap_percentage(x,y)
    
 print("average: ", average*100,"%    x: ", x*100,"%    y: ",y*100,"%")

output

average:  13.0 %    x:  88.0%    y:  7.0 %
星星的軌跡 2025-01-27 16:52:55

无需额外进口。我假设数据是浮点数字列表而不是字符串。百分比计算为与第二个间隔重叠,对于其他情况,请在实现中取消注释该行。

def overlapping_percentage(interval1: list, interval2: list) -> int:
    # min max of each intervals
    m1, M1 = min(interval1), max(interval1)
    m2, M2 = min(interval2), max(interval2)

    # find overlapping boundaries
    lb, ub = 0, 0 # lower and upper bound
    if m1 <= m2 <= M1:
        if M1 >= M2:    # i2 is contained in i1
            lb, ub = m2, M2
        elif M1 < M2:
            lb, ub = m2, M1

    elif m2 <= m1 <= M2:
        if M1 <= M2:    # i1 is contained in i2
            lb, ub = m1, M1
        elif M1 > M2:
            lb, ub = m1, M2

    # percentage
    percent = int((ub - lb) / (interval2[1] - interval2[0]) * 100)
    # percent = int((ub - lb) / (interval1[1] - interval1[0]) * 100) # for the other percentage uncomment this line
    print('overlapping interval', lb, ub)
    print(percent, '%')
    
    return percent


# intervals
#i1, i2 = [567.630, 592.927], [593.000, 618.297]
i1, i2 = [793.843, 802.244], [794.843, 803.244]

overlapping_percentage(i1, i2)
# overlapping interval 794.843 - 802.244
# 88 %

重叠部分可以进一步压缩如下

    ...
    # find overlapping boundaries
    lb, ub = 0, 0 # lower and upper bound
    if m1 <= m2 <= M1:
        lb, ub = m2, min(M1, M2)
    elif m2 <= m1 <= M2:
        lb, ub = m1, min(M1, M2)
    ...

No needs for extra imports. I assumed the data to be list of float numbers and not of string. The percentage is computed as overlapping over second interval, for the other case uncomment the line in the implementation.

def overlapping_percentage(interval1: list, interval2: list) -> int:
    # min max of each intervals
    m1, M1 = min(interval1), max(interval1)
    m2, M2 = min(interval2), max(interval2)

    # find overlapping boundaries
    lb, ub = 0, 0 # lower and upper bound
    if m1 <= m2 <= M1:
        if M1 >= M2:    # i2 is contained in i1
            lb, ub = m2, M2
        elif M1 < M2:
            lb, ub = m2, M1

    elif m2 <= m1 <= M2:
        if M1 <= M2:    # i1 is contained in i2
            lb, ub = m1, M1
        elif M1 > M2:
            lb, ub = m1, M2

    # percentage
    percent = int((ub - lb) / (interval2[1] - interval2[0]) * 100)
    # percent = int((ub - lb) / (interval1[1] - interval1[0]) * 100) # for the other percentage uncomment this line
    print('overlapping interval', lb, ub)
    print(percent, '%')
    
    return percent


# intervals
#i1, i2 = [567.630, 592.927], [593.000, 618.297]
i1, i2 = [793.843, 802.244], [794.843, 803.244]

overlapping_percentage(i1, i2)
# overlapping interval 794.843 - 802.244
# 88 %

The overlapping part can be further compactified as follow

    ...
    # find overlapping boundaries
    lb, ub = 0, 0 # lower and upper bound
    if m1 <= m2 <= M1:
        lb, ub = m2, min(M1, M2)
    elif m2 <= m1 <= M2:
        lb, ub = m1, min(M1, M2)
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文