问题描述
我正在尝试从图中随机选择 n 个样本.为此,我使用如下所示的 random.sample 函数创建了一个名为 X 的列表:
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)
问题是当 numPosSamples 等于 graph.ecount() 时,我收到以下错误:
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
推荐答案
我不确定 numPosSamples
是如何获取其值的,但因为 random.sample
会进行采样没有替换,这里可能发生的是 numPosSamples
大于图中的边数.结果,Python 引发了您看到的 ValueError
.
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.
要么将样本数量减少到少于边的数量,要么使用允许采样和替换的采样方法,例如使用random.choice.
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
.
这篇关于ValueError:样本大于从图中选择样本的总体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!