numpy:替换数组中的随机元素

numpy:替换数组中的随机元素,第1张

numpy:替换数组中的随机元素

只需使用相同形状的任意一个掩盖您的输入数组即可。

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]]


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5649238.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存