📌  相关文章
📜  在 NumPy 数组中查找唯一行

📅  最后修改于: 2022-05-13 01:55:21.705000             🧑  作者: Mango

在 NumPy 数组中查找唯一行

在本文中,我们将讨论如何在 NumPy 数组中查找唯一行。为了在 NumPy 数组中查找唯一行,我们使用 NumPy 库的numpy.unique()函数。

现在,让我们看一个例子:

示例 1:

Python3
# import library
import numpy as np
  
# Create a 2D numpy array
arr2D = np.array([[11, 11, 12, 11],
                     [13, 11, 12, 11],
                     [16, 11, 12, 11],
                     [11, 11, 12, 11]])
  
print('Original Array :' ,
      arr2D, sep = '\n')
  
# Get unique rows from
# complete 2D-array by 
# passing axis = 0 in 
# unique function along
# with 2D-array
uniqueRows = np.unique(arr2D, 
                       axis = 0)
  
# print the output result
print('Unique Rows:',
      uniqueRows, sep = '\n')


Python3
# import library
import numpy as np
  
# create 2d numpy array
array = np.array([[1, 2, 3, 4],
                  [3, 2, 4, 1],
                  [6, 8, 1, 2]])
  
print("Original array: \n",
      array)
  
# Get unique rows from
# complete 2D-array by 
# passing axis = 0 in 
# unique function along
# with 2D-array
uniqueRows = np.unique(array, 
                       axis = 0)
  
# print the output result
print('Unique Rows :',
      uniqueRows,
      sep = '\n')


输出:

Original Array :
[[11 11 12 11]
[13 11 12 11]
[16 11 12 11]
[11 11 12 11]]
Unique Rows:
[[11 11 12 11]
[13 11 12 11]
[16 11 12 11]]

示例 2:

Python3

# import library
import numpy as np
  
# create 2d numpy array
array = np.array([[1, 2, 3, 4],
                  [3, 2, 4, 1],
                  [6, 8, 1, 2]])
  
print("Original array: \n",
      array)
  
# Get unique rows from
# complete 2D-array by 
# passing axis = 0 in 
# unique function along
# with 2D-array
uniqueRows = np.unique(array, 
                       axis = 0)
  
# print the output result
print('Unique Rows :',
      uniqueRows,
      sep = '\n')

输出:

Original array: 
 [[1 2 3 4]
 [3 2 4 1]
 [6 8 1 2]]
Unique Rows :
[[1 2 3 4]
 [3 2 4 1]
 [6 8 1 2]]