0
随机选择我有一个列表的列表(以及它真的是收视率的矩阵),ground_truth。我想使非零项目的20%= 0.我最初的做法是:如何从2D numpy的阵列
ground_truth = [[0,99,98],[0,84,97], [55,0,0]]
ground_truth = np.array(ground_truth)
np.random.choice(ground_truth)
然而,这给出了错误
ValueError: a must be 1-dimensional
所以我的解决办法是我的矩阵压扁成一维数组,然后随机选取20%的非零项。
random_digits = np.random.choice(ground_truth.flatten()[ground_truth.flatten() > 0],
int(round(len(ground_truth.flatten()) * .2)))
in: random_digits
out: array([99, 97])
现在,我想将这些项目设置为0,并将更改反映在我的原始矩阵中。我怎样才能做到这一点?
到底是np.where干什么?当我运行'np.where(ground_truth)'我得到这样的结果:'(阵列([0,0,1,1,2],D型细胞= int64类型),阵列([1,2,1,2,0] ,dtype = int64))'。两个单独的长度数组5.为什么是这样? – Moondra
@Moondra它给出非零元素的行索引(第一元件)和列索引(第二元件)。 [numpy.where](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.where.html)。 – Psidom
谢谢!!!!!! –