/    /  NumPy – Statistical Functions

NumPy – Statistical Functions:

In Numpy, we can perform various statistical calculations using the various functions that are provided in the library like Order statistics, Averages and variances, correlating, Histograms.

Let us work on some of those statistical functions.

Order statistics:

amin: This will return the minimum of an array.
amax: This will return the maximum of an array.

Example:

>>> import numpy as np

>>> x=np.arange(4).reshape((2,2))

>>> x
array([[0, 1],
[2, 3]])

>>> np.amin(x)
0

>>> np.amin(x, axis=0)
array([0, 1])

>>> np.amax(x)
3

>>> np.amax(x, axis=0)
array([2, 3])

>>> np.amax(x, axis=1)
array([1, 3])

Averages and variances:

median: This will return the median along the specified axis.
average: This will return the weighted average along the specified axis.
mean: This will return the arithmetic mean along the specified axis.
std: This will return the standard deviation along the specified axis.
var: This will return the variance along the specified axis.

Example:

>>> import numpy as np

>>> x=np.array([[1,2,3],[4,5,6]])

>>> a
array([[[1, 0, 1],
[0, 0, 1]]])

>>> x
array([[1, 2, 3],
[4, 5, 6]])

>>> np.median(x)
3.5

>>> np.median(x,axis=0)
array([ 2.5, 3.5, 4.5])

>>> np.average(x)
3.5

>>> np.mean(x)
3.5

>>> np.std(x)
1.707825127659933

>>> np.var(x)
2.9166666666666665

Histograms:

histogram: This will return the computed histogram of a set of data.

This function mainly works with bins and set of data given as input. Numpy histogram function will give the computed result as the occurances of input data which fall in each of the particular range of bins. That determines the range of area of each bar when plotted using matplotlib.

Example:

>>> import numpy as np

>>> np.histogram([10,15,16,24,25,45,36,45], bins=[0,10,20,30,40,50])
(array([0, 3, 2, 1, 2], dtype=int32), array([ 0, 10, 20, 30, 40, 50]))

>>> import matplotlib.pyplot as plt

>>> plt.hist([10,15,16,24,25,45,36,45], bins=[0,10,20,30,40,50])
(array([ 0., 3., 2., 1., 2.]), array([ 0, 10, 20, 30, 40, 50]), <a list of 5 Patch objects>)

>>> plt.show()