-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add exactly sampler #5637
Open
sagewe
wants to merge
3
commits into
develop-1.11.5
Choose a base branch
from
feat-1.11.5-add-exactly-sample
base: develop-1.11.5
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -15,6 +15,8 @@ | |||||||
# | ||||||||
|
||||||||
import itertools | ||||||||
import random | ||||||||
import sys | ||||||||
import typing | ||||||||
|
||||||||
from fate_arch.abc import CTableABC | ||||||||
|
@@ -156,27 +158,7 @@ def sample( | |||||||
return Table(self._table.sample(fraction=fraction, seed=seed)) | ||||||||
|
||||||||
if num is not None: | ||||||||
total = self._table.count() | ||||||||
if num > total: | ||||||||
raise ValueError( | ||||||||
f"not enough data to sample, own {total} but required {num}" | ||||||||
) | ||||||||
|
||||||||
frac = num / float(total) | ||||||||
while True: | ||||||||
sampled_table = self._table.sample(fraction=frac, seed=seed) | ||||||||
sampled_count = sampled_table.count() | ||||||||
if sampled_count < num: | ||||||||
frac += 0.1 | ||||||||
else: | ||||||||
break | ||||||||
|
||||||||
if sampled_count > num: | ||||||||
drops = sampled_table.take(sampled_count - num) | ||||||||
for k, v in drops: | ||||||||
sampled_table.delete(k) | ||||||||
|
||||||||
return Table(sampled_table) | ||||||||
return _exactly_sample(self, num, seed) | ||||||||
|
||||||||
raise ValueError( | ||||||||
f"exactly one of `fraction` or `num` required, fraction={fraction}, num={num}" | ||||||||
|
@@ -197,3 +179,58 @@ def subtractByKey(self, other: "Table"): | |||||||
@computing_profile | ||||||||
def union(self, other: "Table", func=lambda v1, v2: v1): | ||||||||
return Table(self._table.union(other._table, func)) | ||||||||
|
||||||||
def _exactly_sample(table: Table, num, seed): | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [autopep8] reported by reviewdog 🐶
Suggested change
|
||||||||
from scipy.stats import hypergeom | ||||||||
|
||||||||
split_size = list(table.mapPartitionsWithIndex(lambda s, it: [(s, sum(1 for _ in it))]).collect()) | ||||||||
total = sum(v for _, v in split_size) | ||||||||
|
||||||||
if num > total: | ||||||||
raise ValueError(f"not enough data to sample, own {total} but required {num}") | ||||||||
# random the size of each split | ||||||||
sampled_size = {} | ||||||||
for split, size in split_size: | ||||||||
if size <= 0: | ||||||||
sampled_size[split] = 0 | ||||||||
else: | ||||||||
sampled_size[split] = hypergeom.rvs(M=total, n=size, N=num) | ||||||||
total = total - size | ||||||||
num = num - sampled_size[split] | ||||||||
|
||||||||
return table.mapPartitionsWithIndex( | ||||||||
func=_ReservoirSample(split_sample_size=sampled_size, seed=seed).func, | ||||||||
shuffle=False, | ||||||||
) | ||||||||
|
||||||||
|
||||||||
class _ReservoirSample: | ||||||||
def __init__(self, split_sample_size, seed): | ||||||||
self._split_sample_size = split_sample_size | ||||||||
self._counter = 0 | ||||||||
self._sample = [] | ||||||||
self._seed = seed if seed is not None else random.randint(0, sys.maxsize) | ||||||||
self._random = None | ||||||||
|
||||||||
def initRandomGenerator(self, split): | ||||||||
self._random = random.Random(self._seed ^ split) | ||||||||
|
||||||||
# mixing because the initial seeds are close to each other | ||||||||
for _ in range(10): | ||||||||
self._random.randint(0, 1) | ||||||||
|
||||||||
def func(self, split, iterator): | ||||||||
self.initRandomGenerator(split) | ||||||||
size = self._split_sample_size[split] | ||||||||
for obj in iterator: | ||||||||
self._counter += 1 | ||||||||
if len(self._sample) < size: | ||||||||
self._sample.append(obj) | ||||||||
continue | ||||||||
|
||||||||
randint = self._random.randint(1, self._counter) | ||||||||
if randint <= size: | ||||||||
self._sample[randint - 1] = obj | ||||||||
|
||||||||
return self._sample | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [autopep8] reported by reviewdog 🐶
Suggested change
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[autopep8] reported by reviewdog 🐶