我已经创建了一种计算方差的方法,但是,可以说我有逻辑错误。而不是获得方差,我得到的输出为无穷大。

查看输出:

============ 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;
 }

最佳答案

size()方法中return之前的variance()是什么?

也许您的意思是meanVar.length

09-28 07:22