Python 1'的数组;处于索引位置的s

Python 1'的数组;处于索引位置的s,python,numpy,matrix,scipy,Python,Numpy,Matrix,Scipy,我目前有一个数组中最小值的索引数组 它看起来像这样: [[0], [1], [2], [1], [0]] [[1, 0, 0] [0, 1, 0] [0, 0, 1] [0, 1, 0] [1, 0, 0]] (最大索引为3) 我想要的是一个如下所示的数组: [[0], [1], [2], [1], [0]] [[1, 0, 0] [0, 1, 0] [0, 0, 1] [0, 1, 0] [1, 0, 0]] 其中,1位于最小值列中 在numpy中有没有

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

它看起来像这样:

[[0],
 [1],
 [2],
 [1],
 [0]]
[[1, 0, 0]
 [0, 1, 0]
 [0, 0, 1]
 [0, 1, 0]
 [1, 0, 0]]
(最大索引为3)

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

[[0],
 [1],
 [2],
 [1],
 [0]]
[[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]])
对于你可以做的列表

>>> a = [[0], [1], [2], [1], [0]]
>>> N = 3
>>> [[1 if x[0] == i else 0 for i in range(N)] for x in a]
[[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0]]