Home > Blockchain >  How do you iterate through two list and count the true positive
How do you iterate through two list and count the true positive

Time:01-18

I'm trying to iterate through a list of predictions and ground truths and count the true positives.

This is the solution I came up with:

    tp = 0
    for p, g in zip(predicted, ground_truth):
        if p and g == True: 
           tp  = 1
        return tp

I am getting an error message saying: SyntaxError: 'return' outside function. But the return is inside the function.

CodePudding user response:

Try this:

def count_true(predicted, ground_truth):
    tp = 0
    for p, g in zip(predicted, ground_truth):
        if p and g == True: 
           tp  = 1
    return tp

count= count_true([True, False, True], [True, True, False])
print(count)

This should return: 1

You need to declare a function with def

CodePudding user response:

def count_true(predicted, ground_truth):
   tp = 0
    for p, g in zip(predicted, ground_truth):
        if p and g == True: 
           tp  = 1
        return tp

You need to define a function, def count_true(p, g)

CodePudding user response:

You can do:

def count_true(predicted, ground_truth):
    return sum(1 if p and g == True else 0 for p, g in zip(predicted, ground_truth))

count= count_true([True, False, True], [True, True, False])
print(count)
#1
  • Related