Numpy中的數(shù)組搜索中np.where方法詳細(xì)介紹
numpy.where (condition[, x, y])
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,所以第一個輸出-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]])
上面這個例子的條件為[[True,False], [True,False]],分別對應(yīng)最后輸出結(jié)果的四個值。第一個值從[1,9]中選,因?yàn)闂l件為True,所以是選1。第二個值從[2,8]中選,因?yàn)闂l件為False,所以選8,后面以此類推。類似的問題可以再看個例子:
>>> 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) (等價于numpy.nonzero)。這里的坐標(biāo)以tuple的形式給出,通常原數(shù)組有多少維,輸出的tuple中就包含幾個數(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)] # 等價于 a[a>5] array([ 6, 8, 10]) >>> np.where([[0, 1], [1, 0]]) (array([0, 1]), array([1, 0]))
上面這個例子條件中[[0,1],[1,0]]的真值為兩個1,各自的第一維坐標(biāo)為[0,1],第二維坐標(biāo)為[1,0] 。
下面看個復(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會輸出每個元素的對應(yīng)的坐標(biāo),因?yàn)樵瓟?shù)組有三維,所以tuple中有三個數(shù)組。
補(bǔ)充
np.where和np.searchsorted同屬于Numpy數(shù)組搜索的一部分,這里先介紹簡單的where
import numpy as np a = np.array([1, 2, 3, 4, 5]) b = np.where(a == 5) print(b)
where方法將會返回一個元祖
(array([4]),)
此外還將介紹一個搜索奇數(shù)和偶數(shù)的方法(數(shù)組全都默認(rèn)使用最上面的a數(shù)組)
可見,簡單的判斷余數(shù)即可
c = np.where(a%2 == 0) print(c) d = np.where(a%2 == 1) print(d)
返回:
(array([1, 3]),) (array([0, 2, 4]),)
關(guān)于np.where方法到這里就結(jié)束啦
到此這篇關(guān)于Numpy中的數(shù)組搜索中np.where方法詳細(xì)介紹的文章就介紹到這了,更多相關(guān)Numpy np.where 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Miniconda安裝教程和虛擬環(huán)境的創(chuàng)建與使用
Anaconda 和 Miniconda 都是 Python 的發(fā)行版,它們提供了一個包管理系統(tǒng)和環(huán)境管理系統(tǒng),本文主要介紹了Miniconda安裝教程和虛擬環(huán)境的創(chuàng)建與使用,感興趣的可以了解一下2025-04-04
python使用selenium登錄QQ郵箱(附帶滑動解鎖)
這篇文章主要為大家詳細(xì)介紹了python使用selenium登錄QQ郵箱,帶滑動解鎖登錄功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
python網(wǎng)絡(luò)爬蟲采集聯(lián)想詞示例
這篇文章主要介紹了python網(wǎng)絡(luò)爬蟲采集聯(lián)想詞示例,需要的朋友可以參考下2014-02-02
使用Pycharm為項(xiàng)目創(chuàng)建一個虛擬環(huán)境完整圖文教程
這篇文章主要給大家介紹了關(guān)于使用Pycharm為項(xiàng)目創(chuàng)建一個虛擬環(huán)境的相關(guān)資料,我們在使用pycharm做項(xiàng)目時,最好給每一個工程都創(chuàng)建一個虛擬環(huán)境,將對應(yīng)的安裝包放在該虛擬環(huán)境中,避免項(xiàng)目與項(xiàng)目之間產(chǎn)生關(guān)系或沖突,便于管理,需要的朋友可以參考下2023-09-09
python tkinter中的錨點(diǎn)(anchor)問題及處理
這篇文章主要介紹了python tkinter中的錨點(diǎn)(anchor)問題及處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
使用pytorch和torchtext進(jìn)行文本分類的實(shí)例
今天小編就為大家分享一篇使用pytorch和torchtext進(jìn)行文本分類的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

