Numpy random.choice items that are not selected

I have an array A, as shown below:

import numpy as np
A = np.random.sample(100)

I want to create 2 random subsets from A so that, combining them, I get A

inx = np.random.choice(np.arange(100), size=70, replace=False)
S1 = A[inx]

So, S1 is one of the subsets, now how can I build S2 to contain all the elements from A that are not in S1; in other words, S2 = A-S1.

+4
source share
3 answers

Defined operations can help:

S2 = A[list(set(range(100)) - set(inx))]

But you may need to sort:

S2 = A[ sorted(list(set(range(100)) - set(inx))) ]
+4
source

(Minor: if A can have duplicate elements, the choice of complementing the indices and the presence of S2 contains all the elements in A and not in S1 - this is not the same thing.)

, , :

>>> A = np.random.sample(10)
>>> S1, S2 = np.split(np.random.permutation(A), [7])
>>> S1
array([ 0.97128145,  0.5617039 ,  0.42625808,  0.39108218,  0.52366291,
        0.73606525,  0.5279909 ])
>>> S2
array([ 0.45652426,  0.38622805,  0.99084781])

np.setdiff1d, , S1:

>>> S2 = np.setdiff1d(A, S1)
>>> S2
array([ 0.38622805,  0.45652426,  0.99084781])
+2

, , .

A = np.random.sample(100)
T = A[:]
np.random.shuffle(T)

size = 70
S1 = T[:size]
S2 = T[size:]
0

Source: https://habr.com/ru/post/1535139/


All Articles