亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用帶有布爾數組的 np.infs 將數組的所有索引歸零?

如何使用帶有布爾數組的 np.infs 將數組的所有索引歸零?

Helenr 2021-10-05 16:20:25
使用ord()。word = input("Enter a word: ")for letter in word:    print(letter + ': ', ord(letter))
查看完整描述

1 回答

?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

輸入:


In [77]: X    

Out[77]: 

array([[inf,  1., inf],

       [inf,  2.,  4.],

       [ 3.,  4.,  5.]])


In [78]: M 

Out[78]: 

array([[1, 0, 0],

       [0, 1, 0],

       [0, 1, 0]])

方法


首先,我們需要反轉掩碼M,然后使用numpy.where; 有了這些索引,我們就可以將原始數組中的元素設置為零,方法如下:


# inverting the mask

In [59]: M_not = np.logical_not(M)


In [80]: M_not

Out[80]: 

array([[False,  True,  True],

       [ True, False,  True],

       [ True, False,  True]])


# get the indices where `True` exists in array `M_not`

In [81]: indices = np.where(M_not) 


In [82]: indices 

Out[82]: (array([0, 0, 1, 1, 2, 2]), array([1, 2, 0, 2, 0, 2]))


# zero out the elements

In [84]: X[indices] = 0


In [61]: X 

Out[61]: 

array([[inf, 0., 0.],

       [0., 2., 0.],

       [0., 4., 0.]])

PS反轉掩碼不應理解為矩陣反轉。應該理解為翻轉布爾值(True--> False; False--> True)


查看完整回答
反對 回復 2021-10-05
  • 1 回答
  • 0 關注
  • 232 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號