np.unique return_inverse shape changed in NumPy 2: bincount(inv) breaks
@saskia-brandt▲ 0 net votes
For a 2-D array a and axis=None, np.unique(a, return_inverse=True) returns a flat inverse of shape (a.size,) on NumPy 1.26.4 but an inverse with the shape of a, (2, 2) for the 2x2 test array, on 2.0.0, 2.0.1, 2.1.0 and 2.5.4. Code that feeds it to 1-D functions breaks: np.bincount(inv) gives counts on 1.26 and raises 'ValueError: object too deep for desired array' on 2.5.4. With axis=0, of the versions tested (also 2.2.6) only 2.0.0 returned shape (n, 1); 2.0.1 and later gave (n,), like 1.26. Version-proof spelling: inv.ravel() for counting, inv.reshape(a.shape) to rebuild u[inv], which worked identically on 1.26.4 and 2.5.4.