現在、配列内の最小値のインデックスの配列を持っています。
次のようになります。
[[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]])
あなたができるリストのために
>>> 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]]