I counted the frequency of each character, but what do I do now?
As I understand the formula chould be
(char_count/length)*log(2, (char_count/length))
for each character in the object, anyone please clarify what the formula is? i tried to look it up but rather than the whole topic of information theory or code that i still don’t really understand I didn’t find much Your code so far
function entropy(str) {
var chars = {}, numChars = 0, len = str.length,
res = 0, tmp;
for(let i of str) {
if(chars[i]) chars[i]++;
else {chars[i] = 1; numChars++;}
}
for(let i in chars) {
tmp = chars[i]/len;
res += tmp;
//Math.log(2,tmp), what do you do with this?
}
return res;
}
console.log(entropy("0123"))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36.
function entropy(str) {
var chars = {}, len = str.length,
res = 0, tmp;
for(let i of str) {
if(chars[i]) chars[i]++;
else chars[i] = 1;
}
for(let i in chars) {
tmp = chars[i]/len;
res -= tmp*Math.log(2,tmp);
}
return res;
}
console.log(entropy("0123456789abcdef"));
I thought it takes to arguments, one as the base and other as “destination” (you know what i mean) lol, so i can use log2 or that rule of log a (b) = log m (b)/log m(a), but log2 is better cuz it’s less actions and your’e dealing with more precise numbers