def get_entropy(question, animals):
""" Finds the entropy of the answers of a question for a set of animals """
response_set = {answer:0.000001 for answer in ANSWERS}
all_responses = query_all_responses(question=question, animals=animals)
for animal in animals:
responses = all_responses.get(animal.name, NO_RESPONSE)
if responses is not None:
responses = sorted(
responses.items(),
key=lambda resp: -resp[1]
)
(first, _) = responses[0]
(last, _) = responses[-1]
response_set[first] += animal.prob
response_set[last] += (1-animal.prob)
else:
print("COULDN'T FIND ANIMAL \"{}\"".format(animal.name))
total = sum(response_set.values())
probs = [count/total for count in response_set.values()]
entropy = sum([-(p)*log2(p) for p in probs])
return entropy
评论列表
文章目录