首页 > 解决方案 > Generating approximately unique set of numbers from numpy array

问题描述

I want to generate unique smaller arrays from numpy array.I used numpy.random.choice which does'nt seem to produce unique array. Here is my code :

sample=np.array([5,6,1,8,9,2,10,4,3,17,11,19,7,15])
for i in range(30):
    sample=np.random.choice(sample,3,replace=True)
    print(sample)

Here is the output

    [8 5 4]
    [4 8 4]
    [4 8 4]
    [4 4 4]
    [4 4 4]
    [4 4 4]
    [4 4 4]
    [4 4 4]
    [4 4 4]
    [4 4 4]
      .
      .
      .
      .
  Goes on like that

I want it to be at least approximately unique or uniformly distributed. But here [4,4,4] dominates. What I am doing wrong?

标签: pythonarraysnumpy

解决方案


You reset sample in the middle of your loop, after which there is not much randomness to be had.

Use something like

 foo=np.random.choice(sample,3,replace=True)

推荐阅读