Skip to content

BUG: np.unique with return_inverse and axis specification yields a wrong shape #26738

@nabenabe0928

Description

@nabenabe0928

Describe the issue:

In NumPy v2.0.0, when I specify return_inverse=True and axis for a 2d array, np.unique will return a wrongly deep-nested array for the inverse.

Reproduce the code example:

import numpy as np


a = np.array([[3, 1], [1, 2], [3, 1]])
u, inv = np.unique(a, return_inverse=True)
a1 = u[inv]
assert np.all(a1 == a)  # True

u, inv = np.unique(a, return_inverse=True, axis=0)
a2 = u[inv.flatten()]
a3 = u[inv]
assert np.all(a2 == a)  # True
assert np.all(a3 == a)  # False

Error message:

No response

Python and NumPy Versions:

  • NumPy: 2.0.0
  • Python: 3.9.13 (main, Aug 25 2022, 23:26:10)
    [GCC 11.2.0]

Runtime Environment:

No response

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions