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

0 votes
by (29.3k 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().

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.

...