首先我创建我的数组
myarray = np.random.random_integers(0,10, size=20)
Run Code Online (Sandbox Code Playgroud)
然后,我想将数组中20%的元素设置为0(或其他一些数字).我该怎么做?涂面膜?
Mik*_*ler 10
您可以计算指数np.random.choice,将所选指数的数量限制为百分比:
indices = np.random.choice(np.arange(myarray.size), replace=False,
size=int(myarray.size * 0.2))
myarray[indices] = 0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1734 次 |
| 最近记录: |