Python中的numpy.random.choice:ValueError:p中非零条目比大小少。

7

我想要根据给定行的概率分布prob来随机选择样本点。 但是,当我调用np.random.choice时,我遇到了错误ValueError: Fewer non-zero entries in p than size。这里的size是什么意思?我还查看了实现,但我还是不理解。谢谢任何帮助!

import numpy as np

# prob is a numpy array of shape (14, 6890)
all_zero = np.where(prob.max(1) < 1e-6)[0] # find indices of rows where all values are smaller
prob[all_zero] = 1 / prob.shape[1] # fill those rows uniformly
prob /= prob.sum(axis=1, keepdims=True)
# ... somewhere later inside a method
for j in range(14):
    sample = np.random.choice(6890, 4, replace=False, p=prob[j]) # error occurs here

有更多的用户使用 numpy 而不是 torch。如果你能够仅使用 numpy 重现那个错误,你将会触达更多的人。 - Brenlla
好的,我已经将那部分编辑掉了。 - Rani
1个回答

5
问题出在你对np.random.choice的使用上,你让它从一个有小于4个非null值的包含6890个元素的数组中(replace=False)选取4个元素,但不能重复选取。例如:
>>> np.random.choice(5, 1, replace=False, p=[0, 0, 0, 0.6, 0.4])
array([4])

>>> np.random.choice(5, 4, replace=False, p=[0, 0, 0, 0.6, 0.4])
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    np.random.choice(5, 4, replace=False, p=[0, 0, 0, 0.6, 0.4])
  File "mtrand.pyx", line 826, in numpy.random.mtrand.RandomState.choice
ValueError: Fewer non-zero entries in p than size

>>> np.random.choice(5, 4, replace=True, p=[0, 0, 0, 0.6, 0.4])
array([3, 3, 4, 3])

因此,分辨率取决于您的需求,您可以确保有更多的非空值,或者启用随机选择中的替换。

参考文献:numpy.random.choice 的文档:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接