在列表中查找与其他元素相比权重最高的元素

2024-09-20 23:00:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个总计为100(百分比)的值列表。我需要找到与其他值相比,构成最高百分比的值。如何确定筛选数据的标准?帮我解释一下逻辑

以下是一些示例和预期输出:

input1 = [46.34, 42.42, 5.11, 2.16, 1.23, 1.19, 0.48, 0.4, 0.22, 0.22, 0.09, 0.04, 0.04, 0.04] 
output1 = [46.34, 42.42]

input2 = [32.98, 31.82, 9.76, 3.21, 1.18, 0.43, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11] 
output2 = [32.98, 31.82]

input3 = [37.72, 30.66, 30.66, 0.72, 0.24] 
output3 = [37.72, 30.66, 30.66]

列表已排序。这不是一个“顶n元素”问题。我不能仅仅从列表中选择(例如:前2名或前3名)元素

p.S.p.S:我在pandas(groupby)中这样做,所以在熊猫中使用逻辑是更好的。非常感谢


Tags: 数据元素示例列表标准排序逻辑百分比
3条回答

我认为您可以将outlier detection逻辑用于您的用例。 您可以计算输入列表的IQR并应用以下公式: outlier= input1 < q1-1.5*IQR | input1 >q3+1.5*IQR

相同的代码:

q1=pd.Series(input1).quantile(0.25)
q3=pd.Series(input1).quantile(0.75)

IQR=q3-q1
output=list(pd.Series(input1)[(input1< (q1 - 1.5 * IQR)) |(input1 > (q3 + 1.5 * IQR))])
output
[46.34, 42.42, 5.11]

您可以根据自己的喜好更改分位数,并检查可能的最佳结果

如果你现在想要一个简陋的方式。你可以简化这个过程,但我可以提示你一个技工。例如:

input3 = [37.72, 30.66, 30.66, 0.72, 0.24] 
output3 = []
output3.append(input3[0])
input3.pop(0)
for i in input3:
    if output3[0] / i <2:
        output3.append(i)
    else:
        continue

print(output3)
[37.72, 30.66, 30.66] 

也适用于其他两个示例。这取决于你的标准。再一次,这只是一种粗糙的方式来向你展示和研究

更新: 我在想这个问题。需要对更大的列表或某些值进行更多测试,但也可以通过调整条件(在我的示例i[0]/i<;2中)对列表进行理解

input1 = [46.34, 42.42, 5.11, 2.16, 1.23, 1.19, 0.48, 0.4, 0.22, 0.22, 0.09, 0.04, 0.04, 0.04]
input2 = [32.98, 31.82, 9.76, 3.21, 1.18, 0.43, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11]
input3 = [37.72, 30.66, 30.66, 0.72, 0.24]

output1 = [x if x != input1[0] else x for x in input1 if input1[0] / x <2]
output2 = [x if x != input2[0] else x for x in input2 if input2[0] / x <2]
output3 = [x if x != input3[0] else x for x in input3 if input3[0] / x <2]

result:
[46.34, 42.42]
[32.98, 31.82]
[37.72, 30.66, 30.66]

我相信你要找的东西叫做percentiles。AFAIK最常见的是50%(又名中位数)和90%。可以使用numpy.percentile计算百分位:

import numpy as np
def filterByPercentile(data, percentile):
    percentile = np.percentile(data,percentile)
    return data[data >= percentile]

input1 = np.array([46.34, 42.42, 5.11, 2.16, 1.23, 1.19, 0.48, 0.4, 0.22, 0.22, 0.09, 0.04, 0.04, 0.04])
input2 = np.array([32.98, 31.82, 9.76, 3.21, 1.18, 0.43, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11])
input3 = np.array([37.72, 30.66, 30.66, 0.72, 0.24])

print("median:")
print(filterByPercentile(input1,50))
print(filterByPercentile(input2,50))
print(filterByPercentile(input3,50))

print("90%:")
print(filterByPercentile(input1,90))
print(filterByPercentile(input2,90))
print(filterByPercentile(input3,90))

输出:

median:
[46.34 42.42  5.11  2.16  1.23  1.19  0.48]
[32.98 31.82  9.76  3.21  1.18  0.43]
[37.72 30.66 30.66]
90%:
[46.34 42.42]
[32.98 31.82]
[37.72]

由您选择百分位数的值

相关问题 更多 >

    热门问题