Skip to content

Instantly share code, notes, and snippets.

@swateek
Created July 28, 2017 10:12
Show Gist options
  • Save swateek/1c0ea7558fed6c4577cc0252f9324dba to your computer and use it in GitHub Desktop.
Save swateek/1c0ea7558fed6c4577cc0252f9324dba to your computer and use it in GitHub Desktop.
Freedman-Diaconis thumb rule for number of bins of a histogram
import math
def numBins(metric, defaultBins):
h = binWidth(metric)
ulim = max(metric)
llim = min(metric)
if (h <= (ulim - llim) / len(metric)):
return defaultBins or 10
return int(math.ceil((ulim - llim) / h))
def binWidth(metric):
return 2 * iqr(metric) * (len(metric) ** (-0.333))
def comparator(a, b):
return a - b
def iqr(metric):
metric[0:].sort(cmp=comparator)
q1 = metric[int(math.floor(len(metric) / 4))]
q3 = metric[int(math.floor(len(metric) * 3 / 4))]
return q3 - q1
metric = []
for x in range(0,1866):
metric.append(x)
print numBins(metric, 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment