I've got an ndarray
myndarray and a set
myset. I can do this:
myndarray[myndarray[:,:,-1] == some_value] = 0
But this returns "unhashable type: 'numpy.ndarray'
"
myndarray[myndarray[:,:,-1] in some_set] = 0
What is it I want to do here?
I've got an ndarray
myndarray and a set
myset. I can do this:
myndarray[myndarray[:,:,-1] == some_value] = 0
But this returns "unhashable type: 'numpy.ndarray'
"
myndarray[myndarray[:,:,-1] in some_set] = 0
What is it I want to do here?
The fact that myndarray[myndarray[:,:,-1] == some_value] = 0
works is itself a bit magical — it's because ndarray has its own definition for ==
, which doesn't mean "check if I'm equal to this other object and return a single boolean" as one might expect for other Python objects; it means "compare each of my elements to each of this other object's elements, and return an array with a boolean in each position saying whether each element is equal".
Alas, there's no such magic for in
, which produced the error message you saw: Python took the entire array as a single value and tried to see if the whole thing appeared in the set, which failed because Python sets can only contain hashable things. But, you can be pretty hopeful that numpy has a built-in function that does this. I googled for "numpy set membership" and it looks like the function you want is numpy.isin, which the documentation suggests is used as follows:
import numpy as np
myndarray = # ...
myset = # ...
myndarray[np.isin(myndarray[:,:,-1], list(myset))] = 0
There are other similar questions on SO, e.g. test for membership in a 2d numpy array, but it looks like isin
postdates those questions.