Link Search Menu Expand Document

getVariance()

In statistics, variance is the expectation of squared deviation of a random variable.

\[Var(X) = E[(X-\mu)^2]\]

Import

import * as datacook from '@pipcook/datacook';
const { getVariance } = datacook.Stat;

Syntax

getVariance(xData: Tensor | RecursiveArray<number>, axis = 0): Tensor

Parameters

Parametertypedescription
xDataTensor | RecursiveArray<number>input data
axis optionalnumberaxis to compute, default=-1, which means calculation will be applied across all axes. If input data is one-dimensional, this parameter will have no effect

Usage

«««< HEAD

Variance calculation for one-dimensinoal data:

829aa619a4d47da4f9345de21739fbe5230f0e0e

const x = [ 1, 2, 3, 4, 5 ];
const y = getVariance(x);
y.print();
/**
Tensor
    2.5
**/

«««< HEAD

Usage

======= Variance calculation for two-dimensinoal data:

const x =  [ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8, 9, 10 ] ];
const v1 = getVariance(x, 0);
v1.print();
/**
 * Tensor
 * [12.5, 12.5, 12.5, 12.5, 12.5]
 ** /
const x =  [ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8, 9, 10 ] ];
const v2 = getVariance(x, 1);
v2.print();
/**
 * Tensor
 * [2.5, 2.5]
 ** /

829aa619a4d47da4f9345de21739fbe5230f0e0e