+3 votes
in Programming Languages by (73.8k points)

The following line of code is giving the error: TypeError: unhashable type: 'numpy.ndarray'.

pids_1 = set([pid_tr_orig[y_tr_orig == 1]])

Variables pid_tr_orig and y_tr_orig are 1D Numpy arrays. How can I fix the error?

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

Since pid_tr_orig and y_tr_orig are Numpy arrays, the code pid_tr_orig[y_tr_orig == 1] will return a Numpy array. So, you do not have to add [] with set().

Change

pids_1 = set([pid_tr_orig[y_tr_orig == 1]])

to

pids_1 = set(pid_tr_orig[y_tr_orig == 1])

It will fix the error.


...