Numpy: Get random set of rows from 2D array

后端 未结 8 1946
挽巷
挽巷 2020-11-28 01:49

I have a very large 2D array which looks something like this:

a=
[[a1, b1, c1],
 [a2, b2, c2],
 ...,
 [an, bn, cn]]

Using numpy, is there a

相关标签:
8条回答
  • 2020-11-28 02:52

    This is a similar answer to the one Hezi Rasheff provided, but simplified so newer python users understand what's going on (I noticed many new datascience students fetch random samples in the weirdest ways because they don't know what they are doing in python).

    You can get a number of random indices from your array by using:

    indices = np.random.choice(A.shape[0], amount_of_samples, replace=False)
    

    You can then use slicing with your numpy array to get the samples at those indices:

    A[indices]
    

    This will get you the specified number of random samples from your data.

    0 讨论(0)
  • 2020-11-28 02:52

    If you need the same rows but just a random sample then,

    import random
    new_array = random.sample(old_array,x)
    

    Here x, has to be an 'int' defining the number of rows you want to randomly pick.

    0 讨论(0)
提交回复
热议问题