gpt4 book ai didi

python - 索引位置为 1 的数组

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:58 25 4
gpt4 key购买 nike

我目前有一个数组中最小值的索引数组。

看起来像这样:

[[0],
[1],
[2],
[1],
[0]]

(最大索引为3)

我想要的是一个如下所示的数组:

[[1, 0, 0]
[0, 1, 0]
[0, 0, 1]
[0, 1, 0]
[1, 0, 0]]

其中 1 在最小值的列中。

在 numpy 中有没有简单的方法来做到这一点?

最佳答案

使用 NumPy 的 == 广播:

>>> minima = np.array([[0], [1], [2], [1], [0]])
>>> minima == arange(minima.max() + 1)
array([[ True, False, False],
[False, True, False],
[False, False, True],
[False, True, False],
[ True, False, False]], dtype=bool)
>>> (minima == arange(minima.max() + 1)).astype(int)
array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
[0, 1, 0],
[1, 0, 0]])

关于python - 索引位置为 1 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17966077/

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