|
1
|
|
|
#!/usr/bin/env python2 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
""" |
|
4
|
|
|
Created on Thu Apr 26 11:39:03 2018 |
|
5
|
|
|
""" |
|
6
|
|
|
|
|
7
|
|
|
def compute_precision(true_positive, false_positive): |
|
8
|
|
|
""" Function to compute Precision""" |
|
9
|
|
|
if true_positive == 0: |
|
10
|
|
|
return 0 |
|
11
|
|
|
return float(true_positive) / float(true_positive + false_positive) |
|
12
|
|
|
|
|
13
|
|
|
def compute_recall(true_positive, false_negative): |
|
14
|
|
|
""" Function to compute Recall""" |
|
15
|
|
|
if true_positive == 0: |
|
16
|
|
|
return 0 |
|
17
|
|
|
return float(true_positive) / float(true_positive + false_negative) |
|
18
|
|
|
|
|
19
|
|
|
def compute_accuracy(true_positive, true_negative, false_positive, false_negative): |
|
20
|
|
|
""" Function to compute Accuracy""" |
|
21
|
|
|
if true_positive + true_negative == 0: |
|
22
|
|
|
return 0 |
|
23
|
|
|
return float(true_positive + true_negative) / \ |
|
24
|
|
|
float(true_positive + true_negative + false_positive + false_negative) |
|
25
|
|
|
|
|
26
|
|
|
def compute_f1score(precision, recall): |
|
27
|
|
|
""" Function to compute F1 Score""" |
|
28
|
|
|
if precision * recall == 0: |
|
29
|
|
|
return 0 |
|
30
|
|
|
return float(2 * precision * recall) / float(precision + recall) |
|
31
|
|
|
|
|
32
|
|
|
def compute_crowd_performance(df_crowd_results, crowd_score_column, experts_score_column): |
|
33
|
|
|
""" Function to evaluate the answers of the crowd at each posible crowd score threshold""" |
|
34
|
|
|
rows = [] |
|
35
|
|
|
rows.append(["Thresh", "TP", "TN", "FP", "FN", "Precision", "Recall", "Accuracy", "F1-score"]) |
|
36
|
|
|
|
|
37
|
|
|
precision = 0.0 |
|
38
|
|
|
recall = 0.0 |
|
39
|
|
|
accuracy = 0.0 |
|
40
|
|
|
f1score = 0.0 |
|
41
|
|
|
|
|
42
|
|
|
for i in range(5, 101, 5): |
|
43
|
|
|
thresh = i / 100.0 |
|
44
|
|
|
|
|
45
|
|
|
true_pos, true_neg, false_pos, false_neg = count_positives_and_negatives(df_crowd_results, \ |
|
46
|
|
|
crowd_score_column, experts_score_column, thresh) |
|
47
|
|
|
|
|
48
|
|
|
precision = compute_precision(true_pos, false_pos) |
|
49
|
|
|
recall = compute_recall(true_pos, false_neg) |
|
50
|
|
|
accuracy = compute_accuracy(true_pos, true_neg, false_pos, false_neg) |
|
51
|
|
|
f1score = compute_f1score(precision, recall) |
|
52
|
|
|
|
|
53
|
|
|
row = [thresh, true_pos, true_neg, false_pos, false_neg, \ |
|
54
|
|
|
precision, recall, accuracy, f1score] |
|
55
|
|
|
rows.append(row) |
|
56
|
|
|
|
|
57
|
|
|
return rows |
|
58
|
|
|
|
|
59
|
|
|
def compute_majority_vote(df_crowd_results, crowd_score_column, experts_score_column, no_workers): |
|
60
|
|
|
""" Function to evaluate the answers of the crowd using majority vote""" |
|
61
|
|
|
|
|
62
|
|
|
true_pos, true_neg, false_pos, false_neg = count_positives_and_negatives(df_crowd_results, \ |
|
63
|
|
|
crowd_score_column, experts_score_column, no_workers) |
|
64
|
|
|
|
|
65
|
|
|
precision = compute_precision(true_pos, false_pos) |
|
66
|
|
|
recall = compute_recall(true_pos, false_neg) |
|
67
|
|
|
accuracy = compute_accuracy(true_pos, true_neg, false_pos, false_neg) |
|
68
|
|
|
f1score = compute_f1score(precision, recall) |
|
69
|
|
|
|
|
70
|
|
|
return true_pos, true_neg, false_pos, false_neg, \ |
|
71
|
|
|
precision, recall, accuracy, f1score |
|
72
|
|
|
|
|
73
|
|
|
def count_positives_and_negatives(df_crowd_results, crowd_score_col, expert_score_col, crowd_value): |
|
74
|
|
|
""" Help function for reading the crowd results """ |
|
75
|
|
|
true_positive = 0 |
|
76
|
|
|
true_negative = 0 |
|
77
|
|
|
false_positive = 0 |
|
78
|
|
|
false_negative = 0 |
|
79
|
|
|
|
|
80
|
|
|
for j in range(len(df_crowd_results.index)): |
|
81
|
|
|
if df_crowd_results[crowd_score_col].iloc[j] >= crowd_value: |
|
82
|
|
|
if df_crowd_results[expert_score_col].iloc[j] == 1: |
|
83
|
|
|
true_positive = true_positive + 1 |
|
84
|
|
|
else: |
|
85
|
|
|
false_positive = false_positive + 1 |
|
86
|
|
|
else: |
|
87
|
|
|
if df_crowd_results[expert_score_col].iloc[j] == 1: |
|
88
|
|
|
false_negative = false_negative + 1 |
|
89
|
|
|
else: |
|
90
|
|
|
true_negative = true_negative + 1 |
|
91
|
|
|
return true_positive, true_negative, false_positive, false_negative |
|
92
|
|
|
|