我的目标是获取一个 np.array 列表并创建一个关联的列表或数组,将每个列表或数组分类为是否有重复。这是我认为可行的方法:
www = [np.array([1, 1, 1]), np.array([1, 1, 1]), np.array([2, 1, 1])]
uniques, counts = np.unique(www, axis = 0, return_counts = True)
counts = [1 if x > 1 else 0 for x in counts]
count_dict = dict(zip(uniques, counts))
[count_dict[i] for i in www]
这种情况下所需的输出是:
[1, 1, 0]
因为第一个和第二个元素在原始列表中有另一个副本。问题似乎是我无法使用 np.array
作为字典的键。
建议?
最佳答案
首先将 www
转换为 2D Numpy 数组,然后执行以下操作:
In [18]: (counts[np.where((www[:,None] == uniques).all(2))[1]] > 1).astype(int)
Out[18]: array([1, 1, 0])
这里我们使用广播来检查所有 www
行与 uniques
数组的相等性,然后在最后一个轴上使用 all()
来查找其中哪些行完全等于 uniques
行。
以下是详细结果:
In [20]: (www[:,None] == uniques).all(2)
Out[20]:
array([[ True, False],
[ True, False],
[False, True]])
# Respective indices in `counts` array
In [21]: np.where((www[:,None] == uniques).all(2))[1]
Out[21]: array([0, 0, 1])
In [22]: counts[np.where((www[:,None] == uniques).all(2))[1]] > 1
Out[22]: array([ True, True, False])
In [23]: (counts[np.where((www[:,None] == uniques).all(2))[1]] > 1).astype(int)
Out[23]: array([1, 1, 0])
关于python - 将 np.arrays 分类为重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56990655/