OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

TypeError: unhashable type: 'numpy.ndarray' in array to set

  • Thread starter Thread starter Erfan Hamidi
  • Start date Start date
E

Erfan Hamidi

Guest
in this def i get TypeError: unhashable type: 'numpy.ndarray' in line retrieved_indices_set = set(retrieved_indices)

Code:
def evaluate_retrieval(query_idx, retrieved_indices, relevant_indices):
    # Convert each list element to a tuple
    # Flatten the two-layer list and convert elements to tuples
    arr = np.array(retrieved_indices) #retrieved_indices => 128 * 128 * 3

# Transpose the array and convert it to a list of tuples
    retrieved_indices = tuple(list(map(tuple, np.vstack(arr.T))))
    print(type(retrieved_indices))

# Create a set from the tuples
    retrieved_indices_set = set(retrieved_indices)
    relevant_retrieved = len(retrieved_indices_set.intersection(relevant_indices_set))
    precision = relevant_retrieved / len(retrieved_indices_set) if len(retrieved_indices_set) > 0 else 0
    return precision

I try this but didn't work
retrieved_indices_tuples = tuple(tuple(tuple(pixel) for pixel in row) for row in retrieved_indices)
<p>in this def i get TypeError: unhashable type: 'numpy.ndarray' in line retrieved_indices_set = set(retrieved_indices)</p>
<pre class="lang-py prettyprint-override"><code>def evaluate_retrieval(query_idx, retrieved_indices, relevant_indices):
# Convert each list element to a tuple
# Flatten the two-layer list and convert elements to tuples
arr = np.array(retrieved_indices) #retrieved_indices => 128 * 128 * 3

# Transpose the array and convert it to a list of tuples
retrieved_indices = tuple(list(map(tuple, np.vstack(arr.T))))
print(type(retrieved_indices))

# Create a set from the tuples
retrieved_indices_set = set(retrieved_indices)
relevant_retrieved = len(retrieved_indices_set.intersection(relevant_indices_set))
precision = relevant_retrieved / len(retrieved_indices_set) if len(retrieved_indices_set) > 0 else 0
return precision

I try this but didn't work
retrieved_indices_tuples = tuple(tuple(tuple(pixel) for pixel in row) for row in retrieved_indices)
</code></pre>
 
Top