查找每列中出现次数最多的元素的最简单方法 [英] Simplest way to find the element that occurs the most in each column
本文介绍了查找每列中出现次数最多的元素的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有
data =
[[a, a, c],
[b, c, c],
[c, b, b],
[b, a, c]]
我想获得一个包含每个列中出现次数最多的元素的列表:result = [b, a, c]
,最简单的方法是什么?
I want to get a list containing the element that occurs the most in each column: result = [b, a, c]
, what is the easiest way to do that ?
我使用Python 2.6.6
I use Python 2.6.6
推荐答案
在统计信息中,您想要的称为 mode . scipy库( http://www.scipy.org/)在scipy.stats
.
In statistics, what you want is called the mode. The scipy library (http://www.scipy.org/) has a mode
function, in scipy.stats
.
In [32]: import numpy as np
In [33]: from scipy.stats import mode
In [34]: data = np.random.randint(1,6, size=(6,8))
In [35]: data
Out[35]:
array([[2, 1, 5, 5, 3, 3, 1, 4],
[5, 3, 2, 2, 5, 2, 5, 3],
[2, 2, 5, 3, 3, 2, 1, 1],
[2, 4, 1, 5, 4, 4, 4, 5],
[4, 4, 5, 5, 2, 4, 4, 4],
[2, 4, 1, 1, 3, 3, 1, 3]])
In [36]: val, count = mode(data, axis=0)
In [37]: val
Out[37]: array([[ 2., 4., 5., 5., 3., 2., 1., 3.]])
In [38]: count
Out[38]: array([[ 4., 3., 3., 3., 3., 2., 3., 2.]])
这篇关于查找每列中出现次数最多的元素的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文