The shape of your variable energy is probably wrong:
>>> from numpy import array
>>> set([1,2,3]) & set(range(2, 10))
set([2, 3])
>>> set(array([1,2,3])) & set(range(2,10))
set([2, 3])
>>> set(array([[1,2,3],])) & set(range(2,10))
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: unhashable type: ‘numpy.ndarray’
And that’s what happens if you read columnar data using your approach
>>> data
array([
[1., 2., 3.],
[3., 4., 5.],
[5., 6., 7.],
[8., 9., 10.]
]) >>>
hsplit(data, 3)[0]
array([
[1.],
[3.],
[5.],
[8.]
])
Probably you can simply use
>>> data[: , 0]
array([1., 3., 5., 8.])
If we use the hash, we can check if an object is hashable or not. () function. If hash() returns a number, it indicates that the object is hashable.,Understanding the root cause of TypeError: unhashable type: ‘numpy.ndarray’: ,We see TypeError: unhashable type: ‘numpy.ndarray’, in the following cases: This is a case where we see an error. The Python interpreter notices that the element is a ndarray object if it checks if elements of the array are hashable. There is an error because ndarray objects are not hashable.
If we have a string, let’s say we do. When we run the function on the object string we will see what happens.
s = “Finxter”
print(hash(s))
Output:
951412520483326359
When we run the function on the ndarray object, we will see what happens.
arr = np.array([1, 2, 3, 4])
print(hash(arr))
Now, let’s see what happens when we convert a multi-dimensional array.
import numpy as np
arr = np.array([
[1, 2, 3, 4]
])
print(set(arr))
This is shown in the following code snippet
import numpy as np
arr = np.array([
[1, 2, 3, 4]
])
print(set(arr[0]))
Consider the following example:
import numpy as np
arr = np.array([
[1],
[2],
[3],
[4]
])
a = dict()
# Adding the first element from the array as a dictionary key
a[arr[0]] = “Value”
As shown below, the inner element should be index to fix this.
import numpy as np
arr = np.array([
[1],
[2],
[3],
[4]
])
a = dict()
# Adding the first element from the array as a dictionary key
a[arr[0, 0]] = “Value”
print(a)
If there is an array and you want to add all the elements of it to a set, then this is what you should do.
import numpy as np
arr = np.array([1, 2, 3, 4])
a = set()
a.add(arr)
To fix this, add elements of the array instead of the array object:
import numpy as np
arr = np.array([1, 2, 3, 4])
a = set()
for ele in arr:
a.add(ele)
print(a)
When trying to get a hash of a NumPy ndarray, the errorTypeError: unhashable type is present. For example, if you want to use an ndarray as a key in a Python dictionary.
You have to use a data type that is readable by computers. The error is called TypeError: unhashable type.
‘numpy.ndarray’ occurs when trying to get the hash value of a NumPy ndarray. ,TypeError occurs whenever you try to perform an illegal operation for a specific data type object.
In the example, the illegal operation is hashing, and the data type is numpy.ndarray. What does type error mean?
import numpy as np
arr = np.array([1, 3, 5, 7])
print(set(arr))
import numpy as np
arr = np.array([1, 3, 5, 7])
print(set(arr))
{
1,
3,
5,
7
}
import numpy as np
arr = np.array([
[1, 3, 5, 7],
[1, 4, 5, 8]
])
print(set(arr))
The elements of the array are a ndarray object, and the objects in the array are not hashable:
print(type(arr[0]))
print(type(arr[1]))
print(type(arr[0]))
print(type(arr[1]))
≺
class ‘numpy.ndarray’≻≺
class ‘numpy.ndarray’≻
import numpy as np
arr = np.array([
[1, 3, 5, 7],
[1, 4, 5, 8]
])
a_set = set()
for i in arr:
a_set.update(set(i))
print(a_set)
In the above code, we use a for loop to iterate over the component array in the multi-dimensional array; we convert each array to a set and call the update method on a set object to get the values for all the array. To see the results, run the code.
{
1,
3,
4,
5,
7,
8
}
import numpy as np
arr = np.array([0])
a_dict = dict()
a_dict[arr] = “X”
print(a_dict)
In the above code, we attempt to use the one element in the numpy array as a key in the dictionary. To see the result, we need to run the code.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –
TypeError Traceback(most recent call last)
— –≻1 a_dict[arr] = “X”
TypeError: unhashable type: ‘numpy.ndarray’
import numpy as np
arr = np.array([0])
a_dict = dict()
a_dict[arr[0]] = “X”
print(a_dict)
Using the index operator, we can get elements of an array. Let’s use the code to get the result.
{
0: ‘X’
}
import numpy as np
arr = np.array([1, 3, 3, 5, 5, 7, 7])
a_set = set()
a_set.add(arr)
print(a_set)
import numpy as np
arr = np.array([1, 3, 3, 5, 5, 7, 7])
a_set = set()
a_set.add(arr)
print(a_set)
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –
TypeError Traceback(most recent call last)
5 a_set = set()
6
— –≻7 a_set.add(arr)
TypeError: unhashable type: ‘numpy.ndarray’
import numpy as np
arr = np.array([1, 3, 3, 5, 5, 7, 7])
a_set = set()
a_set.update(arr)
Let’s run the code to see the result:
{
1,
3,
5,
7
}
Unhashable type ‘Set’, Unhashable type ‘Dict’, Unhashable type ‘Slice’ Error in Python, Unhashable type ‘numpy.ndarray’ Error in Python.
We will start from a dictionary that contains one key:
>>> country = {
“name”: “UK”
} >>>
country {
‘name’: ‘UK’
}
Now add a second item to the dictionary:
>>> country[“capital”] = “London” >>>
country {
‘name’: ‘UK’,
‘capital’: ‘London’
}
This is what happens if we use another dictionary as a key:
>>> info = {“language”: “english”}
>>> country[info] = info[“language”]
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: unhashable type: ‘dict’
There is a similar error but this time it is for a numpy.ndarray. (N-dimensional array).
>>> import numpy as np
>>> x = np.array([[1, 2, 3], [4, 5, 6]])
>>> type(x)
<class ‘numpy.ndarray’>
Let’s find out what happens if we convert the array into a set:
>>> set(x)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: unhashable type: ‘numpy.ndarray’
We see the “unhashable type” error again, I want to confirm if once again we see the same behaviour when we try to apply the hash() function to our ndarray.
>>> hash(x)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: unhashable type: ‘numpy.ndarray’
The array we have defined before was bi-dimensional, now we will do the same test with a uni-dimensional array.
>>> y = np.array([1, 2, 3])
>>> y
array([1, 2, 3])
>>> type(y)
<class ‘numpy.ndarray’>
>>> set(y)
{1, 2, 3}
The first conversion to a set failed due to the fact that we were trying to create a set of NumPy array but it couldn’t be used as an element of a set because it was mutable.
>>> my_set = {np.array([1, 2, 3]), np.array([4, 5, 6])}
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: unhashable type: ‘numpy.ndarray’
For example, An array of strings should be able to be converted to a set without errors.
>>> z = np.array([‘one’, ‘two’, ‘three’])
>>> type(z)
<class ‘numpy.ndarray’>
>>> set(z)
{‘one’, ‘two’, ‘three’}
Let’s find out
>>> user = {
“name”: “John”,
“age”: 25,
“gender”: “male”
} >>>
user[1: 3]
Traceback(most recent call last):
File “”, line 1, in
user[1: 3]
TypeError: unhashable type: ‘slice’
Key-value pairs are used to make dictionaries and they allow access to any value by simply using the associated dictionary key.
>>> user[“name”]
‘John’ >>>
user[“age”]
25
Let’s create a set of numbers:
>>> numbers = {1, 2, 3, 4}
>>> type(numbers)
<class ‘set’>
All good so far, but what happens if one of the elements in the set is a list?
>>> numbers = {1, 2, 3, 4, [5, 6]}
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: unhashable type: ‘list’
Let’s see if we can create a set that provides a tuple instead of a list as one of the items:
>>> numbers = {
1,
2,
3,
4,
(5, 6)
} >>>
numbers {
1,
2,
3,
4,
(5, 6)
}
First of all let’s define a list of sets:
>>> numbers = [{1, 2}, {3, 4}]
>>> numbers
[{1, 2}, {3, 4}]
>>> type(numbers[0])
<class ‘set’>
Start by creating an empty set:
>>> numbers = set()
>>> type(numbers)
<class ‘set’>
The set add method will be used to add a first item of type.
>>> item = {1,2}
>>> type(item)
<class ‘set’>
>>> numbers.add(item)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: unhashable type: ‘set’
Let’s Convert The Item Set To A Frozenset:
>>> item
{1, 2}
>>> type(item)
<class ‘set’>
>>> frozen_item = frozenset(item)
>>> type(frozen_item)
<class ‘frozenset’>
And now add the frozenset to the empty set we have defined before:
>>> numbers
set() >>>
numbers.add(frozen_item) >>>
numbers {
frozenset({
1,
2
})
}
Mutable data types are not hashable: list, set, dictionary.
>>> my_list = [] >>>
print(my_list.__hash__)
None
>>>
my_set = set() >>>
print(my_set.__hash__)
None
>>>
my_dict = {} >>>
print(my_dict.__hash__)
None
Immutable data types are hashable: string, integer, float, tuple, frozenset.
>>> my_string = ”
>>> print(my_string.__hash__)
<method-wrapper ‘__hash__’ of str object at 0x7ffc1805a2f0>
>>> my_integer = 1
>>> print(my_integer.__hash__)
<method-wrapper ‘__hash__’ of int object at 0x103255960>
>>> my_float = 3.4
>>> print(my_float.__hash__)
<method-wrapper ‘__hash__’ of float object at 0x7ffc0823b610>
>>> my_tuple = (1, 2)
>>> print(my_tuple.__hash__)
<method-wrapper ‘__hash__’ of tuple object at 0x7ffc08344940>
>>> my_frozenset = frozenset({1, 2})
>>> print(my_frozenset.__hash__)
<method-wrapper ‘__hash__’ of frozenset object at 0x7ffc0837a9e0>
Disclosure: Some of the links and banners on this page may be affiliate links, which can provide compensation to Codefather.tech at no extra cost to you.Codefather.tech is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means
for sites to earn advertising fees by linking to Amazon.Codefather.tech also participates in affiliate programs with DataCamp, CJ, ShareASale, Ezoic, and other sites.Our affiliate disclaimer is available here.