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

前端 未结 8 1384
粉色の甜心
粉色の甜心 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 12:44
    numpy.random.choice(a, size=however_many, replace=False)
    

    If you want a sample without replacement, just ask numpy to make you one. Don't loop and draw items repeatedly. That'll produce bloated code and horrible performance.

    Example:

    >>> a = numpy.arange(10)
    >>> a
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    >>> numpy.random.choice(a, size=5, replace=False)
    array([7, 5, 8, 6, 2])
    

    On a sufficiently recent NumPy (at least 1.17), you should use the new randomness API, which fixes a longstanding performance issue where the old API's replace=False code path unnecessarily generated a complete permutation of the input under the hood:

    rng = numpy.random.default_rng()
    result = rng.choice(a, size=however_many, replace=False)
    
    0 讨论(0)
  • 2021-02-02 12:47

    Here's one way to find out the index of a randomly selected element:

    import random # plain random module, not numpy's
    random.choice(list(enumerate(a)))[0]
    => 4      # just an example, index is 4
    

    Or you could retrieve the element and the index in a single step:

    random.choice(list(enumerate(a)))
    => (1, 4) # just an example, index is 1 and element is 4
    
    0 讨论(0)
  • 2021-02-02 12:48

    Here is a simple solution, just choose from the range function.

    import numpy as np
    a = [100,400,100,300,300,200,100,400]
    I=np.random.choice(np.arange(len(a)))
    print('index is '+str(I)+' number is '+str(a[I]))
    
    0 讨论(0)
  • 2021-02-02 12:49

    Maybe late but it worth to mention this solution because I think the simplest way to do so is:

    a = [1, 4, 1, 3, 3, 2, 1, 4]
    n = len(a)
    idx = np.random.choice(list(range(n)), p=np.ones(n)/n)
    

    It means you are choosing from the indices uniformly. In a more general case, you can do a weighted sampling (and return the index) in this way:

    probs = [.3, .4, .2, 0, .1]
    n = len(a)
    idx = np.random.choice(list(range(n)), p=probs)
    

    If you try to do so for so many times (e.g. 1e5), the histogram of the chosen indices would be like [0.30126 0.39817 0.19986 0. 0.10071] in this case which is correct.

    Anyway, you should choose from the indices and use the values (if you need) as their probabilities.

    0 讨论(0)
  • 2021-02-02 12:51

    Instead of using choice, you can also simply random.shuffle your array, i.e.

    random.shuffle(a)  # will shuffle a in-place
    
    0 讨论(0)
  • 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:]]
    
    0 讨论(0)
提交回复
热议问题