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ù)組,相當于[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]],分別對應最后輸出結果的四個值。第一個值從[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) 元素的坐標 (等價于numpy.nonzero)。這里的坐標以tuple的形式給出,通常原數(shù)組有多少維,輸出的tuple中就包含幾個數(shù)組,分別對應符合條件元素的各維坐標。
>>> 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,各自的第一維坐標為[0,1],第二維坐標為[1,0] 。
下面看個復雜點的例子:
>>> 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會輸出每個元素的對應的坐標,因為原數(shù)組有三維,所以tuple中有三個數(shù)組。
需要注意的一點是,輸入的不能直接是list,需要轉為array或者為array才行。比如range(10)和np.arange(10)后者返回的是數(shù)組,使用np.where才能達到效果。
到此這篇關于np.where()[0] 和 np.where()[1]的具體使用的文章就介紹到這了,更多相關np.where()[0] 和 np.where()[1]內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在Python中os.fork()產(chǎn)生子進程的例子
今天小編就為大家分享一篇在Python中os.fork()產(chǎn)生子進程的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
pymongo如何通過oplog獲取數(shù)據(jù)(mongodb)
使用MongoDB的oplog(操作日志)進行數(shù)據(jù)同步是高級的用法,主要用于復制和故障恢復,這篇文章主要介紹了pymongo通過oplog獲取數(shù)據(jù)(mongodb),需要的朋友可以參考下2023-09-09
python實現(xiàn)超市管理系統(tǒng)(后臺管理)
這篇文章主要為大家詳細介紹了python實現(xiàn)超市管理系統(tǒng),增加后臺管理,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下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).的解決方法,文中有詳細的解決方案,具有一定的參考價值,需要的朋友可以參考下2024-03-03

