std#

ivy.std(x, /, *, axis=None, correction=0.0, keepdims=False, out=None)[source]#

Calculate the standard deviation of the input array x.

Special Cases

Let N equal the number of elements over which to compute the standard deviation.

  • If N - correction is less than or equal to 0, the standard deviation is NaN.

  • If x_i is NaN, the standard deviation is NaN (i.e., NaN values propagate).

Parameters:
  • x (Union[Array, NativeArray]) – input array.

  • axis (Optional[Union[int, Sequence[int]]], default: None) – axis or axes along which standard deviations must be computed. By default, the standard deviation must be computed over the entire array. If a tuple of integers, standard deviations 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 standard deviation according to N-c where N corresponds to the total number of elements over which the standard deviation is computed and c corresponds to the provided degrees of freedom adjustment. When computing the standard deviation 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 corrected sample standard deviation, 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 standard deviation was computed over the entire array, a zero-dimensional array containing the standard deviation; otherwise, a non-zero-dimensional array containing the standard deviations. The returned array must have the same data type as x.

    Note

    While this specification recommends that this function only accept input arrays having a real-valued floating-point data type, specification-compliant array libraries may choose to accept input arrays having an integer data type. While mixed data type promotion is implementation-defined, if the input array x has an integer data type, the returned array must have the default real-valued floating-point data type.

  • This function conforms to the `Array API Standard

  • <https (//data-apis.org/array-api/latest/>`_. This docstring is an extension of the)

  • `docstring <https (//data-apis.org/array-api/latest/)

  • API_specification/generated/array_api.std.html>`_

  • 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

>>> x = ivy.array([-1., 0., 1.])
>>> y = ivy.std(x)
>>> print(y)
ivy.array(0.81649661)
>>> x = ivy.array([-1., 0., 1.])
>>> z = ivy.std(x, correction=1)
>>> print(z)
ivy.array(1.)
>>> x = ivy.array([[0., 4.]])
>>> y = ivy.std(x, keepdims=True)
>>> print(y)
ivy.array([[2.]])
>>> x = ivy.array([2., 1.])
>>> y = ivy.array(0.)
>>> ivy.std(x, out=y)
>>> print(y)
ivy.array(0.5)
>>> x = ivy.array([[-1., -2.], [3., 3.]])
>>> y = ivy.std(x, axis=1)
>>> print(y)
ivy.array([0.5, 0. ])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([-1., 0., 1.]), b=ivy.array([1.1, 0.2, 1.4]))
>>> y = x.std()
>>> print(y)
{
    a: ivy.array(0.81649661),
    b: ivy.array(0.509902)
}
>>> x = ivy.Container(a=ivy.array([[1., 3.], [3., 6.]]),
...                   b=ivy.array([[ 4., 2.], [2., 1.]]))
>>> y = x.std(axis=1, keepdims=True)
>>> print(y)
{
    a: ivy.array([[1.],
                  [1.5]]),
    b: ivy.array([[1.],
                  [0.5]])
}
Array.std(self, /, *, axis=None, correction=0.0, keepdims=False, out=None)[source]#

ivy.array instance method variant of ivy.std. This method simply wraps the function, and so the docstring for ivy.std also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array.

  • axis (Optional[Union[int, Sequence[int]]], default: None) – axis or axes along which standard deviation must be computed. By default, the product must be computed over the entire array. If a tuple of integers, products 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 standard deviation according to N-c where N corresponds to the total number of elements over which the standard deviation is computed and c corresponds to the provided degrees of freedom adjustment. When computing the standard deviation 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 corrected sample standard deviation, 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) – bool, 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 – container, if the product was computed over the entire array, a zero-dimensional array containing the product; otherwise, a non-zero-dimensional array containing the products. The returned array must have the same data type as self.

Examples

With: class: ivy.Array input:

>>> x = ivy.array([-1., 0., 1.])
>>> y = x.std()
>>> print(y)
ivy.array(0.81649661)
>>> x = ivy.array([-1., 0., 1.])
>>> z = x.std(correction=1)
>>> print(z)
ivy.array(1.)
>>> x = ivy.array([[0., 4.]])
>>> y = x.std(keepdims=True)
>>> print(y)
ivy.array([[2.]])
>>> x = ivy.array([2., 1.])
>>> y = ivy.array(0.)
>>> x.std(out=y)
>>> print(y)
ivy.array(0.5)
>>> x = ivy.array([[-1., -2.], [3., 3.]])
>>> y = x.std(axis=1)
>>> print(y)
ivy.array([0.5, 0. ])
Container.std(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.std. This method simply wraps the function, and so the docstring for ivy.std also applies to this method with minimal changes.

Parameters:
  • self (Container) – input container.

  • axis (Optional[Union[int, Sequence[int], Container]], default: None) – axis or axes along which standard deviation must be computed. By default, the product must be computed over the entire array. If a tuple of integers, products 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 standard deviation according to N-c where N corresponds to the total number of elements over which the standard deviation is computed and c corresponds to the provided degrees of freedom adjustment. When computing the standard deviation 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 corrected sample standard deviation, 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) – bool, 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[Container], default: None) – optional output array, for writing the result to.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • out – optional output, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – container, if the standard deviation was computed over the entire array, a zero-dimensional array containing the standard deviation; otherwise, a non-zero-dimensional array containing the respectve standard deviations. The returned array must have the same data type as self.

Examples

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0., 2.]), b=ivy.array([-4., 5.]))
>>> y = x.std()
>>> print(y)
{
    a: ivy.array(1.),
    b: ivy.array(4.5)
}
>>> x = ivy.Container(a=ivy.array([0.1, 1.1]), b=ivy.array([0.1, 1.1, 2.1]))
>>> y = x.std(keepdims=True)
>>> print(y)
{
    a: ivy.array([0.5]),
    b: ivy.array([0.81649649])
}
>>> x = ivy.Container(a=ivy.array([[2., 1.]]), b=ivy.array([[2., -2.]]))
>>> y = x.std(axis=1, keepdims=True)
>>> print(y)
{
    a: ivy.array([[0.5]]),
    b: ivy.array([[2.]])
}
>>> x = ivy.Container(a=ivy.array([-1., 1., 1.]), b=ivy.array([1.1, 0.2, 1.4]))
>>> x.std(out=x)
>>> print(x)
{
    a: ivy.array(0.94280904),
    b: ivy.array(0.509902)
}
>>> x = ivy.Container(a=ivy.array([0., -2., 1.]), b=ivy.array([1., 1., 1.]))
>>> y = ivy.Container(a=ivy.array(0.), b=ivy.array(0.))
>>> x.std(out=y)
>>> print(y)
{
    a: ivy.array(1.2472192),
    b: ivy.array(0.)
}
>>> x = ivy.Container(a=ivy.array([[-1., 1., 2.], [2., 2., 2.]]),
...                   b=ivy.array([[3., 0., -3.], [4., 1., 4.]]))
>>> y = x.std(axis=1)
>>> print(y)
{
    a: ivy.array([1.2472192, 0.]),
    b: ivy.array([2.44948983, 1.41421354])
}