Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion hands_on/pyanno_voting/pyanno/tests/test_voting.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np

from numpy.testing import assert_array_equal
from pyanno import voting
from pyanno.voting import MISSING_VALUE as MV

Expand All @@ -16,6 +16,9 @@ def test_labels_count():
result = voting.labels_count(annotations, nclasses)
assert result == expected

def test_labels_frequency():
call_labels = voting.labels_frequency([[1, 1, 2], [-1, 1, 2]], 4)
assert_array_equal(call_labels, np.array([ 0. , 0.6, 0.4, 0. ]))

def test_majority_vote():
annotations = [
Expand Down
8 changes: 7 additions & 1 deletion hands_on/pyanno_voting/pyanno/voting.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def majority_vote(annotations):

def labels_frequency(annotations, nclasses):
"""Compute the total frequency of labels in observed annotations.

Example:
>>> labels_frequency([[1, 1, 2], [-1, 1, 2]], 4)
array([ 0. , 0.6, 0.4, 0. ])
Expand All @@ -100,3 +99,10 @@ def labels_frequency(annotations, nclasses):
freq[k] is the frequency of elements of class k in `annotations`, i.e.
their count over the number of total of observed (non-missing) elements
"""
annotations = np.array(annotations)
total_votes = np.sum(annotations != -1)
label_freq = np.empty(nclasses)
for i in range(nclasses):
num_votes = np.sum(annotations == i)
label_freq[i] = num_votes / total_votes
return label_freq