how to get the index of numpy.random.choice? - python

前端 未结 8 1388
粉色の甜心
粉色の甜心 2021-02-02 12:31

Is it possible to modify the numpy.random.choice function in order to make it return the index of the chosen element? Basically, I want to create a list and select elements ran

8条回答
  •  说谎
    说谎 (楼主)
    2021-02-02 13:00

    This is a bit in left field compared with the other answers, but I thought it might help what it sounds like you're trying to do in a slightly larger sense. You can generate a random sample without replacement by shuffling the indices of the elements in the source array :

    source = np.random.randint(0, 100, size=100) # generate a set to sample from
    idx = np.arange(len(source))
    np.random.shuffle(idx)
    subsample = source[idx[:10]]
    

    This will create a sample (here, of size 10) by drawing elements from the source set (here, of size 100) without replacement.

    You can interact with the non-selected elements by using the remaining index values, i.e.:

    notsampled = source[idx[10:]]
    

提交回复
热议问题