Efficiently counting number of unique elements - NumPy / Python

后端 未结 3 2129
遥遥无期
遥遥无期 2021-01-03 02:07

When running np.unique(), it first flattens the array, sorts the array, then finds the unique values. When I have arrays with shape (10, 3000, 3000), it takes about a second

3条回答
  •  無奈伤痛
    2021-01-03 02:32

    Here's a method that works for an array with dtype np.uint8 that is faster than np.unique.

    First, create an array to work with:

    In [128]: a = np.random.randint(1, 128, size=(10, 3000, 3000)).astype(np.uint8)
    

    For later comparison, find the unique values using np.unique:

    In [129]: u = np.unique(a)
    

    Here's the faster method; v will contain the result:

    In [130]: q = np.zeros(256, dtype=int)
    
    In [131]: q[a.ravel()] = 1
    
    In [132]: v = np.nonzero(q)[0]
    

    Verify that we got the same result:

    In [133]: np.array_equal(u, v)
    Out[133]: True
    

    Timing:

    In [134]: %timeit u = np.unique(a)
    2.86 s ± 9.02 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
    
    In [135]: %timeit q = np.zeros(256, dtype=int); q[a.ravel()] = 1; v = np.nonzero(q)
    300 ms ± 5.52 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
    

    So 2.86 seconds for np.unique(), and 0.3 seconds for the alternative method.

提交回复
热议问题