Created
June 7, 2017 18:16
-
-
Save radekk/3d9923cb54e8c0ac7ca55cdc319dd363 to your computer and use it in GitHub Desktop.
Calculating Shannon's entropy with JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Calculate Shannon's entropy for a string | |
*/ | |
module.exports = (str) => { | |
const set = {}; | |
str.split('').forEach( | |
c => (set[c] ? set[c]++ : (set[c] = 1)) | |
); | |
return Object.keys(set).reduce((acc, c) => { | |
const p = set[c] / str.length; | |
return acc - (p * (Math.log(p) / Math.log(2))); | |
}, 0); | |
}; |
Nice. We can modernize and use ES6 set object:
const set = new Set();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@mcnemesis Yes.
Base-2 Shannon entropy calculation, it will give you a number from 0 to 8,
it is a simple frequency-calculation for each character, factorised into a specific range.
alternative that does not use the
=>
syntax.