如何在java中计算数组列的标准偏差?

wbgh16ku  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(372)

这是一个数组:

int[][] array1 = {
                      {1,2},
                      {3,4},
                      {5,6},
                      {7,8},
                      {9,10}
                 };

我的目标是使用上面的数组查找列的标准偏差:

public double stdevCol(int[][] array, int col)
{

}

这是我的总标准偏差函数:

public double stdev2D(int[][] array)
{
    int counter =0;
    double mean = mean2D(array);
    double sumOfSq =0;
    for(int i = 0; i<array.length; i++)
    {
        for(int j = 0 ; j< array[i].length; j++)
        {
            sumOfSq += Math.pow(array[i][j] - mean, 2.0);
            counter++;
        }
    }
    return Math.sqrt(sumOfSq/counter);
}

我的问题是:如果使用总数作为参考点,我会切换而不是 array[i].length(array,col).length ?
实验代码

{
    int counter =0;
    double mean = meanCol(array);
    double sumOfSq =0;
    for(int i = 0; i<array[col].length; i++)
    {
        sumOfSq += Math.pow(array[col].length - mean, 2.0);
        counter++;
    }
    return Math.sqrt(sumOfSq/counter);
}
piah890a

piah890a1#

import java.io.*;

class GFG {

// Function for calculating
// variance
static double variance(double a[], int n) {
    // Compute mean (average of elements)
    double sum = 0;

    for (int i = 0; i < n; i++){
        sum += a[i];
    }
    double mean = (double)sum /(double)n;

    // Compute sum squared differences with mean.
    double sqDiff = 0;
    for (int i = 0; i < n; i++){
        sqDiff += (a[i] - mean) *(a[i] - mean);
     }
    return (double)sqDiff / n;
}

static double standardDeviation(double arr[], int n) {
    return Math.sqrt(variance(arr, n));
}

// Driver Code
public static void main (String[] args) {

double arr[] = {600, 470, 170, 430, 300};
int n = arr.length;

System.out.println( "Variance: " + variance(arr, n));
System.out.println ("Standard Deviation: " + standardDeviation(arr, n));

}
}

当然,我刚刚更改了他的格式

相关问题