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
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))
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With