python - 如何 numpy searchsorted 在 1 列上平分 2 个值的范围并在第 2 列中获取最小值

标签 python numpy

所以我有一个 2 列 numpy 整数数组,比如:

tarray = array([[ 368,  322],
       [ 433,  420],
       [ 451,  412],
       [ 480,  440],
       [ 517,  475],
       [ 541,  503],
       [ 578,  537],
       [ 607,  567],
       [ 637,  599],
       [ 666,  628],
       [ 696,  660],
       [ 726,  687],
       [ 756,  717],
       [ 785,  747],
       [ 815,  779],
       [ 845,  807],
       [ 874,  837],
       [ 905,  867],
       [ 934,  898],
       [ 969,  928],
       [ 994,  957],
       [1027,  987],
       [1057, 1017],
       [1086, 1047],
       [1117, 1079],
       [1148, 1109],
       [1177, 1137],
       [1213, 1167],
       [1237, 1197],
       [1273, 1227],
       [1299, 1261],
       [1333, 1287],
       [1357, 1317],
       [1393, 1347],
       [1416, 1377]])

我正在使用 np.searchsorted 将较低和较高范围的值平分到第 0 列,即两次都可以,例如 241,361 平分到数组中。

ranges = [array([241, 290, 350, 420, 540, 660, 780, 900]),
 array([ 361,  410,  470,  540,  660,  780,  900, 1020])]

例如:np.searchsorted(tarray[:,0], 范围)

这会导致:

array([[ 0,  0,  0,  1,  5,  9, 13, 17],
       [ 0,  1,  3,  5,  9, 13, 17, 21]])

两个结果数组中的每个位置都是值的范围。然后我想要做的是在结果切片的第 1 列中获取最小值的位置。例如,这就是我在 Python 中通过迭代简单表达的意思(如果 searchsorted 的结果是 2 列数组 'f'):

f = array([[ 0,  0,  0,  1,  5,  9, 13, 17],
       [ 0,  1,  3,  5,  9, 13, 17, 21]])

for i,(x,y) in enumerate(zip(*f)):
    if y - x:
        print ranges[1][i], tarray[x:y]

结果是:

410 [[368 322]]
470 [[368 322]
 [433 420]
 [451 412]]
540 [[433 420]
 [451 412]
 [480 440]
 [517 475]]
660 [[541 503]
 [578 537]
 [607 567]
 [637 599]]
780 [[666 628]
 [696 660]
 [726 687]
 [756 717]]
900 [[785 747]
 [815 779]
 [845 807]
 [874 837]]
1020 [[905 867]
 [934 898]
 [969 928]
 [994 957]]

现在解释一下我想要什么:在切片范围内,我想要第 1 列中具有最小值的行。

e.g 540 [[433 420]
 [451 412]
 [480 440]
 [517 475]]

我希望最终结果为 412(如 [451 412])

例如

for i,(x,y) in enumerate(zip(*f)):
    if y - x:
        print ranges[1][i], tarray[:,1:2][x:y].min()

410 322
470 322
540 412
660 503
780 628
900 747
1020 867

基本上我想对其进行矢量化,这样我就可以取回一个数组而不需要迭代,因为它无法满足我的需求。我想要第 1 列中的最小值,用于第 0 列的二等分值范围。

希望我说清楚了!

最佳答案

这似乎实现了您的预期目标,使用 numpy_indexed包(免责声明:我是它的作者):

import numpy_indexed as npi
# to vectorize the concatenation of the slice ranges, we construct all indices implied in the slicing
counts = f[1] - f[0]
idx = np.ones(counts.sum(), dtype=np.int)
idx[np.cumsum(counts)[:-1]] -= counts[:-1]
tidx = np.cumsum(idx) - 1 + np.repeat(f[0], counts)

# combined with a unique label tagging the output of each slice range, this allows us to use grouping to find the minimum in each group
label = np.repeat(np.arange(len(f.T)), counts)
subtarray = tarray[tidx]
ridx, sidx = npi.group_by(label).argmin(subtarray[:, 0])

print(ranges[1][ridx])
print(subtarray[sidx, 1])

关于python - 如何 numpy searchsorted 在 1 列上平分 2 个值的范围并在第 2 列中获取最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38588490/

相关文章:

numpy - numpy.zeros 和 numpy.random.randn 的 numpy 参数不一致的任何原因

python - numpy 的百分位数函数究竟做了什么?

python - 如何使用 numpy.char.join?

python - 如何将包含多个字符串的 python 数组保存到人类可读的文件中

python - 如果存在于另一个数组中,则从一个数组中删除元素,保留重复元素 - NumPy/Python

javascript - Tornado Websockets 演示无法在 OpenShift 上运行

python - 从 Apache Spark 中的模式获取数据类型列表

python - PyTorch 索引 : select complement of indices