Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bbe9f3d

Browse files
Updated normalize to support dict
1 parent b771b97 commit bbe9f3d

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,16 @@ def num_or_str(x):
228228
return str(x).strip()
229229

230230

231-
def normalize(numbers):
231+
def normalize(dist):
232232
"""Multiply each number by a constant such that the sum is 1.0"""
233-
total = float(sum(numbers))
234-
return [(n / total) for n in numbers]
233+
if isinstance(dist, dict):
234+
total = sum(dist.values())
235+
for key in dist:
236+
dist[key] = dist[key] / total
237+
assert 0 <= dist[key] <= 1, "Probabilities must be between 0 and 1."
238+
return dist
239+
total = sum(dist)
240+
return [(n / total) for n in dist]
235241

236242

237243
def clip(x, lowest, highest):

0 commit comments

Comments
 (0)