![]() |
The Python library used for working with arrays is known as NumPy. Python provides various exceptions, including TypeError, which occurs when a specific operation is performed on an unsupported object type. One common TypeError encountered by users is ‘TypeError: Unhashable Type: ‘numpy.ndarray’.’ In this article, we will discuss how to resolve this error. What is Typeerror: Unhashable Type: ‘Numpy.Ndarray’?Whenever the user tries to use the NumPy array as a mutable object, whether it is a dictionary or a set, then we encounter the error Typeerror: Unhashable Type: ‘Numpy.Ndarray’. It is because the value of the mutable object can be transformed later, thus they cannot be hashed. Hence, there is a need to get rid of this Typeerror. The Typeerror: Unhashable Type: ‘Numpy.Ndaaray’ usually occurs in two circumstances:
What is hashability in Python?The property of an object that allows the user to use any datatype as a key in a hash table is known as hashability. It is the mostly commonly used when handling dictionaries and sets in Python. The hashable objects are mutable, i.e., there value can be modified after creation, while the non-hashable objects are immutable, whose value cannot be modified after creation. Examples of hashable objects:Integers, double, string, tuple, etc. falls under the category of hashable objects. Integers: int_value=100 Double: double_value=100.00 String: string_text="Geeks For Geeks" Tuple: tuple_value = (100, 200, 300) Examples of non-hashable objects:List, dictionary, set, etc. falls under the category of non hashable objects. List: list_value=[100, 200, 300] Set: set_value={100, 200, 300} Dictionary: dictionary_value={'a': 100, 'b': 200} How to fix the error?Using NumPy ndarray in Sets1. Example of adding a NumPy ndarray to a setIn this example, we have created a Numpy ndarray with values 50,60, 70 and a set. Further, we have added that Numpy array to the set. This gives us the Typerror. Python3
2. Fixing the error by converting the ndarray to a tupleA built in immutable datatype that is used to store sequence of values is known as tuple. In this method, we will see the way to fix the Typeerror: Unhashable Type: ‘Numpy.Ndarray‘ by using a tuple.
Example:In this example, we have created a NumPy array and then we have converted that array to a tuple to fix the Typeerror: Unhashable Type: ‘Numpy.Ndarray’. Python3
Output:{(500, 600, 700): 'Value'} Using NumPy ndarray in Dictionaries1. Example of using NumPy ndarray as a key in a dictionaryIn this example, we have defined a numpy ndarray with values 50,60 and 70 and a dictionary. Further, we have used numpy ndarray as a key in the dictionary. Python3
2. Fixing the error by using Numpy ndarray as a dictionary valueThe Typeerror occurs when the element is used a key in the dictionary. Thus, in this method, we will fix the Typeerror: Unhashable Type: ‘Numpy.Ndarray’ by using it as a dictionary value.
Example:In this example, we have created a numpy array and then used it a dictionary value to fix the Typeerror: Unhashable Type: ‘Numpy.Ndarray’. Python3
Output:{'key': array([500, 600, 700])} |
Reffered: https://www.geeksforgeeks.org
Python Programs |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |