Python 3.x 计算多维numpy数组中的副本
我使用的是python-3.x,我想计算numpy数组中的重复数。。。。例如:Python 3.x 计算多维numpy数组中的副本,python-3.x,numpy,multidimensional-array,duplicates,Python 3.x,Numpy,Multidimensional Array,Duplicates,我使用的是python-3.x,我想计算numpy数组中的重复数。。。。例如: import numpy as np my_array = np.array([[2, 3, 5], [2, 3, 5], # duplicate of row 0 (this will be count as 1) [2, 3, 5], # duplicate of row 0 (this will be count as 2)
import numpy as np
my_array = np.array([[2, 3, 5],
[2, 3, 5], # duplicate of row 0 (this will be count as 1)
[2, 3, 5], # duplicate of row 0 (this will be count as 2)
[1, 0, 9],
[3, 6, 6],
[3, 6, 6], # duplicate of row 0 (this will be count as 3)
[1, 0, 9]])
我想从outptu中得到的是此数组中的重复数:
the number of the duplicate is 3
大多数方法都返回值,如collections.Counter或return\u counts,如果我正确使用它们,它们不会返回我想要的值
任何建议都将不胜感激您可以通过获取数组长度-数组唯一成员的长度来获取数组的重复计数:
the_number_of the duplicate = len(my_array) - len(np.unique(my_array, axis=0))
你的例子的结果是4([1,0,9]也是重复的)。这里与@Anh Ngoc的答案略有不同。(对于较早版本的numpy,
np.unique
不支持axis
)
非常感谢,我不认为这会像你的回答那么简单:)非常感谢你的建议:)
number_of_duplicates = len(my_array) - len(set(map(tuple, my_array)))