比較和過濾NumPy陣列


NumPy庫擁有廣泛的工具,可以執行陣列的比較和過濾操作。陣列的比較將基於陣列的維度,逐元素地按行和按列進行。當我們想要檢索特定的陣列元素時,就可以應用過濾。

NumPy是Numerical Python的縮寫,用於對多維陣列和矩陣進行數學和科學計算。NumPy中提供了不同的函式和方法來執行陣列的過濾和比較。

比較NumPy陣列

以下是NumPy庫中可用於對陣列執行比較操作的方法。

  • equal()

  • greater()

  • array_equal()

  • all_close()

使用equal()方法

NumPy庫中的equal()方法逐元素比較兩個NumPy陣列,併為每次執行的比較返回布林輸出。詳細來說,當第一個陣列中的一個元素與其在第二個陣列中對應的元素進行比較時,如果它們相等則返回“True”值,否則返回“False”。此過程會對這兩個陣列中的所有元素重複進行。

所有獲得的這些布林值都儲存在另一個數組中,並顯示為輸出。如果輸出陣列包含所有“True”值,則稱這些陣列相同,否則為“False”。

示例

讓我們來看一個簡單的示例,使用equal()方法逐元素比較兩個NumPy陣列:

import numpy as np
a = np.array([1,2,3])
b = np.array([34,2,10])
out = np.equal(a,b)
print("The Boolean output of the equal function:",out)

輸出

以下是將equal函式應用於陣列後的輸出,我們可以觀察到布林輸出。

The Boolean output of the equal function: [False True False]

使用greater()方法

greater()方法用於逐元素比較任意兩個NumPy陣列。如果第一個陣列中的元素大於第二個陣列中對應的元素,則此方法返回True;否則返回False。

示例

在這個例子中,我們嘗試使用greater()方法比較兩個NumPy陣列的元素。每次比較後的返回值都儲存在另一個數組中。

import numpy as np
a = np.array([1,2,3])
b = np.array([34,2,10])
out = np.greater(a,b)
print("The Boolean output of the greater function:",out)

輸出

以下是將greater函式應用於兩個陣列後的輸出。

The Boolean output of the greater function: [False False False]

使用array_equal()方法

array_equal()方法用於逐元素比較兩個NumPy陣列,以檢查這兩個陣列是否相等。如果兩個陣列相等,則返回值為True;否則,返回值為False。

注意 - 只有當陣列中的所有元素都相等時,這兩個陣列才被認為是相等的。

示例

當我們將兩個形狀和大小相同的陣列傳遞給array_equal()函式時,輸出將採用布林格式。

import numpy as np
a = np.array([1,2,3])
b = np.array([34,2,10])
out = np.array_equal(a,b)
print("The Boolean output of the array_equal function:",out)

輸出

以下是array_equal()函式的輸出。

The Boolean output of the array_equal function: False

使用allclose()方法

allclose()方法逐元素比較兩個NumPy陣列,並檢查哪些元素彼此接近。

示例

在這個例子中,如果我們將兩個輸入陣列傳遞給allclose()函式,當元素接近時它將返回true,否則返回false。

import numpy as np
a = np.array([1,2,3])
b = np.array([3,4,2])
out = np.allclose(a,b)
print("The Boolean output of the allclose function:",out)

輸出

以下是allclose()函式的輸出,並返回布林輸出。

The Boolean output of the allclose function: False

過濾NumPy陣列

以下是用於對陣列執行過濾操作的函式。

  • 布林索引

  • where()方法

  • extract()方法

  • delete()方法

布林索引

布林索引允許我們根據布林條件從陣列中選擇元素,即僅提取滿足布林條件的元素。需要用陣列元素滿足的這個條件也稱為布林掩碼。

示例

在這個例子中,我們嘗試使用布林索引檢索陣列的過濾元素。為了過濾元素,我們將首先建立一個布林掩碼,條件為a > 2,然後提取滿足此條件的元素。

import numpy as np
a = np.array([34,2,10])
mask = a > 2
filtered_array = a[mask]
print("The output of the Boolean indexing:",filtered_array)

輸出

以下是將布林索引應用於陣列後的輸出。

The output of the Boolean indexing: [34 10]

使用where()方法

where()方法用於根據使用者給定的條件過濾陣列中的元素。它返回滿足給定條件的陣列元素的索引。

示例

下面的示例演示瞭如何使用where()方法過濾NumPy陣列中的元素。在這裡,我們將條件(a>=2)作為引數傳遞給where()方法,並且只有滿足此條件的值才會顯示在輸出陣列中。

import numpy as np
a = np.array([34,2,10])
filtered_array = np.where(a <=2)
print("The output of the where function :",filtered_array)

輸出

以下是將where函式應用於輸入陣列後的輸出。

The output of the where function : (array([1]),)

使用extract()方法

顧名思義,extract()方法提取所有滿足給定條件的元素。

示例

在這裡,我們將陣列和條件作為引數傳遞給extract()函式。預期提取此陣列中滿足給定條件的元素。

import numpy as np
a = np.array([34,2,10])
filtered_array = np.extract(a <=2, a)
print("The output of the extract function :",filtered_array)

輸出

以下是將extract函式應用於給定輸入陣列後的輸出。

The output of the extract function : [2]

使用delete()方法

delete()方法用於根據使用者指定的條件從NumPy陣列中刪除元素。

返回值將是刪除給定陣列中元素後的陣列,具體取決於條件。我們在此方法中使用的條件引數是從where()方法獲得的。

示例

在此示例中,當我們將輸入陣列以及條件(從where()方法獲得)傳遞給delete()方法時,將刪除滿足給定條件的陣列元素。

import numpy as np
a = np.array([34,2,10])
filtered_array = np.delete(a, np.where(a == 2))
print("The output of the delete function :",filtered_array)

輸出

以下是delete()函式的輸出。

The output of the delete function : [34 10]

更新於:2023年8月7日

438 次瀏覽

啟動您的職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.