Implements reservoir sampler randomly sampling stream of features#33
Open
daniel-j-h wants to merge 1 commit intomasterfrom
Open
Implements reservoir sampler randomly sampling stream of features#33daniel-j-h wants to merge 1 commit intomasterfrom
daniel-j-h wants to merge 1 commit intomasterfrom
Conversation
bkowshik
approved these changes
Jun 27, 2018
Contributor
bkowshik
left a comment
There was a problem hiding this comment.
Such a beautiful implementation! ❤️
| i = random.randint(0, size - 1) | ||
| self.reservoir[i] = v | ||
|
|
||
| self.pushed += 1 |
Contributor
There was a problem hiding this comment.
First we designate a counter, which will be incremented for every data point seen.
| '''Randomly samples k items from a stream of unknown n items. | ||
| ''' | ||
|
|
||
| def __init__(self, capacity): |
Contributor
There was a problem hiding this comment.
The reservoir is generally a list or array of predefined size.
| self.reservoir = [] | ||
| self.pushed = 0 | ||
|
|
||
| def push(self, v): |
Contributor
There was a problem hiding this comment.
Now we can begin adding data.
| size = len(self.reservoir) | ||
|
|
||
| if size < self.capacity: | ||
| self.reservoir.append(v) |
Contributor
There was a problem hiding this comment.
Until we encounter size elements, elements are added directly to reservoir
| assert size == self.capacity | ||
| assert size <= self.pushed | ||
|
|
||
| p = self.capacity / self.pushed |
Contributor
There was a problem hiding this comment.
Once reservoir is full, incoming data points have a size / counter chance to replace an existing sample point
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For #7. Work in progress.
This changeset implements a a randomized online algorithm "reservoir sampling" for randomly sampling k items from a stream of unknown n items. We can use this to randomly sample e.g. k building features in the osmium handlers without having to store all features first or doing two passes.
Tasks:
Refs: