我需要帮助如何计算三分位数。我的数据集示例:
ImmutableList<Double> DataSet = ImmutableList.of(25.0, 100.0, 0.0, 144.0, 9.0, 121.0, 4.0, 225.0, 169.0, 64.0, 49.0, 16.0, 36.0, 1.0, 81.0, 196.0);
double[] dataset = Doubles.toArray(DataSet);
我尝试使用 Google Guava计算中位数,但我不知道如何用它来计算三分位数:
import java.util.Map;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Doubles;
import com.google.common.math.Quantiles;
import com.google.common.math.Quantiles.ScaleAndIndexes;
public class Main {
public static void main(String[] args) {
ImmutableList<Double> DataSet = ImmutableList.of(25.0, 100.0,
0.0, 144.0, 9.0, 121.0, 4.0, 225.0, 169.0, 64.0, 49.0, 16.0, 36.0, 1.0, 81.0, 196.0);
double[] dataset = Doubles.toArray(DataSet);
double median = Quantiles.median().compute(dataset);
System.out.println(median);
}
}
最佳答案
三分位数是 3 分位数。 Guava 有这个方法:
public static Quantiles.Scale scale(int scale)
Specifies the computation of q-quantiles.
Parameters:
scale - the scale for the quantiles to be calculated, i.e. the q of the q-quantiles, which must be positive
所以你可以这样做
Quantiles.scale(3).compute(...)
更新
Guava(和 Google)似乎有自己的三方数概念。它考虑实际值,并根据值而不是索引查找三分位数。
如果这不是你想要的,并且你需要的只是普通的基于索引的三分位数,那么你实际上并不需要一个库,只需自己编写它(显然你需要首先对数据进行排序,然后采取考虑到极端情况):
static double[] terciles(double[] data) {
data = data.clone();
Arrays.sort(data);
int len = data.length;
if (len < 2)
throw new IllegalArgumentException();
double result[] = new double[2];
result[0] = data[len/3];
result[1] = data[2*len/3];
return result;
}
关于java - 从对象列表中查找三分位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62173946/