Skip to content

Instantly share code, notes, and snippets.

View ppseprus's full-sized avatar

Peter Seprus ppseprus

View GitHub Profile
@ppseprus
ppseprus / entropy-in-short.js
Last active February 27, 2022 14:19 — forked from jabney/entropy.js
Javascript implementation of a Shannon entropy calculation in bits per symbol
// Shannon entropy
const entropy = str => {
return [...new Set(str)]
.map(chr => {
return str.match(new RegExp(chr, 'g')).length;
})
.reduce((sum, frequency) => {
let p = frequency / str.length;
return sum + p * Math.log2(1 / p);
}, 0);