Python學習筆記:numpy選擇符合條件數據:select、where、choose、nonzero


一、np.select函數

1.介紹

np.select 函數根據某些條件篩選某些元素。

使用語法為:

import numpy as np
np.select(condlist, choicelist, default=0)
# 返回列表

參數(必須寫成“列表”的形式):

condlist -- 操作數據所依據的條件
choiselist -- 根據condlist條件所需要執行的操作
default -- 不滿足條件所執行的操作

2.傳統循環方法

使用循環、條件判斷的方法執行效率低下,可用 select 替代完成。

a = np.array([1,2,3,4,5,6,7,8,9,10])
result = []
for i in a:
    if i < 6:
        i += 10
    else:
        i = 100
    result.append(i)
print(result)
# [11, 12, 13, 14, 15, 100, 100, 100, 100, 100]

3.單條件

a = np.array([1,2,3,4,5,6,7,8,9,10])
result2 = np.select([a < 6], [a + 10], default=100)
print(result2)
# array([ 11,  12,  13,  14,  15, 100, 100, 100, 100, 100])

對應元素滿足條件執行操作,否則返回默認值。

4.多條件、多操作

a = np.array([[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]])
b = np.array(range(25)).reshape(5,5) + 1
result2 = np.select([a<6, np.logical_and(a>10, a<16), a>20],
                    [a+10, a**2, a*10],
                    default=100)
result2
'''
array([[ 11,  12,  13,  14,  15],
       [100, 100, 100, 100, 100],
       [121, 144, 169, 196, 225],
       [100, 100, 100, 100, 100],
       [210, 220, 230, 240, 250]])
'''

每個條件中,對應為真才執行相應的操作,針對所有條件都不滿足元素,執行默認值default。

# 同時滿足
result3 = np.select([a<12, np.logical_and(a>10, a<16), a>20],
                    [a+10, a**2, a*10],
                    default=100)
result3
# 觀察元素11
'''
array([[ 11,  12,  13,  14,  15],
       [ 16,  17,  18,  19,  20],
       [ 21, 144, 169, 196, 225],
       [100, 100, 100, 100, 100],
       [210, 220, 230, 240, 250]])
'''

同時滿足多個條件下,優先執行條件一、條件二,依次選擇。

二、np.where函數

1.介紹

np.where 函數實現滿足條件,輸出x,不滿足條件輸出y。

使用語法為:

np.where(condition, x, y)

2.提供3個參數

如果全部數組都是一維數組,則等價於:

[xv if c else yv for c, xv, yv in zip(condition, x, y)]

一維數組實例

a = np.arange(10) # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.where(a, 1, -1) # array([-1,  1,  1,  1,  1,  1,  1,  1,  1,  1])
np.where(a > 5, a, a*10) # array([ 0, 10, 20, 30, 40, 50,  6,  7,  8,  9])

多維數組同樣可以使用,取滿足條件的對應元素。

condition = [[True, False],
            [True, True]]
x = [[1, 2], [3, 4]]
y = [[9, 8], [7, 6]]
np.where(condition, x, y)
'''
array([[1, 8],
       [3, 4]])
'''

3.僅有condition參數

缺失x和y參數的情況下,則輸出滿足條件(非0)元素的坐標,等價於 np.asarray(condition).nonzero()

# 廣播機制 broadcast
a = np.array([2,4,6,8,10])
np.where(a > 5) # (array([2, 3, 4], dtype=int64),)
a[np.where(a > 5)] # array([ 6,  8, 10])

多維數組

a = np.arange(27).reshape(3,3,3)
'''
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],
       dtype=int64),
 array([2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2],
       dtype=int64),
 array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
       dtype=int64))
'''

a[np.where(a >5)]
# array([ 6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26])

np.where 輸出每個元素對應的坐標,原始數據是三維數組,則輸出3個數組的tuple。

三、np.choose函數

1.介紹

同樣的,np.choose 實現根據條件選擇相關元素,相比較 for if else 執行效率更高。

使用語法:

np.choose(a, choices, out=None, mode='raise')

參數:

a -- int類型數組 0~(n-1)之間的數
choices -- 被操作的數組 與a同維度
out -- 可選 接收運算結果的數組
mode -- 默認raise 表示數組元素不能超過n
         clip 元素小於0 變為0 大於n-1 變為n-1
         wrap value mode n 余數

2.實操

  • 當a和choices相同維數(1維)
result = np.array([0,0,0,0,0])
a = np.choose([4,2,1,3,0], [11,22,33,44,55], out=result)
print(a) # array([55, 33, 22, 44, 11])
print(result) # array([55, 33, 22, 44, 11])

元素個數代表的是 choices 中的索引 index。

  • 當a和choices相同維數(2維)
d = np.choose([[4,2,1,3,0],[3,4,2,0,1],[0,2,1,4,3]],
             [[11,22,33,32,31],[44,55,66,65,64],[77,88,99,98,97],[111,222,333,332,331],[444,555,666,665,664]])
 
print(d)
'''
[[444  88  66 332  31]
 [111 555  99  32  64]
 [ 11  88  66 665 331]]
'''

內外層索引匹配。

  • 當a的維數多於choices時
b = np.choose([[4,2,1,3,0],[3,4,2,0,1],[0,2,1,4,3]],[11,22,33,44,55])
print(b)
'''
[[55 33 22 44 11]
 [44 55 33 11 22]
 [11 33 22 55 44]]
'''

  • 當a的維數少於choices時
c = np.choose([4,2,1,3,0],
              [[11,22,33,32,31],[44,55,66,65,64],[77,88,99,98,97],[111,222,333,332,331],[444,555,666,665,664]])
print(c) # [444  88  66 332  31]

choices 最外層索引index與a匹配,內層索引默認從0開始,0、1、2、3、4、5逐漸遞增的。

鑒於此,choices的內層元素數量依然要與a的個數進行匹配才行,否則會報錯。

四、np.nonzero函數

np.nonzero 函數用於得到數組中非零元素的位置(數組索引)。

返回的索引值數組是一個2維tuple數組,該tuple數組中包含一維的array數組。

x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
print(x)
np.nonzero(x) # (array([0, 1, 2, 2], dtype=int64), array([0, 1, 0, 1], dtype=int64))
x[np.nonzero(x)] # array([3, 4, 5, 6])

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
np.nonzero(a > 3)
'''
(array([1, 1, 1, 2, 2, 2], dtype=int64),
 array([0, 1, 2, 0, 1, 2], dtype=int64))
'''

參考鏈接:numpy高級函數操作之——select、choose

參考鏈接:numpy.select

參考鏈接:numpy.where() 用法詳解

參考鏈接:numpy.where

參考鏈接:numpy.nonzero


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM