numpy create 2D mask from list of indices [+ then draw from masked array]

后端 未结 1 634
萌比男神i
萌比男神i 2021-01-17 02:12

I have a 2-D array of values and need to mask certain elements of that array (with indices taken from a list of ~ 100k tuple-pairs) before drawing random samples from the re

1条回答
  •  灰色年华
    2021-01-17 02:24

    You can do it efficently using sparse coo matrix

    from scipy import sparse
    xys=[(1,2),(3,4),(6,9),(7,3)]
    
    coords = zip(*xys)
    mask = sparse.coo_matrix((numpy.ones(len(coords[0])), coords ), shape= master_array.shape, dtype=bool)
    draws=numpy.random.choice( master_array[~mask.toarray()].flatten(), size=10)
    

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