【问题标题】:Removing elements from a list which are much larger or much smaller than most of the elements of list从列表中删除比列表的大多数元素大得多或小得多的元素
【发布时间】:2017-05-06 09:59:44
【问题描述】:

我有一些图像,其大小存储为元组列表,如下所示:

(79, 1100)
(51, 1100)
(56, 1100)
(49, 1100)
(47, 1100)
(44, 1100)
(54, 1100)
(15,1100)
(52, 1100)
(115, 1100)
(44, 1100)
(60, 1100)
(51, 1100)
(56, 1100)
(19,1100)
(110,1100)

我想要的是删除所有第一个维度比列表中的大多数元素更小/更大的图像(在本例中为 15,115,19,110)。在这里,我无法定义阈值,因为数字的范围(在第一维中)可能会根据我选择的图像比例而发生很大变化。

【问题讨论】:

  • 如果不设置一个“很多”的因素,您就无法真正定义“很多”。如果您想做到这一点,您可能只需使用与平均值的最大百分比差异。跨度>

标签: python python-2.7 list list-comprehension


【解决方案1】:

您可以使用图像尺寸的平均值和标准差来建立阈值。 让我们使用 numpy 来计算这些指标:

import numpy as np

#convert the list of tuples l_tuples to a numpy array:
l_new = np.array(l_tuples)

#calculate the mean and standard deviation of the first column
mean=np.mean(l_new,axis=0)[0]
std=np.std(l_new,axis=0)[0]

#create a mask to find all elements which are in the interval [mean-std, mean+std]
mask=[abs(x[0]-mean)<std for x in l_new]

#pictures you want to use:
accepted = l_new[mask]
#pictures you dont want to use:
remove = l_new[[not i for i in mask]]

如果您觉得某个标准差对您来说不是一个好的估计值,您可以轻松地将该指标换成不同的指标,例如平均值的固定百分比或其他指标。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 2022-01-14
    相关资源
    最近更新 更多