Skip to content

Instantly share code, notes, and snippets.

@cvanweelden
Created February 17, 2013 12:24
Show Gist options
  • Save cvanweelden/4971289 to your computer and use it in GitHub Desktop.
Save cvanweelden/4971289 to your computer and use it in GitHub Desktop.
A python method for weighted sampling without replacement based on roulette selection.
def weighted_sample(weights, sample_size):
"""
Returns a weighted sample without replacement. Note: weights.dtype should be float for speed, see: http://sociograph.blogspot.nl/2011/12/gotcha-with-numpys-searchsorted.html
"""
totals = np.cumsum(weights)
sample = []
for i in xrange(sample_size):
rnd = random.random() * totals[-1]
idx = np.searchsorted(totals,rnd,'right')
sample.append(idx)
totals[idx:] -= weights[idx]
return sample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment