user1999109
user1999109

Reputation: 441

How to calculate modulus value for a set of values in python?

In one material I found one formula to calculate Precision as below

enter image description here

Here a and b are set of values. After many search in internet I found that modulus means remainder value or absolute value. Here I take modulus as absolute value and my python code for the above formula is as below

import numpy as np

def intersection(lst1, lst2):
    return list(set(lst1) & set(lst2))

a = [7,21]
b = [11, 7, 27, 21]

a_intersect_b=intersection(a,b)
print(" a_intersect_b : ",a_intersect_b)

mod_a_intersect_b=[abs(x) for x in a_intersect_b]
print("|a_intersect_b| : ",mod_a_intersect_b)

mod_a=[abs(x) for x in a]
print("|a| : ",mod_a)

numerator=np.array(mod_a_intersect_b, dtype=np.float)
denominator=np.array(mod_a, dtype=np.float)

print(" mod_a_intersect_b /  mod_a : ", numerator/denominator)

Here I get 2 output values. But in the material and in general the precision is a single value. If the list size increases then the output values also increases. Finally I found that I misunderstood the modulus meaning here. Guide me to get the single precision value as per the above formula. Thanks in advance.

Note: In the formula a and b are set of values. So I used list in my code. Also guide me if I use other option to mention set of values in python then I can get single precision value.

Upvotes: 0

Views: 878

Answers (1)

Mikhail Stepanov
Mikhail Stepanov

Reputation: 3790

As @Hoog mentioned in gis comment, the modulus operation in the case of precision means a cardinality of some set (just a number of elements of the set), so you can define a precision as the following:

def precision(a, b):
    """
    a: set, relevant items
    b: set, retrieved items
    returns: float, precision value
    """
    return len(a & b) / len(a)

len(a) returns nuber of elements of the set, i.e. cardinality, |a| operation.

If a, b is lists, just wrap them in sets first:

def precision(a, b):
    """
    a: set, relevant items
    b: set, retrieved items
    returns: float, precision value
    """
    a, b = set(a), set(b)
    return len(a & b) / len(a)

Also, in data science and related areas precision is a metric which calculates ratio 'true positives' / ('true positives' + 'false positives'). It's the same thing described in other terms - but standart implementations of precision won't help you.

Upvotes: 2

Related Questions