在 numpy 2d 数组中设置为非零值的 0 x%



我尝试了不同的方法,但如果不循环,我似乎不可能有效地做到这一点。

输入是数组 y 和百分比 x。

例如,输入是

y=np.random.binomial(1,1,[10,10])
x=0.5

输出

[[0 0 0 0 1 1 1 1 0 1]
 [1 0 1 0 0 1 0 1 0 1]
 [1 0 1 1 1 1 0 0 0 1]
 [0 1 0 1 1 0 1 0 1 1]
 [0 1 1 0 0 1 1 1 0 0]
 [0 0 1 1 1 0 1 1 0 1]
 [0 1 0 0 0 0 1 0 1 1]
 [0 0 0 1 1 1 1 1 0 0]
 [0 1 1 1 1 0 0 1 0 0]
 [1 0 1 0 1 0 0 0 0 0]]
这是一个

基于masking -

def set_nonzeros_to_zeros(a, setz_ratio):
    nz_mask = a!=0
    nz_count = nz_mask.sum()
    z_set_count = int(np.round(setz_ratio*nz_count))
    idx = np.random.choice(nz_count,z_set_count,replace=False)
    mask0 = np.ones(nz_count,dtype=bool)
    mask0.flat[idx] = 0
    nz_mask[nz_mask] = mask0
    a[~nz_mask] = 0
    return a

我们跳过了所有索引的生成,np.argwhere/np.nonzero转而使用基于掩码的索引来关注性能。

示例运行 -

In [154]: np.random.seed(0)
     ...: a = np.random.randint(0,3,(5000,5000))
# number of non-0s before using solution
In [155]: (a!=0).sum()
Out[155]: 16670017
In [156]: a_out = set_nonzeros_to_zeros(a, setz_ratio=0.2) #set 20% of non-0s to 0s
# number of non-0s after using solution
In [157]: (a_out!=0).sum()
Out[157]: 13336014
# Verify
In [158]: 16670017 - 0.2*16670017
Out[158]: 13336013.6

有一些矢量化方法可能会对您有所帮助,具体取决于您要执行的操作:

# Flatten the 2D array and get the indices of the non-zero elements
c = y.flatten()
d = c.nonzero()[0]
# Shuffle the indices and set the first 100x % to zero
np.random.shuffle(d)
x = 0.5
c[d[:int(x*len(d))]] = 0
# reshape to the original 2D shape
y = c.reshape(y.shape)

毫无疑问,这里需要提高一些效率。

最新更新