Numpy: Get random set of rows from 2D array

后端 未结 8 2043
挽巷
挽巷 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条回答
  •  -上瘾入骨i
    2020-11-28 02:42

    Another option is to create a random mask if you just want to down-sample your data by a certain factor. Say I want to down-sample to 25% of my original data set, which is currently held in the array data_arr:

    # generate random boolean mask the length of data
    # use p 0.75 for False and 0.25 for True
    mask = numpy.random.choice([False, True], len(data_arr), p=[0.75, 0.25])
    

    Now you can call data_arr[mask] and return ~25% of the rows, randomly sampled.

提交回复
热议问题