0

I'd like to sample n random numbers from a linspace without replacement and do so in batches. Thus, each sample in the batch should not have repeated numbers, but numbers may repeat across the batch.

The following code shows how I do it by calling Generator.choice repeatedly.

import numpy as np
low, high = 0, 10
sample_shape = (3,)
n = 5

rng = np.random.default_rng()  # or previously instantiated RNG
space = np.linspace(start=low, stop=high, num=1000)
samples = np.stack(
    [
        rng.choice(space, size=n, replace=False)
        for _ in range(np.prod(sample_shape, dtype=int))
    ]
)
samples = samples.reshape(sample_shape + (n,))

print(f"samples.shape: {samples.shape}")
print(samples)

Current output:

samples.shape: (3, 5)
[[4.15415415 5.56556557 1.38138138 7.78778779 7.03703704]
 [1.48148148 6.996997   0.91091091 3.28328328 2.93293293]
 [7.82782783 9.65965966 9.94994995 5.84584585 5.26526527]]

However, this procedure turns out to be a big bottleneck in my code. Is there a more efficient way of performing this?

aaglovatto
  • 53
  • 6
  • 3
    So it is "without replacement" in a single batch but with replacement across batches? – ayhan Apr 06 '21 at 12:30
  • 2
    Does this answer your question? [Numpy random choice, replacement only along one axis](https://stackoverflow.com/questions/51310009/numpy-random-choice-replacement-only-along-one-axis) – Gulzar Apr 06 '21 at 13:28
  • @ayhan That's correct! – aaglovatto Apr 06 '21 at 15:07
  • @Gulzar The logic behind indexing the combinations is solid, however, I think generating all the combinations for bigger `n` might be very slow. I'll try it out to see if it's better than my current implementation. – aaglovatto Apr 06 '21 at 15:10

0 Answers0