我已经创建了一种计算方差的方法,但是,我只是说我有一个逻辑错误。我没有获得方差,而是输出为Infinity。
见输出:
============ DataSet Test ================================
10 Data Items
[5.55, 47.9, 21.27, 6.59, 42.96, 74.3, 99.17, 50.99, 77.36, 62.76]
Minimum: 5.55 at index 0
Maximum: 99.17 at index 6
Data Range: 93.62
Median : 56.875
Mean : 48.885000000000005
Variance : Infinity
Std Dev : 0.0
以下是代码:
//Return the variance of the points in this DataSet
public double variance()
{
double[] meanVar = this.data;
for (int k = 0; k < meanVar.length; k++)
{
meanVar[k] = meanVar[k] - mean();
meanVar[k] = Math.pow(meanVar[k],2.0);
}
double variance = 0.0;
for (int k = 0; k < meanVar.length; k++)
variance = variance + meanVar[k];
variance = variance / size();
return variance;
}
答案 0 :(得分:1)
size()
方法return
之前的variance()
是什么?
也许你的意思是meanVar.length
。