我有一个总计为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)中这样做,所以在熊猫中使用逻辑是更好的。非常感谢
我认为您可以将
outlier detection
逻辑用于您的用例。 您可以计算输入列表的IQR
并应用以下公式:outlier= input1 < q1-1.5*IQR | input1 >q3+1.5*IQR
相同的代码:
您可以根据自己的喜好更改分位数,并检查可能的最佳结果
如果你现在想要一个简陋的方式。你可以简化这个过程,但我可以提示你一个技工。例如:
也适用于其他两个示例。这取决于你的标准。再一次,这只是一种粗糙的方式来向你展示和研究
更新: 我在想这个问题。需要对更大的列表或某些值进行更多测试,但也可以通过调整条件(在我的示例i[0]/i<;2中)对列表进行理解
我相信你要找的东西叫做percentiles。AFAIK最常见的是50%(又名中位数)和90%。可以使用numpy.percentile计算百分位:
输出:
由您选择百分位数的值
相关问题 更多 >
编程相关推荐