Skip to content

Commit

Permalink
modAL.utils.selection.weighted_random checks added to avoid division …
Browse files Browse the repository at this point in the history
…with zero
  • Loading branch information
cosmic-cortex committed Apr 17, 2018
1 parent e6d658c commit 0d24a4d
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion modAL/utils/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def weighted_random(weights, n_instances=1):
n_instances random indices based on the weights.
"""
assert n_instances <= len(weights), 'n_instances must be less or equal than the size of utility'
weight_sum = np.sum(weights)
assert weight_sum > 0, 'the sum of weights must be larger than zero'

random_idx = np.random.choice(range(len(weights)), size=n_instances, p=weights / np.sum(weights), replace=False)
random_idx = np.random.choice(range(len(weights)), size=n_instances, p=weights/weight_sum, replace=False)
return random_idx

0 comments on commit 0d24a4d

Please sign in to comment.