Created
February 17, 2013 12:24
-
-
Save cvanweelden/4971289 to your computer and use it in GitHub Desktop.
A python method for weighted sampling without replacement based on roulette selection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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