我想要根据给定行的概率分布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