Skip to content

BUG: np.unique_inverse output has wrong shape #25552

@jakevdp

Description

@jakevdp

Describe the issue:

According to the unique_inverse specification, the inverse_indices output of np.unique_inverse should have the same shape as the input:

second element must have the field name inverse_indices and must be an array containing the indices of values that reconstruct x. The array must have the same shape as x and have the default array index data type.

In NumPy's current implementation, inverse_indices is a flattened array.

A similar issue exists for numpy.unique_all.

Reproduce the code example:

import numpy as np
x = np.ones((2, 3))
out = np.unique_inverse(x)
assert out.inverse_indices.shape == x.shape, f"{out.inverse_indices.shape=} {x.shape=}"

Error message:

AssertionError: out.inverse_indices.shape=(6,) x.shape=(2, 3)

Python and NumPy Versions:

>>> import sys, numpy; print(numpy.__version__); print(sys.version)
2.0.0.dev0+git20240106.42fef42
3.9.6 (default, May  7 2023, 23:32:44) 
[Clang 14.0.3 (clang-1403.0.22.14.1)]

Runtime Environment:

>>> import numpy; print(numpy.show_runtime())
[{'numpy_version': '2.0.0.dev0+git20240106.42fef42',
  'python': '3.9.6 (default, May  7 2023, 23:32:44) \n'
            '[Clang 14.0.3 (clang-1403.0.22.14.1)]',
  'uname': uname_result(system='Darwin', node='jmdg-macbookpro.roam.internal', release='23.2.0', version='Darwin Kernel Version 23.2.0: Wed Nov 15 21:53:18 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6000', machine='arm64')},
 {'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
                      'found': ['ASIMDHP'],
                      'not_found': ['ASIMDFHM']}}]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions