本文主要介紹了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]表示列索引
滿足條件(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]],分別對應(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')
只有條件 (condition),沒有x和y,則輸出滿足條件 (即非0) 元素的坐標(biāo) (等價(jià)于numpy.nonzero)。這里的坐標(biāo)以tuple的形式給出,通常原數(shù)組有多少維,輸出的tuple中就包含幾個(gè)數(shù)組,分別對應(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會輸出每個(gè)元素的對應(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:西寧 石家莊 文山 錫林郭勒盟 昆明 懷化 梅州 浙江
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《np.where()[0] 和 np.where()[1]的具體使用》,本文關(guān)鍵詞 np.where,和,的,具體,使用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。