implementing np.unique returning incorrect counts for structured arrays in NumPy 1.25
This might be a silly question, but I'm testing a new approach and I'm having trouble with `np.unique` when using it on a structured array in NumPy 1.25....... I expected it to return unique rows based on specific fields, but the counts for the unique entries seem to be incorrect. Hereโs a simple example of what Iโm doing: ```python import numpy as np data = np.array([ (1, 'apple'), (1, 'apple'), (2, 'banana'), (2, 'banana'), (1, 'apple') ], dtype=[('id', 'i4'), ('fruit', 'U10')]) unique_fruits, counts = np.unique(data, return_counts=True) print("Unique Fruits:", unique_fruits) print("Counts:", counts) ``` I expected to get 2 unique entries: one for `(1, 'apple')` and one for `(2, 'banana')`, with counts corresponding to their occurrences. However, when I run this code, I get: ``` Unique Fruits: [(1, 'apple') (2, 'banana')] Counts: [3 2] ``` Clearly, the count for `(1, 'apple')` is returning 3 instead of 2, and for `(2, 'banana')`, itโs returning 2 instead of 1. I've double-checked that the entries are indeed identical. Iโve also tried using `return_index=True` to see the positions of the unique entries, and they seem correct, but the counts are throwing me off. Is there a particular way I should be using `np.unique` with structured arrays, or am I missing something? Any insights would be appreciated! Am I missing something obvious? How would you solve this? For context: I'm using Python on Windows 10. The stack includes Python and several other technologies. Am I missing something obvious? What would be the recommended way to handle this?