def test_random_upsample(sampleset):
    samples = [('pos', 1), ('pos', 1), ('neg', 0)]
    stratified = sorted(util.upsample(samples, 1, rand=StableRandom(0)))
    assert stratified == [('neg', 0), ('neg', 0), ('pos', 1), ('pos', 1)]

    stratified1 = util.upsample(sampleset, 0, rand=StableRandom(0))
    _, labelcnts = util.group_samples(stratified1, 0)
    assert labelcnts == {0: 50, 1: 50}

    stratified2 = util.upsample(sampleset, 0, rand=StableRandom(1))
    assert stratified1 != stratified2, 'Order should be random'
Example #2
0
def test_random_downsample(sampleset):
    samples = [('pos', 1), ('pos', 1), ('neg', 0)]
    stratified = sorted(util.random_downsample(samples, 1, rand=rnd.Random(0)))
    assert stratified == [('neg', 0), ('pos', 1)]

    stratified1 = util.random_downsample(sampleset, 0, rand=rnd.Random())
    _, labelcnts = util.group_samples(stratified1, 0)
    assert labelcnts == {0: 10, 1: 10}

    stratified2 = util.random_downsample(sampleset, 0, rand=rnd.Random())
    assert stratified1 != stratified2, 'Order should be random'
def test_group_samples():
    samples = [('pos', 1), ('pos', 1), ('neg', 0)]
    groups, labelcnts = util.group_samples(samples, 1)
    assert groups == {0: [('neg', 0)], 1: [('pos', 1), ('pos', 1)]}
    assert labelcnts == cl.Counter({1: 2, 0: 1})