简体   繁体   English

计算每行特定值内的numpy数组中的实例

[英]Count instances in numpy array within a certain value of each row

I have a numpy array such as this 我有一个像这样的numpy数组

[[ 0, 57],
 [ 7, 72],
 [ 2, 51],
 [ 8, 67],
 [ 4, 42]]

I want to find out for each row, how many elements in the 2nd column are within a certain distance (say, 10) of the 2nd column value for that row. 我想为每一行找出第二列中有多少个元素在该行第二列值的特定距离内(例如10)。 So in this example, here the solution would be 所以在这个例子中,这里的解决方案是

[[ 0, 57, 3],
 [ 7, 72, 2],
 [ 2, 51, 3],
 [ 8, 67, 3],
 [ 4, 42, 2]]

So [first row, third column] is 3, because there are 3 elements in the 2nd column (57,51,67) which are within distance 10 from 57. Similarly for each row 因此,[第一行,第三列]为3,因为第二列(57,51,67)中有3个元素,它们与57之间的距离为10。

Any help would be appreciated! 任何帮助,将不胜感激!

Here's one approach leveraging broadcasting with outer-subtraction - 这是利用broadcastingouter-subtraction的一种方法-

(np.abs(a[:,1,None] - a[:,1]) <= 10).sum(1)

With outer subtract builtin and count_nonzero for counting - 使用outer subtract builtincount_nonzero进行计数-

np.count_nonzero(np.abs(np.subtract.outer(a[:,1],a[:,1]))<=10,axis=1)

Sample run - 样品运行-

# Input array
In [23]: a
Out[23]: 
array([[ 0, 57],
       [ 7, 72],
       [ 2, 51],
       [ 8, 67],
       [ 4, 42]])

# Get count
In [24]: count = (np.abs(a[:,1,None] - a[:,1]) <= 10).sum(1)

In [25]: count
Out[25]: array([3, 2, 3, 3, 2])

# Stack with input
In [26]: np.c_[a,count]
Out[26]: 
array([[ 0, 57,  3],
       [ 7, 72,  2],
       [ 2, 51,  3],
       [ 8, 67,  3],
       [ 4, 42,  2]])

Alternatively with SciPy's cdist - 或者使用SciPy's cdist

In [53]: from scipy.spatial.distance import cdist

In [54]: (cdist(a[:,None,1],a[:,1,None], 'minkowski', p=2)<=10).sum(1)
Out[54]: array([3, 2, 3, 3, 2])

For million rows in the input, we might want to resort to a loopy one - 对于输入中的一百万行,我们可能要求助于循环的-

n = len(a)
count = np.empty(n, dtype=int)
for i in range(n):
    count[i] = np.count_nonzero(np.abs(a[:,1]-a[i,1])<=10)

Here's a non-broadcasting approach, which takes advantage of the fact that to know how many numbers are within 3 of 10, you can subtract the number of numbers <= 13 from those strictly less than 7. 这是一种非广播方法,它利用以下事实:要知道10个数字中有3个数字,可以从严格小于7的数字中减去<= 13的数字。

import numpy as np

def broadcast(x, width):
    # for comparison
    return (np.abs(x[:,None] - x) <= width).sum(1)


def largest_leq(arr, x, allow_equal=True):
    maybe = np.searchsorted(arr, x)
    maybe = maybe.clip(0, len(arr) - 1)
    above = arr[maybe] > x if allow_equal else arr[maybe] >= x
    maybe[above] -= 1
    return maybe

def faster(x, width):
    uniq, inv, counts = np.unique(x, return_counts=True, return_inverse=True)
    counts = counts.cumsum()

    low_bounds = uniq - width
    low_ix = largest_leq(uniq, low_bounds, allow_equal=False)
    low_counts = counts[low_ix]
    low_counts[low_ix < 0] = 0

    high_bounds = uniq + width
    high_counts = counts[largest_leq(uniq, high_bounds)]

    delta = high_counts - low_counts
    out = delta[inv]
    return out

This passes my tests: 这通过了我的测试:

for width in range(1, 10):
    for window in range(5):
        for trial in range(10):
            x = np.random.randint(0, 10, width)
            b = broadcast(x, window).tolist()
            f = faster(x, window).tolist()
            assert b == f

and behaves pretty well even at larger sizes: 并且即使在较大尺寸下也表现良好:

In [171]: x = np.random.random(10**6)

In [172]: %time faster(x, 0)
Wall time: 386 ms
Out[172]: array([1, 1, 1, ..., 1, 1, 1], dtype=int64)

In [173]: %time faster(x, 1)
Wall time: 372 ms
Out[173]: array([1000000, 1000000, 1000000, ..., 1000000, 1000000, 1000000], dtype=int64)

In [174]: x = np.random.randint(0, 10, 10**6)

In [175]: %timeit faster(x, 3)
10 loops, best of 3: 83 ms per loop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM