+2 votes
in Programming Languages by (16.4k 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

0 votes
by (28.1k points)

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.