I have this code: def normalize_dictionary(self, d): """""" values = d.values() x = float(sum(values)) n = {} for k in d: n[k] = (d[k]/x) return n def smallest_value(self, nd1, nd2): """""" valuesnd1 = nd1.values() valuesnd2 = nd2.values() x = min(valuesnd1) y = min(valuesnd2) if x < y: return x else: return y def compare_dictionaries(self, d, nd1, nd2): """""" nd1 = self.normalize_dictionary(nd1) nd2 = self.normalize_dictionary(nd2) epsilon = self.smallest_value(nd1, nd2) / 2 log1, log2 = 0.0, 0.0 for key in d: if key in nd1: log1 += d[key] * math.log2(nd1[key]) else: log1 += d[key] * math.log2(epsilon) if key in nd2: log2 += d[key] * math.log2(nd2[key]) else: log2 += d[key] * math.log2(epsilon) return [log1, log2] d = {'a': 5, 'b': 1, 'c': 2} d1 = {'a': 5, 'b': 1, 'c': 2} nd1 = tm.normalize_dictionary(d1) assert nd1 == {'a': 0.625, 'b': 0.125, 'c': 0.25} d2 = {'a': 15, 'd': 1} nd2 = tm.normalize_dictionary(d2) assert nd2 == {'a': 0.9375, 'd': 0.0625} list_of_log_probs = tm.compare_dictionaries(d, nd1, nd2) print(list_of_log_probs) #assert list_of_log_probs[0] == -16.356143810225277 #assert list_of_log_probs[1] == -19.18621880878296 with this given the asserts list_of_log_probs gives me [-10.390359525563188, -15.465547021957407] in stead of the assertions answers.
I have this code:
def normalize_dictionary(self, d):
""""""
values = d.values()
x = float(sum(values))
n = {}
for k in d:
n[k] = (d[k]/x)
return n
def smallest_value(self, nd1, nd2):
""""""
valuesnd1 = nd1.values()
valuesnd2 = nd2.values()
x = min(valuesnd1)
y = min(valuesnd2)
if x < y:
return x
else:
return y
def compare_dictionaries(self, d, nd1, nd2):
""""""
nd1 = self.normalize_dictionary(nd1)
nd2 = self.normalize_dictionary(nd2)
epsilon = self.smallest_value(nd1, nd2) / 2
log1, log2 = 0.0, 0.0
for key in d:
if key in nd1:
log1 += d[key] * math.log2(nd1[key])
else:
log1 += d[key] * math.log2(epsilon)
if key in nd2:
log2 += d[key] * math.log2(nd2[key])
else:
log2 += d[key] * math.log2(epsilon)
return [log1, log2]
d = {'a': 5, 'b': 1, 'c': 2}
d1 = {'a': 5, 'b': 1, 'c': 2}
nd1 = tm.normalize_dictionary(d1)
assert nd1 == {'a': 0.625, 'b': 0.125, 'c': 0.25}
d2 = {'a': 15, 'd': 1}
nd2 = tm.normalize_dictionary(d2)
assert nd2 == {'a': 0.9375, 'd': 0.0625}
list_of_log_probs = tm.compare_dictionaries(d, nd1, nd2)
print(list_of_log_probs)
#assert list_of_log_probs[0] == -16.356143810225277
#assert list_of_log_probs[1] == -19.18621880878296
with this given the asserts list_of_log_probs gives me [-10.390359525563188, -15.465547021957407] in stead of the assertions answers.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images