How to resolve the typeerror: unhashable type: 'numpy.ndarray'?

561    Asked by DavidPiper in Data Science , Asked on Feb 14, 2023

 I'm trying to do a majority voting of the predictions of two deep learning models.The shape of both y_pred and vgg16_y_pred are (200,1) and type 'int64'.

max_voting_pred = np.array([])
for i in range(0,len(X_test)):
    max_voting_pred = np.append(max_voting_pred, statistics.mode([y_pred[i], vgg16_y_pred[i]])) 
I run into the following error:

TypeError: unhashable type: 'numpy.ndarray' How should I pass the data?


Answered by Diya tomar

To resolve the typeerror: unhashable type: 'numpy.ndarray', you must understand the problem.


The problem is that you're passing a list of numpy arrays to the mode function. It requires either a single list of values, or a single numpy array with values (basically any single container will do, but seemingly not a list of arrays). This is because it must make a hashmap of some kind in order to determine the most common occurrences, hence the mode. It is unable to hash a list of arrays. One solution would be to simply index the value out of each array (which then means mode gets a list of integers). Just changing the main line to:

max_voting_pred = np.append(max_voting_pred, mode([a[i][0], b[i][0]]))
Let me know if that doesn't fix things.

If you want something that is perhaps easier than fixing your original code, try using the mode function from the module: scipy.stats.mode. This version allows you to pass the whole array and simply specify an axis along which to compute the mode. Given you have the full vectors of predictions from both models:

Combine both arrays to be the two columns of one single (200, 2) matrix
results = np.concatenate((y_pred, vgg16_y_pred), axis=1)
Now you can perform the mode on that matrix across the single rows, but all in one single operation (no need for a loop):
max_votes = scipy.stats.mode(results, axis=1)
The results contain two things.
the mode values for each row
the counts of that mode within that row.
So to get the results you want (that would match your original max_voters_pred, you must take the first element from max_votes:
max_voters_pred = max_votes[0]

Your Answer

Interviews

Parent Categories