How to calulate Mean, Median and Mode in numpy | 2019
In this article, You will learn about statistics functions like mean, median and mode.
Mean: It means the average number from the list or list of variables.
Median: We can calculate the median by with a middle number of the series. If the series has 2 middle numbers, then we have to calculate avg number.
Mode: Mode function produces most repeated ones from the list.
Example program to to calulate Mean, Median and Mode in numpy
import numpy as np
from scipy import stats
list1 = [1,2,3,4,5,7,8,9,10]
mean = np.mean(list1)
medium = np.median(list1)
mode = stats.mode(list1)
print('Mean is ', mean)
print('Median is ', medium)
print('Mode is ', mode[0])
list2 = [1,2,3,4,5,7,7,8,9,10]
mean = np.mean(list2)
medium = np.median(list2)
mode = stats.mode(list2)
print('\nMean is ', mean)
print('median is ', medium)
print('Mode is ', mode[0])
Output:
Mean is 5.444444444444445 Median is 5.0 Mode is [1] Mean is 5.6 median is 6.0 Mode is [7]
# for multi dimensional array
a = np.array([[1, 2, 3, 4],
[5, 5, 6, 6],
[7, 8, 9, 10],
[5,5,6,6]])
mean = np.mean(a)
medium = np.median(a)
mode = stats.mode(a)
print('Mean is ', mean)
print('Median is ', medium)
print('Mode is ', mode)
Output:
Mean is 5.5 Median is 5.5 Mode is ModeResult(mode=array([[5, 5, 6, 6]]), count=array([[2, 2, 2, 2]]))