[英]Select the values with more than one repetition when using numpy random?
I would like to know how can I select the values with more than one repetition when using numpy random to randomly generate numbers. 我想知道在使用numpy random随机生成数字时如何选择多个重复值。 I am doing the following:
我正在执行以下操作:
1) First generate the numbers between 1-10 1)首先生成1-10之间的数字
ran = ceil( np.random.random(10)*10 )
print ran
[ 2. 9. 8. 9. 10. 8. 10. 7. 1. 1.]
2) Now using itemfreq from scipy.stats to get the frequency table where the second column is the frequency. 2)现在,使用scipy.stats中的itemfreq来获取频率表,其中第二列是频率。
freq_tmp = itemfreq(ran)
print freq_tmp
[[ 1. 2.]
[ 2. 1.]
[ 7. 1.]
[ 8. 2.]
[ 9. 2.]
[ 10. 2.]]
print freq_tmp[0:][:,1]
[ 2. 1. 1. 2. 2. 2.]
this show me only the frequency but I would like to know the values with more than 1 repetition, in this example should be: 这仅显示频率,但我想知道重复次数超过1的值,在此示例中应为:
[1. 8. 9. 10]
Thank you for your help! 谢谢您的帮助!
You are almost there. 你快到了。 You can just put a
>
in the slicer like so: 您可以像这样在切片器中输入
>
:
import numpy as np
a = np.array([[1,2],[2,1],[7,1],[8,2],[9,2],[10,2]])
print(a[a[:,1] > 1][:,0])
output: 输出:
[ 1 8 9 10]
Another numpy solution : 另一个numpy的解决方案:
In [57]: ran=randint(1,11,10)
In [58]: ran
Out[58]: array([3, 4, 6, 1, 9, 4, 2, 8, 6, 8])
In [59]: uniqs,cnts=np.unique(ran,return_counts=True)
In [60]: uniqs,cnts
Out[60]: (array([1, 2, 3, 4, 6, 8, 9]), array([1, 1, 1, 2, 2, 2, 1], dtype=int64))
In [61]: uniqs[cnts>=2]
Out[61]: array([4, 6, 8])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.