mercredi 25 juillet 2018

How to get the remaining sample after using random.sample() in Python?

I have a large list of elements (in this example I'll assume it's filled with numbers). For example: l = [1,2,3,4,5,6,7,8,9,10] Now I want to take 2 samples from that list, one with the 80% of the elements (randomly chosen of course), and the other one with the remaining elements (the 20%), so I can use the bigger one to train a machine-learning tool, and the rest to test that training. The function I used is from random and I used it this way:

sz = len(l) #Size of the original list
per = int((80 * sz) / 100) #This will be the length of the sample list with the 80% of the elements (I guess)
random.seed(1) # As I want to obtain the same results every time I run it.
l2 = random.sample(l, per)

I'm not totally sure, but I believe that with that code I'm getting a random sample with the 80% of the numbers.

l2 = [3,4,7,2,9,5,1,8]

Nonetheless, I can't seem to find the way to get the other sample list with the remaining elements l3 = [6,10] (the sample() function does not remove the elements it takes from the original list). Can you please help me? Thank you in advance.




Aucun commentaire:

Enregistrer un commentaire