Updated On : Jul-20,2020  numpy, basics

# Learning Numpy - Simple Tutorial For Beginners - NumPy - Statistical Functions Part 16¶

NumPy has quite a few useful statistical functions for finding minimum, maximum, percentile standard deviation and variance, etc. from the given elements in the array. The functions are explained as follows −

## numpy.amin() and numpy.amax()¶

These functions return the minimum and the maximum from the elements in the given array along the specified axis.

In [1]:
```import numpy as np
a = np.array([[3,7,5],[8,4,3],[2,4,9]])
```
In [3]:
```print ('Our array is:')
print (a)
```
```Our array is:
[[3 7 5]
[8 4 3]
[2 4 9]]
```
In [4]:
```print ('Applying amin() function:')
print (np.amin(a,1) )
```
```Applying amin() function:
[3 3 2]
```
In [5]:
```print ('Applying amin() function again:')
print (np.amin(a,0) )
```
```Applying amin() function again:
[2 4 3]
```
In [6]:
```print ('Applying amax() function:')
print (np.amax(a))
```
```Applying amax() function:
9
```
In [7]:
```print ('Applying amax() function again:')
print (np.amax(a, axis = 0))
```
```Applying amax() function again:
[8 7 9]
```

## numpy.ptp()¶

The numpy.ptp() function returns the range (maximum-minimum) of values along an axis.

In [8]:
```import numpy as np
a = np.array([[3,7,5],[8,4,3],[2,4,9]])
```
In [9]:
```print ('Our array is:')
print (a)
```
```Our array is:
[[3 7 5]
[8 4 3]
[2 4 9]]
```
In [10]:
```print ('Applying ptp() function:')
print (np.ptp(a) )
```
```Applying ptp() function:
7
```
In [11]:
```print ('Applying ptp() function along axis 1:')
print (np.ptp(a, axis = 1))
```
```Applying ptp() function along axis 1:
[4 5 7]
```
In [12]:
```print ('Applying ptp() function along axis 0:')
print (np.ptp(a, axis = 0))
```
```Applying ptp() function along axis 0:
[6 3 6]
```

## numpy.percentile()¶

Percentile (or a centile) is a measure used in statistics indicating the value below which a given percentage of observations in a group of observations fall. The function numpy.percentile() takes the following arguments.

```numpy.percentile(a, q, axis)
```
Argument Description
a Input array
q The percentile to compute must be between 0-100
axis The axis along which the percentile is to be calculated

### Examples¶

In [14]:
```import numpy as np
a = np.array([[30,40,70],[80,20,10],[50,90,60]])
```
In [15]:
```print ('Our array is:')
print (a)
```
```Our array is:
[[30 40 70]
[80 20 10]
[50 90 60]]
```
In [16]:
```print ('Applying percentile() function:')
print (np.percentile(a,50))
```
```Applying percentile() function:
50.0
```
In [18]:
```print ('Applying percentile() function along axis 1:')
print (np.percentile(a,50, axis = 1))
```
```Applying percentile() function along axis 1:
[40. 20. 60.]
```
In [19]:
```print ('Applying percentile() function along axis 0:')
print (np.percentile(a,50, axis = 0))
```
```Applying percentile() function along axis 0:
[50. 40. 60.]
```

## numpy.median()¶

Median is defined as the value separating the higher half of a data sample from the lower half. The numpy.median() function is used as shown in the following program.

In [ ]:
```import numpy as np
a = np.array([[30,65,70],[80,95,10],[50,90,60]])
```
In [21]:
```print ('Our array is:')
print (a)
```
```Our array is:
[[30 40 70]
[80 20 10]
[50 90 60]]
```
In [22]:
```print ('Applying median() function:')
print (np.median(a))
```
```Applying median() function:
50.0
```
In [23]:
```print ('Applying median() function along axis 0:')
print (np.median(a, axis = 0))
```
```Applying median() function along axis 0:
[50. 40. 60.]
```
In [24]:
```print ('Applying median() function along axis 1:')
print (np.median(a, axis = 1))
```
```Applying median() function along axis 1:
[40. 20. 60.]
```

## numpy.mean()¶

Arithmetic mean is the sum of elements along an axis divided by the number of elements. The numpy.mean() function returns the arithmetic mean of elements in the array. If the axis is mentioned, it is calculated along it.

In [ ]:
```import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
```
In [25]:
```print ('Our array is:')
print (a)
```
```Our array is:
[[30 40 70]
[80 20 10]
[50 90 60]]
```
In [26]:
```print ('Applying mean() function:')
print (np.mean(a))
```
```Applying mean() function:
50.0
```
In [27]:
```print ('Applying mean() function along axis 0:')
print (np.mean(a, axis = 0) )
```
```Applying mean() function along axis 0:
[53.33333333 50.         46.66666667]
```
In [28]:
```print ('Applying mean() function along axis 1:')
print (np.mean(a, axis = 1))
```
```Applying mean() function along axis 1:
[46.66666667 36.66666667 66.66666667]
```

## numpy.average()¶

Weighted average is an average resulting from the multiplication of each component by a factor reflecting its importance. The numpy.average() function computes the weighted average of elements in an array according to their respective weight given in another array. The function can have an axis parameter. If the axis is not specified, the array is flattened.

Considering an array [1,2,3,4] and corresponding weights [4,3,2,1], the weighted average is calculated by adding the product of the corresponding elements and dividing the sum by the sum of weights.

Weighted average = (14+23+32+41)/(4+3+2+1)

In [29]:
```import numpy as np
a = np.array([1,2,3,4])
```
In [30]:
```print ('Our array is:')
print (a)
```
```Our array is:
[1 2 3 4]
```
In [32]:
```print ('Applying average() function:')
print (np.average(a))
### this is same as mean when weight is not specified
wts = np.array([4,3,2,1])
```
```Applying average() function:
2.5
```
In [33]:
```print ('Applying average() function again:')
print (np.average(a,weights = wts))
```
```Applying average() function again:
2.0
```
In [35]:
```# Returns the sum of weights, if the returned parameter is set to True.
print ('Sum of weights')
print (np.average([1,2,3, 4],weights = [4,3,2,1], returned = True))
```
```Sum of weights
(2.0, 10.0)
```

In a multi-dimensional array, the axis for computation can be specified.

In [37]:
```import numpy as np
a = np.arange(6).reshape(3,2)
```
In [38]:
```print ('Our array is:')
print (a)
```
```Our array is:
[[0 1]
[2 3]
[4 5]]
```
In [40]:
```print ('Modified array:')
wt = np.array([3,5])
print (np.average(a, axis = 1, weights = wt))
```
```Modified array:
[0.625 2.625 4.625]
```
In [41]:
```print ('Modified array:')
print (np.average(a, axis = 1, weights = wt, returned = True))
```
```Modified array:
(array([0.625, 2.625, 4.625]), array([8., 8., 8.]))
```

### Standard Deviation¶

Standard deviation is the square root of the average of squared deviations from mean. The formula for standard deviation is as follows −

```std = sqrt(mean(abs(x - x.mean())**2))
```

If the array is [1, 2, 3, 4], then its mean is 2.5. Hence the squared deviations are [2.25, 0.25, 0.25, 2.25] and the square root of its mean divided by 4, i.e., sqrt (5/4) is 1.1180339887498949.

In [43]:
```import numpy as np
print (np.std([1,2,3,4]))
```
```1.118033988749895
```

## Variance¶

Variance is the average of squared deviations, i.e.,

```mean(abs(x - x.mean())**2)
```

In other words, the standard deviation is the square root of variance.

In [44]:
```import numpy as np
print (np.var([1,2,3,4]))
```
```1.25
```
Dolly Solanki

## Support Us

Thank You for visiting our website. If you like our work, please support us so that we can keep on creating new tutorials/blogs on interesting topics (like AI, ML, Data Science, Python, Digital Marketing, SEO, etc.) that can help people learn new things faster. You can support us by clicking on the Coffee button at the bottom right corner. We would appreciate even if you can give a thumbs-up to our article in the comments section below.

## Want to Share Your Views? Have Any Suggestions?

If you want to

• provide some suggestions on topic
• share your views
• include some details in tutorial
• suggest some new topics on which we should create tutorials/blogs
Please feel free to let us know in the comments section below (Guest Comments are allowed). We appreciate and value your feedbacks.