📜  矩阵的方差和标准差

📅  最后修改于: 2021-05-06 22:40:38             🧑  作者: Mango

先决条件–均值,方差和标准偏差,数组的方差和标准偏差
给定大小为n * n的矩阵。我们必须计算给定矩阵的方差和标准差。
例子 :

Input : 1 2 3
        4 5 6
        6 6 6 
Output : variance: 3
         deviation: 1

Input : 1 2 3
        4 5 6
        7 8 9 
Output : variance: 6
         deviation: 2  

解释:
应该通过将矩阵的每个元素的总和相加来计算第一均值。计算出均值后,应将其从矩阵的每个元素中减去,然后对每个项求平方并通过将总和除以总和来找出方差
偏差:它是方差的平方根。
例子:

1 2 3
      4 5 6
      7 8 9

此处的均值为5 ,方差约为6.66

下面是代码实现:

C++
// CPP program to find mean and
// variance of a matrix.
#include 
using namespace std;
 
// variance function declaration
int variance(int, int, int);
 
// Function for calculating mean
int mean(int a[][3], int n)
{
    // Calculating sum
    int sum = 0;
    for (int  i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            sum += a[i][j];
     
    // Returning mean
    return sum / (n * n);
}
 
// Function for calculating variance
int variance(int a[][3], int n, int m)
{
    int sum = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
 
            // subtracting mean from elements
            a[i][j] -= m;
 
            // a[i][j] = fabs(a[i][j]);
            // squaring each terms
            a[i][j] *= a[i][j];
        }
    }
 
    // taking sum
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            sum += a[i][j];   
 
    return sum / (n * n);
}
 
// driver program
int main()
{
    // declaring and initializing matrix
    int mat[3][3] = { { 1, 2, 3 },
                      { 4, 5, 6 },
                      { 7, 8, 9 } };
 
    // for mean
    int m = mean(mat, 3);
 
    // for variance
    int var = variance(mat, 3, m);
 
    // for standard deviation
    int dev = sqrt(var);
 
    // displaying variance and deviation
     cout << "Mean: " << m << "\n"
          << "Variance: " << var << "\n"
          << "Deviation: " << dev << "\n";
    return 0;
}


Java
// Java program to find mean
// and variance of a matrix.
import java.io.*;
 
class GFG
{
// Function for
// calculating mean
static int mean(int a[][],
                int n)
{
    // Calculating sum
    int sum = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            sum += a[i][j];
     
    // Returning mean
    return sum / (n * n);
}
 
// Function for
// calculating variance
static int variance(int a[][],
                    int n, int m)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
 
            // subtracting mean
            // from elements
            a[i][j] -= m;
 
            // a[i][j] = fabs(a[i][j]);
            // squaring each terms
            a[i][j] *= a[i][j];
        }
    }
 
    // taking sum
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            sum += a[i][j];
 
    return sum / (n * n);
}
 
// Driver Code
public static void main (String[] args)
{
 
// declaring and
// initializing matrix
int mat[][] = {{1, 2, 3},
               {4, 5, 6},
               {7, 8, 9}};
 
// for mean
int m = mean(mat, 3);
 
// for variance
int var = variance(mat, 3, m);
 
// for standard
// deviation
double dev = (int)Math.sqrt(var);
 
// displaying variance
// and deviation
System.out.println("Mean: " + m);
System.out.println("Variance: " +
                            var);
System.out.println("Deviation: " +
                        (int)dev);
}
}
 
// This code is contributed
// by akt_mit


Python3
# Python3 program to find mean
# and variance of a matrix.
import math;
 
# variance function declaration
# Function for calculating mean
def mean(a, n):
 
    # Calculating sum
    sum = 0;
    for i in range(n):
        for j in range(n):
            sum += a[i][j];
     
    # Returning mean
    return math.floor(int(sum / (n * n)));
 
# Function for calculating variance
def variance(a, n, m):
    sum = 0;
    for i in range(n):
        for j in range(n):
 
            # subtracting mean
            # from elements
            a[i][j] -= m;
 
            # a[i][j] = fabs(a[i][j]);
            # squaring each terms
            a[i][j] *= a[i][j];
 
    # taking sum
    for i in range(n):
        for j in range(n):
            sum += a[i][j];
 
    return math.floor(int(sum / (n * n)));
 
# Driver Code
 
# declaring and
# initializing matrix
mat = [[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]];
 
# for mean
m = mean(mat, 3);
 
# for variance
var = variance(mat, 3, m);
 
# for standard deviation
dev = math.sqrt(var);
 
# displaying variance
# and deviation
print("Mean:", m);
print("Variance:", var);
print("Deviation:", math.floor(dev));
 
# This code is contributed by mits


C#
// C# program to find mean
// and variance of a matrix.
using System;
 
class GFG
{
     
// Function for
// calculating mean
static int mean(int [,]a,
                int n)
{
    // Calculating sum
    int sum = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            sum += a[i, j];
     
    // Returning mean
    return sum / (n * n);
}
 
// Function for
// calculating variance
static int variance(int [,]a,
                    int n, int m)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
 
            // subtracting mean
            // from elements
            a[i, j] -= m;
 
            // a[i][j] = fabs(a[i][j]);
            // squaring each terms
            a[i, j] *= a[i, j];
        }
    }
 
    // taking sum
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            sum += a[i,j];
 
    return sum / (n * n);
}
 
// Driver Code
static public void Main ()
{
 
// declaring and
// initializing matrix
int [,]mat = {{1, 2, 3},
              {4, 5, 6},
              {7, 8, 9}};
 
// for mean
int m = mean(mat, 3);
 
// for variance
int var = variance(mat, 3, m);
 
// for standard deviation
double dev = (int)Math.Sqrt(var);
 
// displaying variance and deviation
Console.WriteLine("Mean: " + m );
    Console.WriteLine("Variance: " + var);
    Console.WriteLine("Deviation: " + dev);
}
}
 
// This code is contributed by ajit


PHP


Javascript


输出 :
Mean: 5
Variance: 6
Deviation: 2