-
Notifications
You must be signed in to change notification settings - Fork 9
Array elements are not hashable #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Not sure about a reason, I'd guess it's because data-apis/array-api#582 did not arrive at an actionable conclusion. For dtypes specifically, a workaround is to use Your example though: are you trying to hash arrays themselves? this does not sound healthy since arrays are mutable. |
Hi @ev-br
Actually, this code is not trying to hash arrays, only the values. This is existing code from confusion_matrix and it works fine with numpy. Not sure why Python raises
|
Changing a dict into a list:
so your code tries to hash 0D arrays, and that fails. It does not fail with numpy only because indexing returns "array scalars", and those do not exist anywhere else. For instance, your snippet fails on CuPy just the same:
So I suppose the solution is to call
|
And if it feels that All that said, your OP on being able to actually use dtypes as dictionary keys makes total sense to me, so is something to consider evolving the standard for. |
Hi @ev-br, thanks for your timely response and your effort. So, I understand that indexing an labels= array_api_strict.asarray([4, 5, 6])
print(labels[0])
print(type(labels[0]))
... even if we're only indexing a single element. I didn't expect that, but I am now a bit clearer on this, thank you. |
To circle back to the question in the title: can we use dtypes as dict keys? This was discussed at length in the Array API meeting earlier today, and the conclusion is it's surprisingly hard, and it's not going to happen in 2024 at least. (see the meeting notes at https://hackmd.io/zn5bvdZTQIeJmb3RW1B-8g So pragmatically, as of 12.12.2024 there are two options:
and use these typecodes (the values of the |
As @betatim pointed out, we talked about hashing If you want another way to do it, you would have to invent some completely new API. NumPy will (often) return scalars and provides APIs to ensure you get scalars, but there are no scalars here and arrays are mutable so they cannot be hashed. If anything, I think this issue may be asking for a In hindsight, I have to say that I am not sure how much that part even matters. Do you really write |
Yes, the title and the actual issue here are different :-). Hashing arrays is clearly out of the question (says he who was once storing |
Yeah, but do you build one for each |
Exactly. Which is why in the meeting I was suggesting to pivot to just mandating the existence of |
I've changed the title to match the top comment. It was too confusing for my little brain :D If we want to discuss hashing the dtype, maybe lets make a new issue? |
Actually, I should have checked about dtypes themselves: they are hashable today.
So I guess we can close this one since there's hardly a chance that arrays themselves will get hashable. |
To summarise the original issue as well: An array-api-strict array isn't hashable because it is mutable. Like a
The way to make it work is to explicitly convert the |
Sorry for the confusion. I had expected to have an element returned when indexing a single element of an array_api_strict arrray, like in numpy; now I see that returning a 0dim array instead even makes more sense. And then from your discussion it also seems that there's a difference between an element and a dtype, which I had thought were the same all along. My problem is resolved for over a week anyways. Thanks everyone for your help. |
In scikit-learn we use array values as dict keys, but trying this with
array_api_strict
array dtypes fails:TypeError: unhashable type: 'Array'
Since the dtypes are all numbers or boolean, they are immutable and - I was thinking - also hashable? For numpy and torch arrays they are.
Is this a bug or on purpose for some reason?
The text was updated successfully, but these errors were encountered: