To sample a pair without replacements, you can use np.random.choice
:
np.random.choice(X, size=2, replace=False)
Alternatively, to sample multiple elements at a time, note that all possible pairs may be represented by the elements of range(len(X)*(len(X)-1)/2)
, and sample from that using np.random.randint
.
combs = np.array(list(itertools.combinations(X, 2)))
sample = np.random.randint(len(combs), size=10)
combs[sample[np.newaxis]]
Following up on @user2357112's comment, given from the OP's own answer that they do not appear to care if the sample size itself is deterministic, and noting that sampling with the Mersenne Twister is slower than basic arithmetic operations, a different solution if X
is so large that generating the combinations is not feasibile would be
sample = np.random.randint(len(X)**2, size=N)
i1 = sample // len(X)
i2 = sample % len(X)
X[np.vstack((i1, i2)).T[i1 != i2]]
This produces a sample whose average size is N * (1 - 1/len(X))
.