gpt4 book ai didi

python - 根据概率将列替换为 0

转载 作者:行者123 更新时间:2023-12-05 04:25:47 24 4
gpt4 key购买 nike

如何根据概率将 numpy 数组中的列替换为某个数字,如果它是 (1,X,X) 形状。我找到了替换行的代码,但不知道如何修改它,所以它适用于替换列。

grid_example = np.random.rand(1,5,5)
probs = np.random.random((1,5))
grid_example[probs < 0.25] = 0
grid_example

谢谢!

enter image description here

最佳答案

使用:

import numpy as np

rng = np.random.default_rng(42)

grid_example = rng.random((1, 5, 5))
probs = rng.random((1, 5))

grid_example[..., (probs < 0.25).flatten()] = 0
print(grid_example)

输出

[[[0.         0.43887844 0.         0.         0.09417735]
[0. 0.7611397 0. 0. 0.45038594]
[0. 0.92676499 0. 0. 0.4434142 ]
[0. 0.55458479 0. 0. 0.6316644 ]
[0. 0.35452597 0. 0. 0.7783835 ]]]

符号[..., (probs < 0.25).flatten()]将 bool 索引应用于最后一个索引。更多关于 documentation .

关于python - 根据概率将列替换为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73170174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com