NumPy - 查詢唯一行



在 NumPy 陣列中查詢唯一行

在 NumPy 中,陣列可以包含多行資料,有時您可能需要識別唯一行,這意味著這些行在陣列中只出現一次。查詢唯一行涉及根據其內容確定哪些行與其他行不同。

在 NumPy 中,我們可以使用unique() 函式來實現此目的。

使用 union1d() 函式

np.unique() 函式通常用於查詢陣列中唯一的元素。當與axis引數一起使用時,它可以用來查詢唯一的行。以下是語法:

numpy.unique(a, axis=None, return_index=False, return_inverse=False, return_counts=False)

其中:

  • a - 輸入陣列。
  • axis - 查詢唯一值的軸。對於行,設定為 0。
  • return_index - 確定是否返回第一次出現的索引。
  • return_inverse - 確定是否返回可以重建陣列的索引。
  • return_counts - 確定是否返回唯一值的計數。

示例:查詢一維陣列中的唯一元素

np.unique() 函式最簡單的用法是在一維陣列中查詢唯一元素:

import numpy as np

# Define a 1D array with duplicate values
array = np.array([1, 2, 2, 3, 4, 4, 5])

# Find unique elements
unique_elements = np.unique(array)

print("Unique Elements:\n", unique_elements)

獲得以下輸出:

Unique Elements:
[1 2 3 4 5]

示例:二維陣列中的唯一行

在下面的示例中,我們使用 unique() 函式檢索二維陣列中的唯一行,刪除任何重複的行:

import numpy as np

# Define an array with duplicate rows
array = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [1, 2, 3],
    [7, 8, 9]
])

# Find unique rows
unique_rows = np.unique(array, axis=0)

print("Unique Rows:\n", unique_rows)

這將產生以下結果:

Unique Rows:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

查詢具有索引的唯一行

透過在 unique() 函式中將return_index引數設定為True,我們可以在 NumPy 中找到原始陣列中唯一行的索引。

示例

在這個例子中,我們使用 unique() 函式查詢唯一行及其索引:

import numpy as np

# Define an array with duplicate rows
array = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [1, 2, 3],
    [7, 8, 9]
])

# Find unique rows and their indices
unique_rows, indices = np.unique(array, axis=0, return_index=True)

print("Unique Rows:\n", unique_rows)
print("Indices of Unique Rows:\n", indices)

以上程式碼的輸出如下:

Unique Rows:
[[1 2 3]
 [4 5 6]
 [7 8 9]]
Indices of Unique Rows:
[0 1 3]

重建原始陣列

如果您需要根據唯一行重建原始陣列,您可以使用 np.unique() 函式返回的索引,並將return_inverse引數設定為True。反向索引可以用來從唯一值映射回原始資料。

示例

在這個例子中,我們使用 unique() 函式識別 NumPy 陣列中的唯一行及其原始索引。然後我們使用這些索引重建陣列,以驗證唯一行與原始陣列(不包含重複項)匹配:

import numpy as np

# Define an array with duplicate rows
array = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [1, 2, 3],
    [7, 8, 9]
])

# Find unique rows and their indices
unique_rows, indices = np.unique(array, axis=0, return_index=True)

# Reconstruct the original array using the indices
reconstructed_array = array[np.sort(indices)]

print("Reconstructed Array:\n", reconstructed_array)

獲得的輸出如下所示:

Reconstructed Array:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

計數唯一行

除了查詢唯一行之外,您可能還希望計算每行在陣列中出現的次數。在 NumPy 中,您可以透過在 unique() 函式中將return_counts引數設定為True來實現此目的。

這在處理多維陣列時特別有用,其中每一行代表一個記錄或觀察。

示例

在下面的示例中,我們使用 unique() 函式檢索原始陣列中每行唯一行的計數:

import numpy as np

# Define an array with duplicate rows
array = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [1, 2, 3],
    [7, 8, 9]
])

# Find unique rows and their counts
unique_rows, counts = np.unique(array, axis=0, return_counts=True)

print("Unique Rows:\n", unique_rows)
print("Counts of Each Row:\n", counts)

執行上述程式碼後,我們將獲得以下輸出:

Unique Rows:
[[1 2 3]
 [4 5 6]
 [7 8 9]]
Counts of Each Row:
[2 1 1]

多維陣列

對於多維陣列,您可以使用 np.unique() 函式透過將axis引數設定為0來查詢唯一行。要處理所有維度上的唯一值,您可以使用預設設定。

示例

在下面的示例中,我們將 3D 陣列展平為 2D,然後使用 unique() 函式查詢唯一行:

import numpy as np

# Define a 3D array
array = np.array([
    [[1, 2], [3, 4]],
    [[1, 2], [5, 6]],
    [[1, 2], [3, 4]]
])

# Flatten the 3D array to 2D for uniqueness check
array_2d = array.reshape(-1, array.shape[-1])

# Find unique rows in the flattened array
unique_rows = np.unique(array_2d, axis=0)

print("Unique Rows in 3D Array:\n", unique_rows)

產生的結果如下:

Unique Rows in 3D Array:
[[1 2]
 [3 4]
 [5 6]]
廣告