larray.Array.std_by

Array.std_by(*axes_and_groups, dtype=None, ddof=1, out=None, skipna=None, keepaxes=False, **explicit_axes)[source]

Compute the sample standard deviation.

Normalized by N-1 by default. This can be changed using the ddof argument.

Parameters
*axes_and_groupsNone or int or str or Axis or Group or any combination of those

The standard deviation is performed along all axes except the given one(s). For groups, standard deviation is performed along groups and non associated axes. The default (no axis or group) is to perform the standard deviation over all the dimensions of the input array.

An axis can be referred by:

  • its index (integer). Index can be a negative integer, in which case it counts from the last to the first axis.

  • its name (str or AxisReference). You can use either a simple string (‘axis_name’) or the special variable X (X.axis_name).

  • a variable (Axis). If the axis has been defined previously and assigned to a variable, you can pass it as argument.

You may not want to perform the standard deviation over a whole axis but over a selection of specific labels. To do so, you have several possibilities:

  • ([‘a1’, ‘a3’, ‘a5’], ‘b1, b3, b5’) : labels separated by commas in a list or a string

  • (‘a1:a5:2’) : select labels using a slice (general syntax is ‘start:end:step’ where is ‘step’ is optional and 1 by default).

  • (a=’a1, a2, a3’, X.b[‘b1, b2, b3’]) : in case of possible ambiguity, i.e. if labels can belong to more than one axis, you must precise the axis.

  • (‘a1:a3; a5:a7’, b=’b0,b2; b1,b3’) : create several groups with semicolons. Names are simply given by the concatenation of labels (here: ‘a1,a2,a3’, ‘a5,a6,a7’, ‘b0,b2’ and ‘b1,b3’)

  • (‘a1:a3 >> a123’, ‘b[b0,b2] >> b12’) : operator ‘ >> ‘ allows to rename groups.

dtypedtype, optional

The data type of the returned array. Defaults to None (the dtype of the input array).

ddofint, optional

“Delta Degrees of Freedom”: the divisor used in the calculation is N - ddof, where N represents the number of elements. Defaults to 1.

outArray, optional

Alternate output array in which to place the result. It must have the same shape as the expected output and its type is preserved (e.g., if dtype(out) is float, the result will consist of 0.0’s and 1.0’s). Axes and labels can be different, only the shape matters. Defaults to None (create a new array).

skipnabool, optional

Whether to skip NaN (null) values. If False, resulting cells will be NaN if any of the aggregated cells is NaN. Defaults to True.

keepaxesbool or label-like, optional

Whether reduced axes are left in the result as dimensions with size one. If True, reduced axes will contain a unique label representing the applied aggregation (e.g. ‘sum’, ‘prod’, …). It is possible to override this label by passing a specific value (e.g. keepaxes=’summation’). Defaults to False.

Returns
Array or scalar

Examples

>>> arr = ndtest((2, 8), dtype=float)
>>> arr[:,:] = [[0, 3, 5, 6, 4, 2, 1, 3],
...             [7, 3, 2, 5, 8, 5, 6, 4]]
>>> arr
a\b   b0   b1   b2   b3   b4   b5   b6   b7
 a0  0.0  3.0  5.0  6.0  4.0  2.0  1.0  3.0
 a1  7.0  3.0  2.0  5.0  8.0  5.0  6.0  4.0
>>> arr.std_by()
2.1908902300206643
>>> # along axis 'a'
>>> arr.std_by('a')
a   a0   a1
   2.0  2.0

Select some columns only

>>> arr.std_by('a', ['b0','b1','b3'])
a   a0   a1
   3.0  2.0
>>> # or equivalently
>>> # arr.std_by('a','b0,b1,b3')

Split an axis in several parts

>>> arr.std_by('a', (['b0', 'b1', 'b3'], 'b5:'))
a\b  b0,b1,b3  b5:
 a0       3.0  1.0
 a1       2.0  1.0
>>> # or equivalently
>>> # arr.std_by('a','b0,b1,b3;b5:')

Same with renaming

>>> arr.std_by('a', (X.b['b0', 'b1', 'b3'] >> 'b013', X.b['b5:'] >> 'b567'))
a\b  b013  b567
 a0   3.0   1.0
 a1   2.0   1.0
>>> # or equivalently
>>> # arr.std_by('a','b0,b1,b3>>b013;b5:>>b567')