var#
- ivy.var(x, /, *, axis=None, correction=0.0, keepdims=False, out=None)[source]#
Calculate the variance of the input array x.
Special Cases
Let N equal the number of elements over which to compute the variance.
If N - correction is less than or equal to 0, the variance is NaN.
If x_i is NaN, the variance is NaN (i.e., NaN values propagate).
- Parameters:
x (
Union
[Array
,NativeArray
]) – input array. Should have a floating-point data type.axis (
Optional
[Union
[int
,Sequence
[int
]]], default:None
) – axis or axes along which variances must be computed. By default, the variance must be computed over the entire array. If a tuple of integers, variances must be computed over multiple axes. Default:None
.correction (
Union
[int
,float
], default:0.0
) – degrees of freedom adjustment. Setting this parameter to a value other than 0 has the effect of adjusting the divisor during the calculation of the variance according to N-c where N corresponds to the total number of elements over which the variance is computed and c corresponds to the provided degrees of freedom adjustment. When computing the variance of a population, setting this parameter to 0 is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the unbiased sample variance, setting this parameter to 1 is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel’s correction). Default:0
.keepdims (
bool
, default:False
) – if True, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see Broadcasting). Otherwise, if False, the reduced axes (dimensions) must not be included in the result. Default:False
.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to.
- Return type:
- Returns:
ret – if the variance was computed over the entire array, a zero-dimensional array containing the variance; otherwise, a non-zero-dimensional array containing the variances. The returned array must have the same data type as x.
This method conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.
Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts
ivy.Container
instances in place of any of the arguments.Examples
With
ivy.Array
input:>>> x = ivy.array([0.1, 0.2, 0.3, 0.3, 0.9, 0.10]) >>> y = ivy.var(x) >>> print(y) ivy.array(0.07472222)
>>> x = ivy.array([0.1, 0.2, 0.3, 0.3, 0.9, 0.10]) >>> y = ivy.array(0.0) >>> ivy.var(x, out=y) >>> print(y) ivy.array(0.07472222)
>>> x = ivy.array([[0.1, 0.2, 0.3], [0.3, 0.9, 0.10]]) >>> print(ivy.var(x, axis=1, keepdims=True)) ivy.array([[0.00666667], [0.11555555]])
>>> x = ivy.array([[0.1, 0.2, 0.3], [0.3, 0.9, 0.10]]) >>> y = ivy.var(x, correction=1) >>> print(y) ivy.array(0.08966666)
With
ivy.Container
input: >>> x = ivy.Container(a=ivy.array([0.1, 0.2, 0.9]), … b=ivy.array([0.7, 0.1, 0.9])) >>> y = ivy.var(x) >>> print(y) {a: ivy.array(0.12666667), b: ivy.array(0.11555555)
}
- Array.var(self, /, *, axis=None, correction=0.0, keepdims=False, out=None)[source]#
ivy.Array instance method variant of ivy.var. This method simply wraps the function, and so the docstring for ivy.var also applies to this method with minimal changes.
Special Cases
Let N equal the number of elements over which to compute the variance.
If N - correction is less than or equal to 0, the variance is NaN.
If x_i is NaN, the variance is NaN (i.e., NaN values propagate).
- Parameters:
self (
Array
) – input array. Should have a floating-point data type.axis (
Optional
[Union
[int
,Sequence
[int
]]], default:None
) – axis or axes along which variances must be computed. By default, the variance must be computed over the entire array. If a tuple of integers, variances must be computed over multiple axes. Default:None
.correction (
Union
[int
,float
], default:0.0
) – degrees of freedom adjustment. Setting this parameter to a value other than 0 has the effect of adjusting the divisor during the calculation of the variance according to N-c where N corresponds to the total number of elements over which the variance is computed and c corresponds to the provided degrees of freedom adjustment. When computing the variance of a population, setting this parameter to 0 is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the unbiased sample variance, setting this parameter to 1 is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel’s correction). Default:0
.keepdims (
bool
, default:False
) – if True, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see Broadcasting). Otherwise, if False, the reduced axes (dimensions) must not be included in the result. Default:False
.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to.
- Return type:
Array
- Returns:
ret – if the variance was computed over the entire array, a zero-dimensional array containing the variance; otherwise, a non-zero-dimensional array containing the variances. The returned array must have the same data type as x.
Examples
>>> x = ivy.array([[0.0, 1.0, 2.0], ... [3.0, 4.0, 5.0], ... [6.0, 7.0, 8.0]]) >>> y = x.var() >>> print(y) ivy.array(6.6666665)
>>> x = ivy.array([[0.0, 1.0, 2.0], ... [3.0, 4.0, 5.0], ... [6.0, 7.0, .08]]) >>> y = x.var(axis=0) >>> print(y) ivy.array([6., 6., 4.1])
>>> x = ivy.array([[0.0, 1.0, 2.0], ... [3.0, 4.0, 5.0], ... [6.0, 7.0, .08]]) >>> y = ivy.array([0., 0., 0.]) >>> x.var(axis=1, out=y) >>> print(y) ivy.array([0.667, 0.667, 9.33 ])
- Container.var(self, /, *, axis=None, correction=0.0, keepdims=False, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#
ivy.Container instance method variant of ivy.var. This method simply wraps the function, and so the docstring for ivy.var also applies to this method with minimal changes.
- Parameters:
self (
Container
) – input container. Should have a floating-point data type.axis (
Optional
[Union
[int
,Sequence
[int
],Container
]], default:None
) – axis or axes along which variances must be computed. By default, the variance must be computed over the entire array for each array in the input container. If a tuple of integers, variances must be computed over multiple axes. Default:None
.correction (
Union
[int
,float
,Container
], default:0.0
) – degrees of freedom adjustment. Setting this parameter to a value other than 0 has the effect of adjusting the divisor during the calculation of the variance according to N-c where N corresponds to the total number of elements over which the variance is computed and c corresponds to the provided degrees of freedom adjustment. When computing the variance of a population, setting this parameter to 0 is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the unbiased sample variance, setting this parameter to 1 is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel’s correction). Default:0
.keepdims (
Union
[bool
,Container
], default:False
) – if True, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see Broadcasting). Otherwise, if False, the reduced axes (dimensions) must not be included in the result. Default:False
. input array. Should have a floating-point data type.key_chains (
Optional
[Union
[List
[str
],Dict
[str
,str
],Container
]], default:None
) – The key-chains to apply or not apply the method to. Default isNone
.to_apply (
Union
[bool
,Container
], default:True
) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue
.prune_unapplied (
Union
[bool
,Container
], default:False
) – Whether to prune key_chains for which the function was not applied. Default isFalse
.map_sequences (
Union
[bool
,Container
], default:False
) – Whether to also map method to sequences (lists, tuples). Default isFalse
.out (
Optional
[Container
], default:None
) – optional output, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Container
- Returns:
ret – a container containing different arrays depends on parameters. see below for the types of arrays in the returned container if the variance was computed over the entire array, a zero-dimensional array containing the variance; otherwise, a non-zero-dimensional array containing the variances. The returned container must have the same data type as self.
Examples
>>> x = ivy.Container(a=ivy.array([0.0, 1.0, 2.0]), ... b=ivy.array([3.0, 4.0, 5.0])) >>> y = x.var() >>> print(y) { a: ivy.array(0.6666667), b: ivy.array(0.6666667) }
>>> x = ivy.Container(a=ivy.array([0.0, 1.0, 2.0]), ... b=ivy.array([3.0, 4.0, 5.0])) >>> y = ivy.Container(a=ivy.array(0.), b=ivy.array(0.)) >>> x.var(out=y) >>> print(y) { a: ivy.array(0.6666667), b: ivy.array(0.6666667) }
>>> x = ivy.Container(a=ivy.array([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]]), ... b=ivy.array([[6.0, 7.0, 8.0], [9.0, 10.0, 11.0]])) >>> y = ivy.Container(a=ivy.array([0., 0., 0.]), b=ivy.array([0., 0., 0.])) >>> x.var(axis=0, out=y) >>> print(y) { a: ivy.array([2.25, 2.25, 2.25]), b: ivy.array([2.25, 2.25, 2.25]) }