我一直在寻找一种方法来计算给定列表中每个值的百分位排名,但迄今为止我一直没有成功。
例如:给定值列表
更新代码:
请问这是否正确,是否有更干净的库来实现这个功能?谢谢!
org.apache.commons.math3
提供了一种从值列表中获取第pth百分位数的方法,但我想要的是相反的。我想对列表中的每个值进行排名。有没有人知道一个库或一种在Apache commons math中实现这一点的方法?例如:给定值列表
{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
请问这是否正确,是否有更干净的库来实现这个功能?谢谢!