[英]Creating a matrix where each element is equal to the minimum of its row and column index
[英]Vectorized implementation to get index of minimum element in each row
我有一個像這樣的numpy.ndarray:
array([[ 11.18033989, 0. ],
[ 8.24621125, 3. ],
[ 13.03840481, 5. ],
[ 6. , 5.38516481],
[ 11.18033989, 3.16227766],
[ 0. , 11.18033989],
[ 8.06225775, 4.24264069]])
我想得到一個新的數組A,這樣A [i]是上面矩陣的第i行中最小元素的索引。 如: array([1, 1, 1, 1, 1, 0, 1])
我可以使用argmin for for循環,但由於我希望這個算法可擴展,我正在尋找一種使用矢量化實現的方法。 我猜numpy會提供這樣的功能,但我是numpy的新手,所以我不知道在哪里看。
如果X
是你的數組,
X.argmin(axis=1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.