只需使用相同形状的任意一个掩盖您的输入数组即可。
import numpy as np# input arrayx = np.array([[ 1., 2., 3.], [ 4., 5., 6.], [ 7., 8., 9.]])# random boolean mask for which values will be changedmask = np.random.randint(0,2,size=x.shape).astype(np.bool)# random matrix the same shape of your datar = np.random.rand(*x.shape)*np.max(x)# use your mask to replace values in your input arrayx[mask] = r[mask]
产生这样的东西:
[[ 1. 2. 3. ] [ 4. 5. 8.54749399] [ 7.57749917 8. 4.22590641]]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)