Skip to content

Instantly share code, notes, and snippets.

@ppseprus
ppseprus / entropy-in-short.js
Last active August 22, 2024 10:13 — 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);