I've needed to convert base-2 log odds output by the wink classifier into percentages. Below is the before and after array and below that is the code. Am I right in the way I've done it?

// output from wink-naive-bayes-text-classifier
[
  [ 'customer_care', -1.2157504215360433 ],
  [ 'punctuality_attendance', -2.3506271763729654 ],
  [ 'communication_skills', -4.13375644337974 ],
  [ 'teamwork', -4.301914875614537 ],
  [ 'confidence', -4.322807400088463 ],
  [ 'problem_solving', -4.383191837527313 ],
  [ 'professional_attitude', -5.093039228240558 ],
  [ 'completion_tasks', -5.605374585911287 ],
  [ 'improve_knowledge', -6.180724397591117 ],
  [ 'organisation_skills', -7.640024143000568 ],
  [ 'dealing_responsibility', -7.867817712297384 ]
]
// converted output
[
  { label: 'customer_care', value: 0.22868516194721988 },
  { label: 'punctuality_attendance', value: 0.0870159340321568 },
  { label: 'communication_skills', value: 0.01576990322838913 },
  { label: 'teamwork', value: 0.013361650261324388 },
  { label: 'confidence', value: 0.01308900361992875 },
  { label: 'problem_solving', value: 0.012331479457270554 },
  { label: 'professional_attitude', value: 0.006101871253423689 },
  { label: 'completion_tasks', value: 0.0036645641205249563 },
  { label: 'improve_knowledge', value: 0.002064656956992967 },
  { label: 'organisation_skills', value: 0.0004805857698240168 },
  { label: 'dealing_responsibility', value: 0.0003827224488948971 }
]

// code

let results = nbc.computeOdds("I'm a little teapot, short and stout.");

let newresults = results.map(el => {
    const newobj = {};
    const label = el[0];
    const logodds = el[1];
    let logratio = Math.exp(logodds) / (Math.exp(logodds) + 1);
    newobj["label"] = label;
    newobj["value"] = logratio;
    return newobj;
});

0 Answers