查找NumPy数组中的唯一行
在这篇文章中,我们将讨论如何在NumPy数组中找到唯一的行。为了在NumPy数组中找到唯一的行,我们使用NumPy库的numpy.unique()函数。
语法: numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)
现在,让我们看一个例子。
示例 1:
# 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')
输出:
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:
# 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]]