2

I tried to find recall but type error occur

import pandas as pd
y_test = {'o1':  [0,1,0,1],'o2': [1,1,0,1],'o3':[0,0,1,1]}
y_test = pd.DataFrame (y_test)
y_pred = {'o1':  [1,1,0,1],'o2': [1,0,0,1],'o3':[1,0,1,1]}
y_pred = pd.DataFrame (y_pred)
y_pred = y_pred.to_numpy()


def precision(y_test, y_pred):
    i = set(y_test).intersection(y_pred)
    len1 = len(y_pred)
    if len1 == 0:
        return 0
    else:
        return len(i) / len1

print("recall of Binary Relevance Classifier: " + str(precision(y_test, y_pred)))

This code shown an error: actually i try to find recall for multi label classification error details iven below

TypeError                                 Traceback (most recent call last)
<ipython-input-41-8f3ca706a8e6> in <module>
16         return len(i) / len1
17 
---> 18 print("recall of Binary Relevance Classifier: " + str(precision(y_test, y_pred)))
<ipython-input-41-8f3ca706a8e6> in precision(y_test, y_pred)
 9 
10 def precision(y_test, y_pred):
---> 11     i = set(y_test).intersection(y_pred)
 12     len1 = len(y_pred)
 13     if len1 == 0:

TypeError: unhashable type: 'numpy.ndarray'
1
  • 2
    Please add the full error traceback to your question. Commented Aug 19, 2020 at 12:47

1 Answer 1

6

Your numpy array y_test cannot be converted to a set (on line 11), because the array is 2 dimensional.

For an iterable to be converted to a set, the items need to all be hashable. For a 1-d numpy array that's fine, because numbers are hashable:

>>> array_1d = np.array([1, 2, 3])
>>> array_1d
array([1, 2, 3])
>>> set(array_1d)
{1, 2, 3}

But for a 2-d array, you'll get this error because the nested arrays are not themselves hashable:

>>> array_2d = np.array([[1,2,3], [1,2,3], [1,2,3]])
>>> array_2d
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])
>>> set(array_2d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'numpy.ndarray'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.