Given a tuple list a
:
a =[(23, 11), (10, 16), (13, 11), (12, 3), (4, 15), (10, 16), (10, 16)]
We can count how many appearances of each tuple we have using Counter
:
>>> from collections import Counter
>>> b = Counter(a)
>>> b
Counter({(4, 15): 1, (10, 16): 3, (12, 3): 1, (13, 11): 1, (23, 11): 1}
Now, the idea is to select 3 random tuples from the list, without repetition, such that the count determines the probability that a particular tuple is chosen.
For instance, (10, 16)
is more likely to be chosen than the others - its weight is 3/7 while the other four tuples have weight 1/7.
I have tried to use np.random.choice
:
a[np.random.choice(len(a), 3, p=b/len(a))]
But I'm not able to generate the tuples.
Im trying:
a =[(23, 11), (10, 16), (13, 11), (10, 16), (10, 16), (10, 16), (10, 16)]
b = Counter(a)
c = []
print "counter list"
print b
for item in b:
print "item from current list"
print item
print "prob of the item"
print (float(b[item])/float(len(a)))
c.append(float(b[item])/float(len(a)))
print "prob list"
print c
print (np.random.choice(np.arange(len(b)), 3, p=c, replace=False))
In this case im getting the random indexes of the array.
Is there any more optimized way not to have to calculate the probabilities array?
Also there is an issue that is that the prob array does not correspond to the Counter array.