简体   繁体   中英

how to generate a matrix of random integers without duplicates in python

i need some help to create a matrix of dim (65, 8) where all elements are unique integers in range(522) in python

thanx!!

def bootstrap(x, num_samples, statistic, alpha):
"""Returns bootstrap estimate of 100.0*(1-alpha) CI for statistic."""
n = len(x)
y=len(x)**(1/3)
idx = np.random.randint(0, n, (num_samples, y)) #Return unique random integers from 0 to 520 in a martix with size num_samples X 520.
samples = x[idx]
stat = np.sort(statistic(samples, 1))
return (stat[int((alpha/2.0)*num_samples)],
        stat[int((1-alpha/2.0)*num_samples)])

In my opinion the easiest way would be to simply shuffle range of elements ie

x = numpy.arange(1, 521)
numpy.random.shuffle(x)

and then reshape it to matrix you want using numpy.reshape

x = numpy.reshape(x, (65,8))

Thank you a lot! However, I think the right answer is:

x = np.arange(1, 521).reshape((65, 8))
np.random.shuffle(x)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM