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,所以第一個輸出-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]中選,因為條件為True,所以是選1。第二個值從[2,8]中選,因為條件為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),因為原數(shù)組有三維,所以tuple中有三個數(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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Python中os.fork()產(chǎn)生子進(jìn)程的例子
今天小編就為大家分享一篇在Python中os.fork()產(chǎn)生子進(jìn)程的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
pymongo如何通過oplog獲取數(shù)據(jù)(mongodb)
使用MongoDB的oplog(操作日志)進(jìn)行數(shù)據(jù)同步是高級的用法,主要用于復(fù)制和故障恢復(fù),這篇文章主要介紹了pymongo通過oplog獲取數(shù)據(jù)(mongodb),需要的朋友可以參考下2023-09-09
Python爬蟲基礎(chǔ)之XPath語法與lxml庫的用法詳解
這篇文章主要給大家介紹了關(guān)于Python爬蟲基礎(chǔ)之XPath語法與lxml庫用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
python實現(xiàn)超市管理系統(tǒng)(后臺管理)
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)超市管理系統(tǒng),增加后臺管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10
Django提示mysql版本過低:django.db.utils.NotSupportedError: My
這篇文章主要介紹了Django提示mysql版本過低:django.db.utils.NotSupportedError: MySQL 8 or later is required (found 5.7.26).的解決方法,文中有詳細(xì)的解決方案,具有一定的參考價值,需要的朋友可以參考下2024-03-03
一篇文章搞懂Python程序流程控制結(jié)構(gòu)
這篇文章主要給大家介紹了關(guān)于Python程序流程控制結(jié)構(gòu)的相關(guān)資料,本節(jié)學(xué)習(xí)了Python程序的控制結(jié)構(gòu)之順序結(jié)構(gòu)、分支結(jié)構(gòu)、循環(huán)結(jié)構(gòu),文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
Python3.7實現(xiàn)中控考勤機(jī)自動連接
這篇文章主要為大家詳細(xì)介紹了Python3.7實現(xiàn)中控考勤機(jī)自動連接,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08

