python 统计矩阵中出现元素最多的值

  • np.bincount只能针对一维数据
a = np.array([1,2,3,4,2])
b = np.bincount(a)
b
Out[95]: array([0, 1, 2, 1, 1], dtype=int64)
np.argmax(b)

---------------------------------------------------------------
Out[96]: 2

这样就得到了矩阵a中出现次数最多的元素。

a = np.array([1,2,5,6,2])
b = np.bincount(a)
b
Out[99]: array([0, 1, 2, 0, 0, 1, 1], dtype=int64)
np.argmax(b)

--------------------------------------------------------------------
Out[100]: 2

由此可见,b中的值的意义是,比如位置1上的值为1,表示1在a中出现了一次。位置6上的值为1,表示6在a中出现了一次。

 

参考链接:https://blog.csdn.net/VictoriaW/article/details/72834428?utm_source=blogxgwz6

你可能感兴趣的:(python基础)