function prob()

in code/decision-tree/js/EntropyBubble.js [43:59]


function prob(data) {
  // track data length (used a couple times)
  const n = data.length;
  // empty data has entropy of zero
  if (n < 1) {
    return 0.5;
  }
  // init counter
  const counts = {};
  // count occurrences of elements in data
  for (const d of data) {
    counts[d] = counts[d] ? counts[d] + 1 : 1;
  }
  //  ensure value for class so no NaN
  counts["positive"] = counts["positive"] ? counts["positive"] : 0;
  return counts["positive"] / n;
}