STATISTICAL FUNCTIONS IN NUMPY

In this tutorial, we are going to learn about the statistical functions in Numpy and work with some of them like finding min, max values. Calculating Mode and mean values and finding standard deviation and variance.




What are Statistical Functions?

There are a number of statistical function in Numpy to find values related to statistics like minimum, maximum etc by going through each element of the given array. Let’s look at some examples to learn more about them.

numpy statistical

Let’s discover how to use the minimum and maximum function in numpy:

Min and Max Function in Numpy

import numpy as np
arr = np.array([[3,5,6],[7,8,9]])

print("The array is: ")
print(arr)

print("\n")
print("Min value is: ")
print(np.min(arr))

print("\n")
print("Max value is: ")
print(np.max(arr))


 

Output:

The array is: 
[[3 5 6]
 [7 8 9]]

Min value is: 
3

Max value is: 
9




Finding the Median in Numpy

Median function in numpy is used for finding the median of the array. Let’s look at an example to find out about it:

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

print("The array is: ")
print(arr)
print("\n")


print("Median function applied: ")
print(np.median(arr))

Output:

The array is: 
[[10 20 30]
 [40 50 60]
 [70 80 90]]


Median function applied: 
50.0

Finding the Mean in Numpy

The mean function in numpy is used for calculating the mean of the elements present in the array. You can calculate the mean by using the axis number as well but it only depends on a special case, normally if you want to find out the mean of the whole array then you should use the simple np.mean() function. For example:

import numpy as np
arr = np.array([[10,20,30],[40,50,60],[70,80,99]])

print("The array is: ")
print(arr)
print("\n")


print("Mean function applied: ")
print(np.mean(arr))

print("Mean function applied on axis 1: ")
print(np.mean(arr, axis=1))

Output:

The array is: 
[[10 20 30]
 [40 50 60]
 [70 80 99]]


Mean function applied: 
51.0
Mean function applied on axis 1: 
[20. 50. 83.]

Finding Standard Deviation in Numpy

You can find out the standard deviation of an array using the std() function, for example:

import numpy as np
arr = np.array([10,20,30])

print("The standard deviation of the array is: ")
print(np.std(arr))

Output:

The standard deviation of the array is: 
8.16496580927726

Finding Variance in Numpy
As you may or may not know, that variance is the mean (average) of squared deviations, and in order to calculate the variance in numpy we use the var() function.

import numpy as np
arr = np.array([10,20,30])

print("The variance of the array is: ")
print(np.var(arr))

Output:

The variance of the array is: 
66.66666666666667