![]() ![]() So is what you usually want to go for, except for very small output size/ k. If x is a multi-dimensional array, it is only shuffled. Print(5, timeit.timeit("random.sample( population=range(10**5), k=10**1)", setup="import random", number=10**3)) #0.008387799956835806 The NumPy random.permutation() function randomly permutes a sequence or an array, and returns it. Print("when output size/k is very small, random.sample() is quicker") Print("when output size/k is large, np.fault_rng().choice() is far far quicker, even when including time taken to create np.fault_rng()") Example 1 : In this example we can see that by using () method, we are able to. (It basically does the shuffle-and-slice thing internally.) Return : Return the random sequence of permuted values. Restore the state of the random number generator to s, and then create a new random permutation of the integers from 1 to 8. There's also a replace argument in the legacy function, but this argument was implemented inefficiently and then left inefficient due to random number stream stability guarantees, so its use isn't recommended. Save the current state of the random number generator and create a random permutation of the integers from 1 to 8. You can also use () and slicing, but this will be less efficient: a = numpy.arange(20) If you're on a pre-1.17 NumPy, without the Generator API, you can use random.sample() from the standard library: print(random.sample(range(20), 10)) Feature request: 'np.permute' 17085 Open mruberry opened this issue on 6 comments mruberry commented on It is the correct mathematical name for the operation It would be helpful to provide library writers a mechanism to permute both NumPy-like arrays and PyTorch tensors. Numbers = rng.choice(20, size=10, replace=False) offers a replace argument to sample without replacement: from numpy.random import default_rng Unlike many other numpy/random functions, () doesnt provide an obvious way to return multiple results in a single function call.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |