查找范围内模式值的方法

问题描述 投票:1回答:1

我在此结构中有一个numpy维度值数组:

arr = array([[3067,   78, 3172,  134],
             [3237,   89, 3394,  128],
             [3475,   87, 3743,  141],
             [3763,   86, 3922,  131],
             [3238,  147, 3259,  154]])

基本上存储屏幕上数据的位置,其中值表示为:[x_left, y_top, x_right, y_bottom]]

我只需要处理x_left值,因为我试图找到页面上我最有可能找到这些对象的位置。

我知道scipy.mode,它将返回模式值。有没有一种方法可以返回多个模式,例如给定numpy列中的前10个模式值?更好的是,有没有一种使用模式的方式,以使模式在给定范围内?例如,上面的行的x_left32373238值非常接近对齐。有没有一种方法可以将这两个值计算为单个模式值?

python numpy scipy
1个回答
1
投票

您可以将numpy数组列转换为熊猫系列并使用.value_counts()

import pandas as pd
x_left = pd.Series(arr[:,0])
x_left.value_counts()
#3475    1
#3237    1
#3067    1
#3763    1
#3238    1
#dtype: int64

您还可以将值舍入为例如最接近的10个整数,以将范围之间的值分组。>

def customRound(x, base=10):
    return base * round(x/base)
x_left_round = x_left.apply(customRound)
x_left_round.value_counts()
#3240    2
#3760    1
#3070    1
#3480    1
#dtype: int64

然后您会看到您有两个接近3240的x_left

© www.soinside.com 2019 - 2024. All rights reserved.