# Descriptive Statistics#

Numpy offers many statistical functions, but if you want to obtain several statistical variables from the same array, it’s necessary to process the data several times to calculate the various parameters. This example shows how to use the DescriptiveStatistics class to obtain several statistical variables with a single calculation. Also, the calculation algorithm is incremental and is more numerically stable.

Note

Pébay, P., Terriberry, T.B., Kolla, H. et al. Numerically stable, scalable formulas for parallel and online computation of higher-order multivariate central moments with arbitrary weights. Comput Stat 31, 1305–1325, 2016, https://doi.org/10.1007/s00180-015-0637-z

```import dask.array
import numpy

import pyinterp
```

Create a random array

```values = numpy.random.random_sample((2, 4, 6, 8))
```

Create a DescriptiveStatistics object.

```ds = pyinterp.DescriptiveStatistics(values)
```

The constructor will calculate the statistical variables on the provided data. The calculated variables are stored in the instance and can be accessed using different methods:

• mean

• var

• std

• skewness

• kurtosis

• min

• max

• sum

• sum_of_weights

• count

```ds.count()
```
```array([384], dtype=uint64)
```
```ds.mean()
```
```array([0.52467272])
```

It’s possible to get a structured numpy array containing the different statistical variables calculated.

```ds.array()
```
```array([(384, -1.16349537, 0.99478143, 0.52467272, 0.00324656, -0.12603512, 384., 201.4743235, 0.08202834)],
dtype=[('count', '<u8'), ('kurtosis', '<f8'), ('max', '<f8'), ('mean', '<f8'), ('min', '<f8'), ('skewness', '<f8'), ('sum_of_weights', '<f8'), ('sum', '<f8'), ('var', '<f8')])
```

Like numpy, it’s possible to compute statistics along axis.

```ds = pyinterp.DescriptiveStatistics(values, axis=(1, 2))
ds.mean()
```
```array([[0.50161524, 0.47832087, 0.55965523, 0.5930476 , 0.49297042,
0.57258422, 0.54168189, 0.50496045],
[0.52801771, 0.42951348, 0.55554672, 0.40733321, 0.53284628,
0.4670159 , 0.61665105, 0.6130032 ]])
```

The class can also process a dask array. In this case, the call to the constructor triggers the calculation.

```ds = pyinterp.DescriptiveStatistics(dask.array.from_array(values,
chunks=(2, 2, 2, 2)),
axis=(1, 2))
ds.mean()
```
```array([[0.50161524, 0.47832087, 0.55965523, 0.5930476 , 0.49297042,
0.57258422, 0.54168189, 0.50496045],
[0.52801771, 0.42951348, 0.55554672, 0.40733321, 0.53284628,
0.4670159 , 0.61665105, 0.6130032 ]])
```

Finally, it’s possible to calculate weighted statistics.

```weights = numpy.random.random_sample((2, 4, 6, 8))
ds = pyinterp.DescriptiveStatistics(values, weights=weights, axis=(1, 2))
ds.mean()
```
```array([[0.4917618 , 0.47250149, 0.51442153, 0.57084807, 0.46471164,
0.62028813, 0.52056957, 0.50287215],
[0.53133015, 0.43128425, 0.58438632, 0.36840336, 0.55897629,
0.4996137 , 0.62320828, 0.65902314]])
```

Total running time of the script: ( 0 minutes 0.012 seconds)

Gallery generated by Sphinx-Gallery