You may also want to check out all available functions/classes of the module numpy.random, or try the search function. You can vote up the ones you like or vote down the ones you dont like, and go to the original project or source file by following the links above each example. So is what you usually want to go for, except for very small output size/ k. While the shuffle method cannot accept more than 1 array, there is a way to achieve this by using another important method of the random module np.random.permutation. The following are 30 code examples of (). Print("when output size/k is very small, random.sample() is quicker") If x is an array, make a copy and shuffle the elements randomly. If x is an integer, randomly permute np.arange (x). If x is a multi-dimensional array, it is only shuffled along its first index. 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()") (It basically does the shuffle-and-slice thing internally.) 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. Examples > np.random.permutation(10) array ( 1, 7, 4, 3, 0, 9, 2, 5, 8, 6) random > np.random.permutation( 1, 4, 9, 12, 15) array ( 15, 1, 9, 4, 12) random > arr np.arange(9).reshape( (3, 3)) > np.random. 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)) 13 Answers Sorted by: 1486 The idiomatic way to do this with Pandas is to use the. Numbers = rng.choice(20, size=10, replace=False) offers a replace argument to sample without replacement: from numpy.random import default_rng
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |