question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request `numpy.unique`

See original GitHub issue

Platform: Ubuntu 16.04 Python version: 3.6 Numba version: 0.37.0

I was trying to get a speedup in my code where I use numpy.unique() to find unique elements in an n-dimensional array. If I use this:

x = np.unique(arr)

this works fine and returns the expected results. But if I do this:

@jit(nogil=True, nopython=True)
def find_unique(arr):
    return np.unique(arr)

x = find_unique(arr)

This throws an error with the following message:

UntypedAttributeError: Failed at nopython (nopython frontend)
Unknown attribute 'unique' of type Module(<module 'numpy' from '/opt/conda/lib/python3.6/site-packages/numpy/__init__.py'>)
File "<ipython-input-15-bd84a508a624>", line 5
[1] During: typing of get attribute at <ipython-input-15-bd84a508a624> (5)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
stuartarchibaldcommented, Apr 9, 2018

Thanks for the report. You are seeing that message because Numba does not yet support numpy.unique. Functions that are supported are listed here. I’m editing the title and making this a feature request. Thanks.

1reaction
Rik-de-Kortcommented, Apr 18, 2018

I’m not sure how to work with keyword arguments using the highlevel API (I’d have to spend some time on it), but since it’s not going to make it into numba until at least 0.39, here’s code for an overloaded implementation which returns the counts as well.

@overload(np.unique)
def np_unique(a):
        def np_unique_impl(a):
            b = np.sort(a.flatten())
            unique = list(b[:1])
            counts = [1 for _ in unique]
            for x in b[1:]:
                if x != unique[-1]:
                    unique.append(x)
                    counts.append(1)
                else:
                    counts[-1] += 1
            return unique, counts
    return np_unique_impl
Read more comments on GitHub >

github_iconTop Results From Across the Web

Feature request numpy.unique axis=0 · Issue #7663 - GitHub
Feature request I want to go from a = np.array([[1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0], ... line...
Read more >
numpy.unique — NumPy v1.24 Manual
Returns the sorted unique elements of an array. There are three optional outputs in addition to the unique elements: the indices of the...
Read more >
Numpy unique 2D sub-array [duplicate] - Stack Overflow
This is a new feature in the upcoming 1.13, as np.unique(a, axis=0) . You could simply copy the new implementation and use it...
Read more >
Find unique values in a numpy array with frequency & indices
In this article we will discuss how to find unique values / rows / columns in a 1D & 2D Numpy array. Also...
Read more >
The Ultimate Beginner's Guide to NumPy | by Anne Bonner
Feel free to take a look at the pull request for the absolute ... To get the indices of unique values in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found