Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Sample larger than population selecting samples from graph

I am trying to randomly select n samples from a graph. In order to do so I create a list called X using the random.sample function like the following:

X= random.sample(range(graph.ecount()), numPosSamples)

The problem is that when numPosSamples is equal to graph.ecount() I receive the following error:

ValueError: Sample larger than population

Any help will be much appreciated. Thanks

like image 202
ahajib Avatar asked Oct 20 '22 13:10

ahajib


2 Answers

You can add some logic that detects if your list if shorter than the number of samples you want.

For example:

a = list(range(10))
num_samples = 20
sample(a, num_samples if len(a) > num_samples else len(a))
like image 82
Andres Romero Avatar answered Oct 22 '22 02:10

Andres Romero


I'm not sure how numPosSamples is getting its value, but because random.sample does sampling without replacement, what is probably happening here is that numPosSamples is greater than the number of edges in your graph. As a result, Python raises the ValueError that you are seeing.

Either reduce the number of samples to less than the number of edges, or use a method of sampling that allows for sampling with replacement, such as a list comprehension with random.choice.

like image 23
Zachary Cross Avatar answered Oct 22 '22 04:10

Zachary Cross