在 numpy 数组中查找模式的最有效方法 [英] Most efficient way to find mode in numpy array

查看:39
本文介绍了在 numpy 数组中查找模式的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含整数(正数或负数)的二维数组.每一行代表特定空间站点随时间变化的值,而每一列代表给定时间不同空间站点的值.

I have a 2D array containing integers (both positive or negative). Each row represents the values over time for a particular spatial site, whereas each column represents values for various spatial sites for a given time.

所以如果数组是这样的:

So if the array is like:

1 3 4 2 2 7
5 2 2 1 4 1
3 3 2 2 1 1

结果应该是

1 3 2 2 2 1

注意当mode有多个值时,任意一个(随机选择)都可以设置为mode.

Note that when there are multiple values for mode, any one (selected randomly) may be set as mode.

我可以一次迭代查找模式一的列,但我希望 numpy 可能有一些内置函数来做到这一点.或者如果有一个技巧可以在不循环的情况下有效地找到它.

I can iterate over the columns finding mode one at a time but I was hoping numpy might have some in-built function to do that. Or if there is a trick to find that efficiently without looping.

推荐答案

检查 scipy.stats.mode()(灵感来自@tom10 的评论):

Check scipy.stats.mode() (inspired by @tom10's comment):

import numpy as np
from scipy import stats

a = np.array([[1, 3, 4, 2, 2, 7],
              [5, 2, 2, 1, 4, 1],
              [3, 3, 2, 2, 1, 1]])

m = stats.mode(a)
print(m)

输出:

ModeResult(mode=array([[1, 3, 2, 2, 1, 1]]), count=array([[1, 2, 2, 2, 1, 2]]))

如您所见,它同时返回模式和计数.您可以通过m[0]直接选择模式:

As you can see, it returns both the mode as well as the counts. You can select the modes directly via m[0]:

print(m[0])

输出:

[[1 3 2 2 1 1]]

这篇关于在 numpy 数组中查找模式的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆