jax.numpy.var — JAX documentation (original) (raw)

jax.numpy.var#

jax.numpy.var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *, where=None, correction=None)[source]#

Compute the variance along a given axis.

JAX implementation of numpy.var().

Parameters:

Returns:

An array of the variance along the given axis.

Return type:

Array

See also

Examples

By default, jnp.var computes the variance along all axes.

x = jnp.array([[1, 3, 4, 2], ... [5, 2, 6, 3], ... [8, 4, 2, 9]]) with jnp.printoptions(precision=2, suppress=True): ... jnp.var(x) Array(5.74, dtype=float32)

If axis=1, variance is computed along axis 1.

jnp.var(x, axis=1) Array([1.25 , 2.5 , 8.1875], dtype=float32)

To preserve the dimensions of input, you can set keepdims=True.

jnp.var(x, axis=1, keepdims=True) Array([[1.25 ], [2.5 ], [8.1875]], dtype=float32)

If ddof=1:

with jnp.printoptions(precision=2, suppress=True): ... print(jnp.var(x, axis=1, keepdims=True, ddof=1)) [[ 1.67] [ 3.33] [10.92]]

To include specific elements of the array to compute variance, you can usewhere.

where = jnp.array([[1, 0, 1, 0], ... [0, 1, 1, 0], ... [1, 1, 1, 0]], dtype=bool) with jnp.printoptions(precision=2, suppress=True): ... print(jnp.var(x, axis=1, keepdims=True, where=where)) [[2.25] [4. ] [6.22]]