gpt4 book ai didi

java - 计算列表中每个值的百分位数

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:09:50 25 4
gpt4 key购买 nike

我一直在寻找一种方法来计算给定列表中每个值的百分位排名,但到目前为止我一直没有成功。

org.apache.commons.math3 为您提供了一种从值列表中获取第 p 个百分位数的方法,但我想要的恰恰相反。我想对列表中的每个值进行排名。有没有人知道 Apache 公共(public)数学中的库或方法可以实现这一点?

例如:给定一个值列表 {1,2,3,4,5},我希望每个值的百分位等级最大为 99 或 100最小值为 0 或 1。

更新代码:

public class TestPercentile {

public static void main(String args[]) {
double x[] = { 10, 11, 12, 12, 12, 12, 15, 18, 19, 20 };
calculatePercentiles(x);
}

public static void calculatePercentiles(double[] arr) {
for (int i = 0; i < arr.length; i++) {
int count = 0;
int start = i;
if (i > 0) {
while (i > 0 && arr[i] == arr[i - 1]) {
count++;
i++;
}
}
double perc = ((start - 0) + (0.5 * count));
perc = perc / (arr.length - 1);
for (int k = 0; k < count + 1; k++)
System.out.println("Percentile for value " + (start + k + 1)
+ " = " + perc * 100);
}
}}

Sample Output:
Percentile for value 1 = 0.0
Percentile for value 2 = 11.11111111111111
Percentile for value 3 = 22.22222222222222
Percentile for value 4 = 50.0
Percentile for value 5 = 50.0
Percentile for value 6 = 50.0
Percentile for value 7 = 50.0
Percentile for value 8 = 77.77777777777779
Percentile for value 9 = 88.88888888888889
Percentile for value 10 = 100.0

有人可以告诉我这是否正确,是否有一个库可以更干净地执行此操作?

谢谢!

最佳答案

这实际上取决于您对百分位数的定义。以下是使用 NaturalRanking 的解决方案并重新缩放到 0-1 间隔。很高兴 NaturalRanking 有一些处理相等值的策略,并且已经实现了 nans。

import java.util.Arrays;
import org.apache.commons.math3.stat.ranking.NaNStrategy;
import org.apache.commons.math3.stat.ranking.NaturalRanking;
import org.apache.commons.math3.stat.ranking.TiesStrategy;

public class Main {

public static void main(String[] args) {
double[] arr = {Double.NaN, 10, 11, 12, 12, 12, 12, 15, 18, 19, 20};

PercentilesScaledRanking ranking = new PercentilesScaledRanking(NaNStrategy.REMOVED, TiesStrategy.MAXIMUM);
double[] ranks = ranking.rank(arr);

System.out.println(Arrays.toString(ranks));
//prints:
//[0.1, 0.2, 0.6, 0.6, 0.6, 0.6, 0.7, 0.8, 0.9, 1.0]
}
}

class PercentilesScaledRanking extends NaturalRanking {

public PercentilesScaledRanking(NaNStrategy nanStrategy, TiesStrategy tiesStrategy) {
super(nanStrategy, tiesStrategy);
}

@Override
public double[] rank(double[] data) {
double[] rank = super.rank(data);
for (int i = 0; i < rank.length; i++) {
rank[i] = rank[i] / rank.length;
}
return rank;
}
}

关于java - 计算列表中每个值的百分位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20480674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com