statistics compute mean and median python code example
Example 1: calculate mode in python
# Calculating the mode when the list of numbers may have multiple modes
from collections import Counter
def calculate_mode(n):
c = Counter(n)
num_freq = c.most_common()
max_count = num_freq[0][1]
modes = []
for num in num_freq:
if num[1] == max_count:
modes.append(num[0])
return modes
# Finding the Mode
def calculate_mode(n):
c = Counter(n)
mode = c.most_common(1)
return mode[0][0]
#src : Doing Math With Python.
Example 2: python mean
# Probably the most convenient set of statistics functions are found
# in the Numpy package
# Basic syntax:
import numpy as np
np.mean(your_list) # Calculate mean
np.median() # Calculate median
np.std() # Calculate standard deviation
np.var() # Calculate variance