示例数据:
array(
[[ 1., 1.],
[ 2., 1.],
[ 0., 1.],
[ 0., 0.],
[ 0., 0.]])
希望达到的预期结果是:
>>> [0.,0.]
ie) 最常见的一对。
似乎不起作用的方法:
使用statistics
,因为numpy数组不可哈希。
使用scipy.stats.mode
,因为它返回每个轴上的众数,例如)对于我们的示例,它给出
mode=array([[ 0., 1.]])
示例数据:
array(
[[ 1., 1.],
[ 2., 1.],
[ 0., 1.],
[ 0., 0.],
[ 0., 0.]])
>>> [0.,0.]
ie) 最常见的一对。
似乎不起作用的方法:
使用statistics
,因为numpy数组不可哈希。
使用scipy.stats.mode
,因为它返回每个轴上的众数,例如)对于我们的示例,它给出
mode=array([[ 0., 1.]])
numpy
中的unique
函数来高效地完成此操作:pairs, counts = np.unique(a, axis=0, return_counts=True)
print(pairs[counts.argmax()])
[0. 0.]
。这与IT技术有关。通过标准库的一种方法是使用collections.Counter
。
这将为您提供最常见的一对和计数。在Counter.most_common()
上使用[0]
索引来检索最高计数。
import numpy as np
from collections import Counter
A = np.array(
[[ 1., 1.],
[ 2., 1.],
[ 0., 1.],
[ 0., 0.],
[ 0., 0.]])
c = Counter(map(tuple, A)).most_common()[0]
# ((0.0, 0.0), 2)
Counter
仅接受可哈希对象。
axis
参数仅在(相当新的)numpy
v.1.13中可用。 - jpp