xvt*_*vtk 5 python partitioning numpy
我有两个长度相等的数组(x1和x2),它们具有重叠的值范围.
我需要找到一个值q,从而l1-l2最小化,并
l1 = x1[np.where(x1 > q)].shape[0]
l2 = x2[np.where(x2 < q)].shape[0]
Run Code Online (Sandbox Code Playgroud)
我需要它才能获得相当高的性能,因为阵列可能很大.使用原生numpy例程的解决方案将是首选.
我想我可能已经找到了一种相当简单的方法来做到这一点。
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
Run Code Online (Sandbox Code Playgroud)
M4rtini 的解决方案生成q = 38.7867527.