Skip to content

Instantly share code, notes, and snippets.

@mmmayo13
Created May 18, 2017 13:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mmmayo13/3b8c05130f89d02d05341c68d278ebf6 to your computer and use it in GitHub Desktop.
Save mmmayo13/3b8c05130f89d02d05341c68d278ebf6 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats
def descriptive_stats(distribution):
'''
Compute and present simple descriptive stats for a distribution
Parameters
----------
distribution: list
Distribution as a Python list
'''
# Convert distribution as numpy ndarray
dist = np.array(distribution)
print 'Descriptive statistics for distribution:\n', dist
print 'Number of scores:', len(dist)
print 'Number of unique scores:', len(np.unique(dist))
print 'Sum:', sum(dist)
print 'Min:', min(dist)
print 'Max:', max(dist)
print 'Range:', max(dist)-min(dist)
print 'Mean:', np.mean(dist, axis=0)
print 'Median:', np.median(dist, axis=0)
print 'Mode:', scipy.stats.mode(dist)[0][0]
print 'Variance:', np.var(dist, axis=0)
print 'Standard deviation:', np.std(dist, axis=0)
print '1st quartile:', np.percentile(dist, 25)
print '3rd quartile:', np.percentile(dist, 75)
print 'Distribution skew:', scipy.stats.skew(dist)
plt.hist(dist, bins=len(dist))
plt.yticks(np.arange(0, 6, 1.0))
plt.title('Histogram of distribution scores')
plt.show()
descriptive_stats([ 1, 4, 5, 6, 8, 8, 9, 10, 10, 11, 11, 13, 13, 13, 14, 14, 15, 15, 15, 15 ])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment