np.where()[0] 和 np.where()[1]的具體使用
本文主要介紹了np.where()[0] 和 np.where()[1]的具體使用,以及np.where()的具體用法,廢話不多說,具體如下:
import numpy as np a = np.arange(12).reshape(3,4) print('a:', a) print('np.where(a > 5):', np.where(a > 5)) print('a[np.where(a > 5)]:', a[np.where(a > 5)]) print('np.where(a > 5)[0]:', np.where(a > 5)[0]) print('np.where(a > 5)[1]:', np.where(a > 5)[1]) print(a[np.where(a > 5)[0], np.where(a > 5)[1]])
a: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] np.where(a > 5): (array([1, 1, 2, 2, 2, 2]), array([2, 3, 0, 1, 2, 3])) a[np.where(a > 5)]: [ 6 7 8 9 10 11] np.where(a > 5)[0]: [1 1 2 2 2 2] np.where(a > 5)[1]: [2 3 0 1 2 3] [ 6 7 8 9 10 11]
np.where()[0] 表示行索引,np.where()[1]表示列索引
numpy.where() 有兩種用法:
1. np.where(condition, x, y)
滿足條件(condition),輸出x,不滿足輸出y。
如果是一維數(shù)組,相當(dāng)于[xv if c else yv for (c,xv,yv) in zip(condition,x,y)]
>>> aa = np.arange(10) >>> np.where(aa,1,-1) array([-1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) # 0為False,所以第一個(gè)輸出-1 >>> np.where(aa > 5,1,-1) array([-1, -1, -1, -1, -1, -1, 1, 1, 1, 1]) >>> np.where([[True,False], [True,True]], # 官網(wǎng)上的例子 [[1,2], [3,4]], [[9,8], [7,6]]) array([[1, 8], [3, 4]])
上面這個(gè)例子的條件為[[True,False], [True,False]],分別對(duì)應(yīng)最后輸出結(jié)果的四個(gè)值。第一個(gè)值從[1,9]中選,因?yàn)闂l件為True,所以是選1。第二個(gè)值從[2,8]中選,因?yàn)闂l件為False,所以選8,后面以此類推。類似的問題可以再看個(gè)例子:
>>> a = 10 >>> np.where([[a > 5,a < 5], [a == 10,a == 7]], [["chosen","not chosen"], ["chosen","not chosen"]], [["not chosen","chosen"], ["not chosen","chosen"]]) array([['chosen', 'chosen'], ['chosen', 'chosen']], dtype='<U10')
2. np.where(condition)
只有條件 (condition),沒有x和y,則輸出滿足條件 (即非0) 元素的坐標(biāo) (等價(jià)于numpy.nonzero)。這里的坐標(biāo)以tuple的形式給出,通常原數(shù)組有多少維,輸出的tuple中就包含幾個(gè)數(shù)組,分別對(duì)應(yīng)符合條件元素的各維坐標(biāo)。
>>> a = np.array([2,4,6,8,10]) >>> np.where(a > 5) # 返回索引 (array([2, 3, 4]),) >>> a[np.where(a > 5)] # 等價(jià)于 a[a>5] array([ 6, 8, 10]) >>> np.where([[0, 1], [1, 0]]) (array([0, 1]), array([1, 0]))
上面這個(gè)例子條件中[[0,1],[1,0]]的真值為兩個(gè)1,各自的第一維坐標(biāo)為[0,1],第二維坐標(biāo)為[1,0] 。
下面看個(gè)復(fù)雜點(diǎn)的例子:
>>> a = np.arange(27).reshape(3,3,3) >>> a array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]) >>> np.where(a > 5) (array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]), array([2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2])) # 符合條件的元素為 [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]
所以np.where會(huì)輸出每個(gè)元素的對(duì)應(yīng)的坐標(biāo),因?yàn)樵瓟?shù)組有三維,所以tuple中有三個(gè)數(shù)組。
需要注意的一點(diǎn)是,輸入的不能直接是list,需要轉(zhuǎn)為array或者為array才行。比如range(10)和np.arange(10)后者返回的是數(shù)組,使用np.where才能達(dá)到效果。
到此這篇關(guān)于np.where()[0] 和 np.where()[1]的具體使用的文章就介紹到這了,更多相關(guān)np.where()[0] 和 np.where()[1]內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python 實(shí)現(xiàn)局域網(wǎng)遠(yuǎn)程屏幕截圖案例
這篇文章主要介紹了Python 實(shí)現(xiàn)局域網(wǎng)遠(yuǎn)程屏幕截圖案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03使用Python通過oBIX協(xié)議訪問Niagara數(shù)據(jù)的示例
這篇文章主要介紹了使用Python通過oBIX協(xié)議訪問Niagara數(shù)據(jù)的示例,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-12-12Python利用docx模塊實(shí)現(xiàn)快速操作word文件
這篇文章主要為大家詳細(xì)介紹了Python如何利用docx模塊實(shí)現(xiàn)快速操作word文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-09-09在PyCharm中找不到Conda創(chuàng)建的環(huán)境的解決方法
本文主要介紹了在PyCharm中找不到Conda創(chuàng)建的環(huán)境的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Python closure閉包解釋及其注意點(diǎn)詳解
這篇文章主要介紹了Python closure閉包解釋及其注意點(diǎn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08100行python代碼實(shí)現(xiàn)跳一跳輔助程序
這篇文章主要介紹了100行代碼實(shí)現(xiàn)跳一跳輔助程序,接下來要分享的是用“純軟件”的方法來玩“跳一跳”。本人只做過Android開發(fā),因此下面只給出Android平臺(tái)下的實(shí)現(xiàn)方法。需要的朋友可以參考下2018-01-01Python通過requests模塊實(shí)現(xiàn)抓取王者榮耀全套皮膚
只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不如實(shí)踐帶來的提升快,只有在實(shí)例中才能獲得能力的提升,本篇文章手把手帶你用Python實(shí)現(xiàn)抓取王者榮耀全套皮膚,大家可以在過程中查缺補(bǔ)漏,提升水平2021-10-10使用Python、TensorFlow和Keras來進(jìn)行垃圾分類的操作方法
這篇文章主要介紹了如何使用Python、TensorFlow和Keras來進(jìn)行垃圾分類,這個(gè)模型在測(cè)試集上可以達(dá)到約80%的準(zhǔn)確率,可以作為一個(gè)基礎(chǔ)模型進(jìn)行后續(xù)的優(yōu)化,需要的朋友可以參考下2023-05-05