+3 votes
in Programming Languages by (75.0k 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 (353k 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().


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


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

It will fix the error.