Last active
June 4, 2017 21:14
-
-
Save JohnLonginotto/bd1a846544b31ebe6f96190c1cd84a83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import argparse | |
parser = argparse.ArgumentParser( description="https://www.youtube.com/watch?v=7S94ohyErSw&t=90") | |
parser.add_argument("-t", "--tests", default=50, type=int, help='The number of tests to average') | |
parser.add_argument("-tp", "--true_positives", required=True, type=int, help='The actual number of True Positives') | |
parser.add_argument("-tn", "--true_negatives", required=True, type=int, help='The actual number of True Negatives (Total things - True Positives)') | |
parser.add_argument("-tpa", "--true_positives_A", required=True, type=int, help='True Positives tool A detects') | |
parser.add_argument("-fpa", "--false_positives_A", required=True, type=int, help='False Positives tool A detects') | |
parser.add_argument("-tpb", "--true_positives_B", required=True, type=int, help='True Positives tool B detects') | |
parser.add_argument("-fpb", "--false_positives_B", required=True, type=int, help='False Positives tool B detects') | |
args = parser.parse_args() | |
def rando(things): | |
things_copy = list(things) | |
random.shuffle(things_copy) | |
return things_copy | |
def mean(list_of_lists): | |
result = [] | |
for y in range(len(list_of_lists[0])): | |
result.append( str( sum([x[y] for x in list_of_lists])/float(len(list_of_lists)) )[:6].ljust(6) ) | |
return ' '.join(result).rjust(40) | |
def do_tests(tests,true_positives,true_negatives,true_positives_A,false_positives_A,true_positives_B,false_positives_B): | |
A_results = [] | |
B_results = [] | |
union_results = [] | |
intersect_results = [] | |
for test in range(tests): | |
possible = set(range(true_negatives+true_positives)) | |
reality_positive = set(rando(possible)[:true_positives]) | |
reality_negative = possible-reality_positive | |
A_true_positive = set(rando(reality_positive)[:true_positives_A]) | |
A_false_positive = set(rando(reality_negative)[:false_positives_A]) | |
A_false_negative = reality_positive-A_true_positive | |
A_true_negative = reality_negative - A_false_positive | |
A_results.append([len(A_true_positive),len(A_true_negative),len(A_false_positive),len(A_false_negative)]) | |
B_true_positive = set(rando(reality_positive)[:true_positives_B]) | |
B_false_positive = set(rando(reality_negative)[:false_positives_B]) | |
B_false_negative = reality_positive-B_true_positive | |
B_true_negative = reality_negative - B_false_positive | |
B_results.append([len(B_true_positive),len(B_true_negative),len(B_false_positive),len(B_false_negative)]) | |
intersect_true_positive = len(A_true_positive.intersection(B_true_positive)) | |
intersect_false_positive = len(A_false_positive.intersection(B_false_positive)) | |
intersect_false_negative = len(A_false_negative.intersection(B_false_negative)) | |
intersect_true_negative = len(A_true_negative.intersection(B_true_negative)) | |
intersect_results.append([intersect_true_positive,intersect_true_negative,intersect_false_positive,intersect_false_negative]) | |
union_true_positive = len(A_true_positive.union(B_true_positive)) | |
union_false_positive = len(A_false_positive.union(B_false_positive)) | |
union_false_negative = len(A_false_negative.union(B_false_negative)) | |
union_true_negative = len(A_true_negative.union(B_true_negative)) | |
union_results.append([union_true_positive,union_true_negative,union_false_positive,union_false_negative]) | |
print mean(A_results),mean(B_results),mean(intersect_results),mean(union_results) | |
print ' Tool A Tool B Intersect Union' | |
print ' TP TN FP FN TP TN FP FN TP TN FP FN TP TN FP FN' | |
for _ in range(5): | |
do_tests(tests=args.tests,true_positives=args.true_positives,true_negatives=args.true_negatives,true_positives_A=args.true_positives_A,false_positives_A=args.false_positives_A,true_positives_B=args.true_positives_B,false_positives_B=args.false_positives_B) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment