mar
mar

Reputation: 333

How to compare two arrays in python, and get the boolean output based on a significant value

I am doing classification. I have two arrays, the first is 'Actual', and the second is 'Predicted'. I want to compare these two arrays. Suppose the first array is:

Actual = [1, 1, 2, 3, 1]

this tells us that the the first, second, and the last indexes are corresponding to class 1.

The 'Predicted' array is:

Predicted = [1, 1, 1, 3, 3]

this tells us that the first and second indexes have been predicted accurately.

If you see the predicted array, the first and second arrays have been accurately predicted, but the fifth value of label 1 has not been accurately predicted. I want the output the tells us just those indexes that accurately predicted as 1, and those not. like this:

output = [True, True, False]

by this output, we know that two labels of 1 have been predicted accurately, and one label has not been accurately.

Update I want to evaluate just based on value 1. If you see, the forth predicted value is accurately predicted by 3, but I do not want that, because I want evaluate 1 value.

Upvotes: 2

Views: 4416

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 91009

If you just need to True and False values only, you can still use zip function and list comprehension as below -

>>> Actual = [1, 1, 2, 3, 1]
>>> Predicted = [1, 1, 1, 3, 3]
>>> output = [a == p for a,p in zip(Actual,Predicted) if p==1]
>>> output
[True, True, False]

Explanation - This would take all the elements where Predicted has value 1. And then the output list would contain whether those values are same in Actual or not.

Please note - zip would only take elements till the shortest list. But I am guessing that is not an issue in your case.

Upvotes: 2

Related Questions