獲得從元組位置矩陣(np.ndarray)我有一些零隨機值成從np.where
x=np.array([[1,2,3,0],[4,0,5,0],[7,0,0,0],[0,9,8,0]])
>>> x
array([[1, 2, 3, 0],
[4, 0, 5, 0],
[7, 0, 0, 0],
[0, 9, 8, 0]])
矩陣而想要隨機值成僅是不爲零的位置。我可以從np.where
pos = np.where(x!=0)
>>> (array([0, 0, 0, 1, 1, 2, 3, 3], dtype=int64), array([0, 1, 2, 0, 2, 0, 1, 2], dtype=int64))
得到(行,列)位置的元組是否有用於從pos
位置矩陣x
np.random
(或別的東西),只有在不改變其中爲零的方式嗎?
# pseudocode
new_x = np.rand(x, at pos)
那麼您可以使用非零()方法來查找所有非零索引!結帳答案中的細節! – DexJ