-
Notifications
You must be signed in to change notification settings - Fork 0
/
reservoir_sampling.py
105 lines (89 loc) · 2.36 KB
/
reservoir_sampling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
# 蓄水池算法
# 更多的讨论: http://hatlonely.github.io/2018/01/26/蓄水池算法/
import unittest
import random
def reservior_sampling(sequence, k):
n = len(sequence)
if k > n:
return sequence
sample = list()
for i in range(k):
sample.append(sequence[i])
for i in range(k, n):
j = random.randint(0, i)
if j >= k:
continue
sample[j] = sequence[i]
return sample
def weighted_reservior_sampling_achao(sequence, k):
n = len(sequence)
if k > n:
return sequence
wsum = 0
sample = list()
for i in range(k):
sample.append(sequence[i])
wsum += sequence[i]['weight'] / k
for i in range(k, n):
wsum += sequence[i]['weight'] / k
p = sequence[i]['weight'] / wsum
j = random.random()
if j <= p:
sample[random.randint(0, k-1)] = sequence[i]
return sample
class TestReserviorSampling(unittest.TestCase):
def test_reservior_sampling(self):
sequence = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
k = 4
times = dict()
for item in sequence:
times[item] = 0
for i in range(100000):
sample = reservior_sampling(sequence, k)
for item in sample:
times[item] += 1
print(times)
def test_weighted_reservior_sampling_achao(self):
sequence = [{
'value': 0,
'weight': 10,
}, {
'value': 1,
'weight': 20,
}, {
'value': 2,
'weight': 30,
}, {
'value': 3,
'weight': 10,
}, {
'value': 4,
'weight': 20,
}, {
'value': 5,
'weight': 30,
}, {
'value': 6,
'weight': 10,
}, {
'value': 7,
'weight': 20,
}, {
'value': 8,
'weight': 30,
}, {
'value': 9,
'weight': 10,
}]
k = 4
times = dict()
for item in sequence:
times[item['value']] = 0
for i in range(100000):
sample = weighted_reservior_sampling_achao(sequence, k)
for item in sample:
times[item['value']] += 1
print(times)
if __name__ == '__main__':
unittest.main()