Select cells randomly from NumPy array - without replacement

前端 未结 6 1048
萌比男神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条回答
  •  温柔的废话
    2021-02-19 17:15

    How about using numpy.random.shuffle or numpy.random.permutation if you still need the original array?

    If you need to change the array in-place than you can create an index array like this:

    your_array = 
    index_array = numpy.arange(your_array.size)
    numpy.random.shuffle(index_array)
    
    print your_array[index_array[:10]]
    

提交回复
热议问题