-
Notifications
You must be signed in to change notification settings - Fork 1
/
BinWState.py
49 lines (36 loc) · 1.07 KB
/
BinWState.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
from numpy.random import multinomial
class BinWState:
stats = []
def __init__(self, statistics):
self.stats = normalization(statistics)
def sample(self, size):
dic = {}
for i in range(size):
sampling = list(multinomial(1, self.stats, size=1)[0])
s2 = list2str_inv(sampling)
dic = add2dict(dic, s2)
return dic
def to_w_state(lista):
return lista.index(max(lista))
def list2str_inv(lista):
string = [str(lista[i]) for i in range(len(lista))]
separator = ''
string = separator.join(string)
return string[::-1]
def add2dict(dictionary, item):
if item in list(dictionary.keys()):
dictionary[item] = dictionary[item] + 1
else:
dictionary[item] = 1
return dictionary
def normalization(lista):
suma = sum(lista)
for i in range(len(lista)):
if lista[i] >= 1.0:
lista = [0.0] * len(lista)
lista[i] = 1.0
elif lista[i] <= 0.0:
lista[i] = 0.0
else:
lista[i] = lista[i]/suma
return lista