I would like to know how can I select the values with more than one repetition when using numpy random to randomly generate numbers. I am doing the following:
1) First generate the numbers between 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.
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. 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 :
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])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.