Skip to content

Instantly share code, notes, and snippets.

@gautamsreekumar
Created January 28, 2020 20:18
Show Gist options
  • Save gautamsreekumar/5cb087be199a44606ec519fbe5e657d0 to your computer and use it in GitHub Desktop.
Save gautamsreekumar/5cb087be199a44606ec519fbe5e657d0 to your computer and use it in GitHub Desktop.
An example to calculate mean and standard deviation for a frequency distribution table
import numpy as np
import matplotlib.pyplot as plt
# directly listing all the midpoints of the interval using np.linspace
midpoints = np.linspace(5, 95, 10)
# frequency of each interval
freq = np.array([0, 5, 315, 1606, 3105, 2935, 1500, 324, 21, 0])
# elementwise product to get midpoint*frequency
midpt_freq = midpoints*freq
# mean of the frequency distribution
mean = np.sum(midpt_freq)/np.sum(freq)
# variance of the frequency distribution
var = np.sum(freq*(midpoints-mean)**2)/np.sum(freq)
# std deviation is the square root of variance
std = np.sqrt(var)
print("Mean", mean, "Standard deviation", std)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment