ValueError: sample is larger than sample by sample from graph

I am trying to randomly select n samples from a graph. To do this, I create a list named X using the random.sample function as shown below:

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

      

The problem is, when numPosSamples is graph.ecount (), I get the following error:

ValueError: Sample larger than population

      

Any help would be much appreciated. Thanks to

+3


source to share


2 answers


I'm not sure how it numPosSamples

gets its value, but since it random.sample

is fetching without replacing, what is probably happening here is that there are numPosSamples

more than the number of edges in your graph. As a result, Python creates the ValueError

one you see.



Either reduce the number of samples to the number of edges, or use a sampling technique that allows you to perform select and replace, such as a list comprehension with random.choice

.

+1


source


You can add some logic that determines if your list is shorter than the number of samples required.

For example:



a = list(range(10))
num_samples = 20
sample(a, num_samples if len(a) > num_samples else len(a))

      

0


source







All Articles