Did you encounter Typeerror: unhashable type: ‘numpy.ndarray’?
Well, errors in programming are common, you may run into errors no matter what language or project you’re working on.
Hence in this guide, we will determine why this error occurs and also provide solutions to remove the error quickly.
But before we resolve the error let’s understand first what is Python hashable…
What is Python hashable?
Python Hashable object is an object which does not change once defined. It can be represented using the hash function as a unique hash value.
Apparently, even if it’s obvious the hashable object does not really mean that the object is immutable.
Though it means that every object in Python is hashable, however not all hashable objects are immutable.
What is Typeerror: unhashable type: ‘numpy.ndarray’?
The TypeError: unhashable type: ‘numpy.ndarray’ Python error is raised when we use a NumPy array as a key in a dictionary or as an element in a set.
Wherein the dictionary key or element of a set must be hashable.
When the typeerror: unhashable type: numpy.ndarray occurs
TypeError: unhashable type: ‘numpy.ndarray’ occurs when:
- Converting multi-dimensional ndarray object to a set of objects.
- Assigning a ndarray as a dictionary key.
- Adding ndarray object to a set.
How this error typeerror unhashable type ‘numpy.ndarray’ happen?
Meanwhile, we can check if the object is hashable or not using the hash() function.
Technically, we will know if the object is hashable when the hash() function returns a number.
Let’s assume we have a string and we will run with the hash() function.
Let’s see what happens:
sample = "ITSOURCECODE!"
print(hash(sample))
Output:
8878721391279815830
So when we run the function on a string object it returns a number that indicates it is a hashable object.
On the other hand, this time we will run the object with hash() function on a ndarray object.
Let’s see what happens.
import numpy as np
arr = np.array([1,2,3,4])
print(hash(arr))
Output:
Traceback (most recent call last): File "C:\Users\Windows\PycharmProjects\pythonProject\main.py", line 4, in <module> print(hash(arr)) TypeError: unhashable type: 'numpy.ndarray'
As we can see, it throws an error because ndarray is not hashable.
How to fix Typeerror: unhashable type: ‘numpy.ndarray’
Now, let’s discuss each of these cases and solutions to fix the error.
Solution 1: Access the element data correctly
When we convert a multi-dimensional array this will cause an error.
Here is the example code:
import numpy as np
arr = np.array([[11,12,13,14]])
print(set(arr))
Output:
Traceback (most recent call last): File "C:\Users\Windows\PycharmProjects\pythonProject\main.py", line 4, in <module> print(set(arr)) TypeError: unhashable type: 'numpy.ndarray'
When we run the code we see an error, It is because the Python interpreter wanted to see if the elements of the array are hashable.
Additionally, the element is detected as a ndarray object which is why the error shows up.
To resolve this error, we need to access the element data correctly. In this case, we can solve the error by specifying the set(arr[0]).
Examine the following code.
import numpy as np
arr = np.array([[11,12,13,14]])
print(set(arr[0]))
Output:
{11, 12, 13, 14}
Solution 2: Index the inner element rightly
Since only hashable objects can be added as a dictionary. This will give an error if the unhashable object is added as a dictionary key.
import numpy as np
arr = np.array([[11],[22],[23],[24]])
sample = dict()
# Adding the first element from the array as a dictionary key
sample[arr[0]] = "ITSOURCECODE!"
If we run this code this will, throw an error.
As we can see an error occurs because arr[0] is [1], which is a ndarray object (unhashable type).
Traceback (most recent call last): File "C:\Users\Windows\PycharmProjects\pythonProject\main.py", line 7, in <module> sample[arr[0]] = "ITSOURCECODE!" TypeError: unhashable type: 'numpy.ndarray'
To fix this, we need to index the inner element rightly as indicated below:
import numpy as np
arr = np.array([[11],[12],[13],[14]])
sample = dict()
# Adding the first element from the array as a dictionary key
sample[arr[0,0]] = "ITSOURCECODE!"
print(sample)
Output:
{11: 'ITSOURCECODE!'}
Solution 3: The elements of the array instead of the array object
When we want to add all the elements of the array to a set. Then, we can see an error instead of its elements in the set() function.
import numpy as np
arr = np.array([11,12,13,14])
sample = set()
sample.add(arr)
Output:
Traceback (most recent call last): File "C:\Users\Windows\PycharmProjects\pythonProject\main.py", line 5, in <module> sample.add(arr) TypeError: unhashable type: 'numpy.ndarray'
To fix this, add the elements of the array instead of the array object, as shown below:
import numpy as np
arr = np.array([11,12,13,14])
sample = set()
for ele in arr:
sample.add(ele)
print(sample)
Output:
{11, 12, 13, 14}
Conclusion
In conclusion, Typeerror: unhashable type: ‘numpy.ndarray’ is raised when converting multi-dimensional ndarray object to a set of objects, assigning a ndarray as a dictionary key, and adding ndarray object to a set.
We hope that this guide has helped you resolve this error and get back to coding.
If you are finding solutions to some errors you might encounter we also have Typeerror: nonetype object is not callable.
Thank you for reading!