第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

迭代 numpy 數(shù)組以在離開行索引的子數(shù)組中找到最大值

迭代 numpy 數(shù)組以在離開行索引的子數(shù)組中找到最大值

森林海 2021-12-09 14:41:38
我想找到沿軸 = 0 的二維數(shù)組的最大值,我不想在行索引處包含該值。我對(duì)這個(gè)解決方案不滿意,因?yàn)槲倚枰谝话偃f行上運(yùn)行它,而且我不想在這里使用 for 循環(huán)。我試過numpy.argmax但它計(jì)算行的最大值,包括行索引處的值。我的二維數(shù)組Arry=([[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]])預(yù)期產(chǎn)出[1, 3, 1]第一行 [1, 0.5, 0.3, 0, 0.2] 在索引 1 處具有最大值,即 0.5,因?yàn)橹?1 在行索引 0 處,同樣在第二行中最大值為 0.8,即索引 3,第 4 行沒有有任何最大值,因?yàn)槎紴榱阄业拇aimport numpy as npfor idx,subarry in enumerate(Arry):    newlist=np.delete(subarry, idx)    idx_min=min(np.where(subarry==np.max(newlist))[0])    if idx_min != 0: min_elem_idx.append(idx_min)print(min_elem_idx)[1, 3, 1]我正在尋找一種 Pythonic 的方式來實(shí)現(xiàn)這一點(diǎn)而不使用 for 循環(huán)
查看完整描述

1 回答

?
不負(fù)相思意

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è)值都為零的情況(因此“沒有最大值”,正如您定義的那樣),您必須做一些額外的工作。


查看完整回答
反對(duì) 回復(fù) 2021-12-09
  • 1 回答
  • 0 關(guān)注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)