in code/decision-tree/js/EntropyBubble.js [16:41]
function entropy(data) {
// track data length (used a couple times)
const n = data.length;
// empty data has entropy of zero
if (n <= 1) {
return 0;
}
// init counter
const counts = {};
// count occurrences of elements in data
for (const d of data) {
counts[d] = counts[d] ? counts[d] + 1 : 1;
}
// start entropy at 0
let entropyValue = 0;
// loop through data and calculate entropy
Object.keys(counts).forEach((c) => {
// get relative frequency of item
const prob = counts[c] / n;
// if > 0, tally entropy
if (prob > 0) {
entropyValue -= prob * Math.log2(prob);
}
});
return entropyValue;
}