Skip to content

np.unique(arr, axis) slow for axis=0 #15713

Open
@mganahl

Description

@mganahl

Numpy's unique function is about 10 times slower when acting on arrays with ndim=2 as
compared to ndim=1 arrays. This is even true if the 2d array is a trivial expansion of an original 1d array (see below).

Reproducing code example:

import numpy as np
import time
arr = np.random.randint(0,20,1000)
mat = np.expand_dims(arr,1)
t1 = time.time()
for _ in range(100):
    u = np.unique(arr)
print((time.time() - t1)/100)
t1 = time.time()
for _ in range(100):
    u2 = np.unique(mat, axis=0)
print((time.time() - t1)/100) #this is ~10 times slower
assert np.all(np.squeeze(u2)==u)

Numpy/Python version information:

1.18.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions