跳转至

Federated Sampling

From Fate v0.2 supports sample method. Sample module supports threee sample modes: random sample mode, stratified sample mode, and exact sample by weight.

  • In random mode, "downsample" and "upsample" methods are provided. Users can set the sample parameter "fractions", which is the sample ratio within data.

  • In stratified mode, "downsample" and "upsample" methods are also provided. Users can set the sample parameter "fractions" too, but it should be a list of tuples in the form (label_i, ratio). Tuples in the list each specify the sample ratio of corresponding label. e.g.

  • When using exact_by_weight mode, samples will be duplicated ceil(weight) copies. Any zero-weighted samples will be discarded. Note that this mode requires that instances have match id: please set extend_sid in configuration when uploading data for this sample mode.

[(0, 1.5), (1, 2.5), (3, 3.5)]

Param

sample_param

Classes

SampleParam(mode='random', method='downsample', fractions=None, random_state=None, task_type='hetero', need_run=True)

Bases: BaseParam

Define the sample method

Parameters:

Name Type Description Default
mode

specify sample to use, default: 'random'

'random'

fractions: None or float or list if mode equals to random, it should be a float number greater than 0, otherwise a list of elements of pairs like [label_i, sample_rate_i], e.g. [[0, 0.5], [1, 0.8], [2, 0.3]]. default: None

random_state: int, RandomState instance or None, default: None random state

need_run: bool, default True Indicate if this module needed to be run

Source code in federatedml/param/sample_param.py
47
48
49
50
51
52
53
54
def __init__(self, mode="random", method="downsample", fractions=None,
             random_state=None, task_type="hetero", need_run=True):
    self.mode = mode
    self.method = method
    self.fractions = fractions
    self.random_state = random_state
    self.task_type = task_type
    self.need_run = need_run
Attributes
mode = mode instance-attribute
method = method instance-attribute
fractions = fractions instance-attribute
random_state = random_state instance-attribute
task_type = task_type instance-attribute
need_run = need_run instance-attribute
Functions
check()
Source code in federatedml/param/sample_param.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def check(self):
    descr = "sample param"
    self.mode = self.check_and_change_lower(self.mode,
                                            ["random", "stratified", "exact_by_weight"],
                                            descr)

    self.method = self.check_and_change_lower(self.method,
                                              ["upsample", "downsample"],
                                              descr)

    if self.mode == "stratified" and self.fractions is not None:
        if not isinstance(self.fractions, list):
            raise ValueError("fractions of sample param when using stratified should be list")
        for ele in self.fractions:
            if not isinstance(ele, collections.Container) or len(ele) != 2:
                raise ValueError(
                    "element in fractions of sample param using stratified should be a pair like [label_i, rate_i]")

    return True

最后更新: 2022-10-25