TypeError: unhashable type: 'numpy.ndarray'
时间: 2023-11-11 22:01:52 浏览: 49
将numpy.ndarray格式图像转化为_io.BufferedReader格式
This error message occurs when a numpy array is used as a key in a dictionary or as an element in a set. Numpy arrays are mutable objects and therefore cannot be hashed, which is required for use as a key or element in a hash table.
To fix this error, you can convert the numpy array to a hashable type such as a tuple or a string. For example, if you have a numpy array called `arr`, you can convert it to a tuple using the `tuple` function:
```
arr = np.array([1, 2, 3])
key = tuple(arr)
```
Alternatively, you can convert the numpy array to a string using the `tostring` method:
```
arr = np.array([1, 2, 3])
key = arr.tostring()
```
You can then use the resulting tuple or string as a key or element in your dictionary or set.
阅读全文