查找相等地划分两个Numpy数组的值 [英] Find Value that Partitions two Numpy Arrays Equally

查看:104
本文介绍了查找相等地划分两个Numpy数组的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个长度相等的数组(x1x2),它们的值范围重叠.

I have two arrays (x1 and x2) of equal length that have overlapping ranges of values.

我需要找到一个值q,以使l1-l2最小化,并且

I need to find a value q such that l1-l2 is minimized, and

l1 = x1[np.where(x1 > q)].shape[0]
l2 = x2[np.where(x2 < q)].shape[0]

由于阵列可能很大,因此我需要使其具有合理的高性能.首选使用本机numpy例程的解决方案.

I need this to be reasonably high-performance since the arrays can be large. A solution using native numpy routines would be preferred.

推荐答案

我想我可能已经找到了一种非常简单的方法.

I think I may have found a fairly simple way to do it.

x1 = (50 - 10) * np.random.random(10000) + 10
x2 = (75 - 25) * np.random.random(10000) + 25

x1.sort()
x2.sort()
x2 = x2[::-1] # reverse the array

# The overlap point should fall where the difference is smallest
diff = np.abs(x1 - x2)

# get the index of where the minimum occurs
loc = np.where(diff == np.min(diff))

q1 = x1[loc]    # 38.79087351
q2 = x2[loc]    # 38.79110941

M4rtini的解决方案生成q = 38.7867527.

M4rtini's solution produces q = 38.7867527.

这篇关于查找相等地划分两个Numpy数组的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆