给出一个元组列表a:
a =[(23, 11), (10, 16), (13, 11), (12, 3), (4, 15), (10, 16), (10, 16)]我们可以使用Counter计算每个元组的出现次数:
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}现在,我们的想法是从列表中选择3个随机元组,无重复,这样计数就可以确定选择特定元组的概率.
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.
例如,(10, 16)比其他的更有可能被选中-它的权重为3/7,而其他四个元组的权重为1/7.
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.
我尝试使用np.random.choice:
a[np.random.choice(len(a), 3, p=b/len(a))]但是我无法生成元组.
我正在尝试
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?
还有一个问题,就是prob数组与Counter数组不对应.
Also there is an issue that is that the prob array does not correspond to the Counter array.
推荐答案这可以解决问题
from collections import Counter import matplotlib.pyplot as plt import numpy as np import random listOfNumbers =[(23, 11), (10, 16), (13, 11), (10, 16), (10, 16), (10, 16), (10, 16)] b = Counter(listOfNumbers) c = [] pres=[] for k,v in b.most_common(): c.append(float(v)/float(len(listOfNumbers))) pres.append(k) resultIndex = np.random.choice(np.arange(len(b)), 3, p=c, replace=False) ass=[] for res in resultIndex: ass.append(pres[res]) print ass现在只是看看是否有任何方法可以对其进行优化.
Now is just to see if is there any way to optimize it.