1 回答

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊
這應(yīng)該可以解決問題:
a = np.array([[1, 0.5, 0.3, 0, 0.2],
[0, 1, 0.2, 0.8, 0],
[0, 1, 1, 0.3, 0],
[0, 0, 0, 1, 0]])
# Create an array of ones the same size as a
b = np.ones_like(a)
# Fill the diagonal of b with NaN
np.fill_diagonal(b, np.nan)
# Multiply the arrays in order to remove the index column from the max
c = a*b
# Find the index of the max value of every row (excluding the index value)
np.nanargmax(c, axis=1)
輸出:
array([1, 3, 1, 0])
為了過濾掉每個(gè)值都為零的情況(因此“沒有最大值”,正如您定義的那樣),您必須做一些額外的工作。
添加回答
舉報(bào)