Skip to content

Instantly share code, notes, and snippets.

@emorisse
Last active May 20, 2020 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emorisse/68f031730c7ad70598a8db928f54c8c9 to your computer and use it in GitHub Desktop.
Save emorisse/68f031730c7ad70598a8db928f54c8c9 to your computer and use it in GitHub Desktop.
function histogram(data, bins=5) {
const min = Math.min(...data)
const max = Math.max(...data)
const size = Math.ceil((max-min)/bins)
var histogram = new Array(bins).fill(0);
for (const item of data) {
index = Math.floor((item - min) / size)
if ( index > ( bins -1 ) ) { // total hack
index--
}
histogram[index]++;
}
return histogram;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment