# Statistical Functions in NumPy Python

In this tutorial, we will learn about the **statistical functions of numpy in Python**. We all know that the numpy module holds the functionalities to process arrays. Numpy has many functions that can perform many complex statistical operations easily. Using numpy, we can easily calculate the **mean, median, ptp, percentile, max, min, etc.**

Let’s understand the working of these functions using Python codes.

#### average() in NumPy

This is a simple statistical function which calculates the average for the given array. Let’s see Python code for the function.

import numpy as np arr=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) print(np.average(arr))

Here we have defined an array “arr” in numpy.

output: 6.5

we can perform this operation according to an axis.

import numpy as np arr=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) print(np.average(arr,axis=1))

output: [ 2.5 6.5 10.5]

axis can be 0/1.

#### median() in NumPy

This is another statistical value, we can easily calculate the median in Python. Let’s see how.

import numpy as np arr=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) print(np.median(arr))

output: 6.5

#### percentile() in NumPy

Percentile is a measure used in statistics which indicates the value below which a given percentage of observations in a group of observations falls. This function takes 3 arguments percentile(array,q,axis).

**array**: the array for which we want to find the percentile

**q**: the percentile value(0-100)

**axis**: it can be 0/1

import numpy as np arr=np.array([[10,20,30],[40,50,60],[70,80,90]]) print(np.percentile(arr,50,axis=1))

output: [20. 50. 80.]

#### ptp()

This function returns the range(max-min) of values in the axis.

import numpy as np arr=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) print(np.ptp(arr,axis=0))

output: [8 8 8 8]

Here also the axis can be 0/1.

#### Standard Deviation

Standard deviation is the square root of the average of squared deviations from the mean.

import numpy as np arr=np.array([1,2,3,4]) print(np.std(arr))

output: 1.118033988749895

#### Variance

Standard deviation is the square root of variance. It can be termed as a square of standard deviation is variance.

import numpy as np arr=np.array([1,2,3,4]) print(np.var(arr))

output: 1.25

Also read:

## Leave a Reply