-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_sampler.py
71 lines (58 loc) · 1.88 KB
/
random_sampler.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
#############################################################################
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
#+++++++++++++++++++Built by Miguel Fernandes Guerreiro+++++++++++++++++++++#
#+++++++++++++++++++++++++++++++++22/05/2017++++++++++++++++++++++++++++++++#
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
#############################################################################
'''
sampling iterable objects
'''
from random import randint
from copy import deepcopy
def WithReposition(n,iterable):
'''subsamples **with** reposition
requires:[iterable: str(), tuple(), list()] and [n: int()] number of samples
to retrieve
ensures:length n list with n random samples from iterable provided
'''
l = len(iterable)
k = []
while n>len(k):
k.append(randint(0, l-1))
l = []
for i in list(k):
l.append(iterable[i])
return l
def WithoutReposition(n,iterable):
'''subsamples **without** reposition
requires:[iterable: str(), tuple(), list()] and [n: int()] number of samples
to retrieve
ensures:length n list with n different random samples from iterable provided
'''
l = len(iterable)
k = set()
while n>len(k):
k.add(randint(0, l-1))
l = []
for i in list(k):
l.append(iterable[i])
return l
def negativeSampling(sampling,iterable):
'''subsamples negative of selection list
requires:
[iterable: str(), tuple(), list()]
[sampling: tuple(), list()] indexes[int()] *not* to retrieve
ensures:
collection without indexes present in sampling
'''
j = deepcopy(list(iterable))
s = sorted(list(set(sampling)), reverse=True)
for i in s:
j.pop(i)
return j
if __name__=='__main__':
print('test')
k = 'asdfg'
print(WithReposition(4,k))
print(WithoutReposition(4,k))
print(negativeSampling((1,4,2,3,9),"0123456789"))