Select cells randomly from NumPy array - without replacement

前端 未结 6 1050
萌比男神i
萌比男神i 2021-02-19 16:41

I\'m writing some modelling routines in NumPy that need to select cells randomly from a NumPy array and do some processing on them. All cells must be selected without replacemen

6条回答
  •  -上瘾入骨i
    2021-02-19 17:08

    Let's say you have an array of data points of size 8x3

    data = np.arange(50,74).reshape(8,-1)
    

    If you truly want to sample, as you say, all the indices as 2d pairs, the most compact way to do this that i can think of, is:

    #generate a permutation of data's size, coerced to data's shape
    idxs = divmod(np.random.permutation(data.size),data.shape[1])
    
    #iterate over it
    for x,y in zip(*idxs): 
        #do something to data[x,y] here
        pass
    

    Moe generally, though, one often does not need to access 2d arrays as 2d array simply to shuffle 'em, in which case one can be yet more compact. just make a 1d view onto the array and save yourself some index-wrangling.

    flat_data = data.ravel()
    flat_idxs = np.random.permutation(flat_data.size)
    for i in flat_idxs:
        #do something to flat_data[i] here
        pass
    

    This will still permute the 2d "original" array as you'd like. To see this, try:

     flat_data[12] = 1000000
     print data[4,0]
     #returns 1000000
    

提交回复
热议问题