Home > Enterprise >  how to compare two boolean lists
how to compare two boolean lists

Time:09-30

For Python: I need to write two functions that satisfy this question

Problem 5. Suppose 10 patients numbered 0 through 9 are participating in a trial of a new Covid Test. Through other means, it is known whether each patient actually has covid. This information is recorded in a list called HasCovid of 10 elements. HasCovid = [True, True, True, False, False, True, False, True, False, False]. Once the new test is given to each of patients you know which of them has tested positive. This is indicated by a list of test results called TestsPositive = [True, False, True, False, True, True, False,True,False, True] A patient is a True Positive if they have tested positive and they do indeed have Covid. A patient is a False Positive if they test positive but are known not to have covid. Write a function num_TP which counts the number of true positives and another called num_FP which counts the number of false positives. This is a specific example of 10 patients. Your code should work for any such pair of Boolean lists, both with 10 elements.

CodePudding user response:

I'm going to answer this presuming you have the data available in a Dataframe already.

It would look like this:

>>> df
   TestsPositive IsPositive
0     True    False
1     True    True
...etc

You can utilise pandas summary techniques

>>> ((df['TestsPositive'] == True) & (df['IsPositive'] == True)).sum()
1

CodePudding user response:

Another option here, you create a function that passes in both lists, and uses the index of each list to reference each other.

The key here is to use the enumerate function to create the index number

def false_positive(test_result, actual_status):
    false_positives = 0
    for i,test in enumerate(test_result):
        # count false positives
        if test == True:
            if test != actual_status[i]:
                false_positives  = 1
    return false_positives

I think that's more along the lines you're looking for.

  • Related