将分类器输出限制在“0”和“1”之间

Restrict Classifier output between "0" and "1"

我正在尝试使用朴素贝叶斯算法按对象值对数组进行分类,但我的结果是 return 负值。 我希望它的 return 值介于 0 和 1 之间(代表百分比) 这是我的分类器

class Bayes{
  constructor(...categories) {
    this.categories     = {};
    this.categoryCounts = {};
    categories.forEach(category => {
      this.categories[category]     = {};
      this.categoryCounts[category] = 0;
    });
  }

  train(category, dataset) {
    this.categoryCounts[category]++;
    Object.keys(dataset).forEach(key => {
      this.categories[category][key] = (this.categories[category][key] || 0) + dataset[key];
    });
  };

  classify(dataset) {
    let scores = {};
    let trainingCount = Object.values(this.categoryCounts).reduce((a, b) => a + b );
    Object.keys(this.categories).forEach(category => {
      scores[category] = 0;
      let categoryWords = this.categories[category];
      let total = Object.values(categoryWords).reduce((a, b) => a + b );
      Object.keys(dataset).forEach(function (key) {
        let value = dataset[key];
        let s     = categoryWords[key] || 0.1;
        let i     = 0;
        while(i<value){
          scores[category] += Math.log(s / parseFloat(total));
          i++;
        }
      });
      let s = this.categoryCounts[category] || 0.1;
      scores[category] += Math.log(s / trainingCount);
    });
    return scores;
  };

};

var b = new Bayes('good', 'bad');
b.train('good', { dry: 1, wet: 0 });
b.train('bad', { dry: 0, wet: 1 });
b.train('good', { dry: 0, wet: 1, green: 1});
b.train('good', { dry: 1, wet: 0, green: 1});
console.log(b.classify({ dry: 0, wet: 1, green: 2}));

这是输出:

Object {
  bad: -5.991464547107982,
  good: -3.729701448634191
}

我想 return 结果的百分比如下:

Object {
  bad: 0.30,
  good: 0.70
}

提前致谢

我删除了scores[category] += Math.log(s / trainingCount);,

中的日志功能

class Bayes{
  constructor(...categories) {
    this.categories     = {};
    this.categoryCounts = {};
    categories.forEach(category => {
      this.categories[category]     = {};
      this.categoryCounts[category] = 0;
    });
  }

  train(category, dataset) {
    this.categoryCounts[category]++;
    Object.keys(dataset).forEach(key => {
      this.categories[category][key] = (this.categories[category][key] || 0) + dataset[key];
    });
  };

  classify(dataset) {
    let scores = {};
    let trainingCount = Object.values(this.categoryCounts).reduce((a, b) => a + b );
    Object.keys(this.categories).forEach(category => {
      scores[category] = 0;
      let categoryWords = this.categories[category];
      let total = Object.values(categoryWords).reduce((a, b) => a + b );
      Object.keys(dataset).forEach(function (key) {
        let value = dataset[key];
        let s     = categoryWords[key] || 0.1;
        let i     = 0;
        while(i<value){
          scores[category] += Math.log(s / parseFloat(total));
          i++;
        }
      });
      let s = this.categoryCounts[category] || 0.1;
      scores[category] = (s / trainingCount);
    });
    return scores;
  };

};

var b = new Bayes('good', 'bad');
b.train('good', { dry: 1, wet: 0 });
b.train('bad', { dry: 0, wet: 1 });
b.train('good', { dry: 0, wet: 1, green: 1});
b.train('good', { dry: 1, wet: 0, green: 1});
console.log(b.classify({ dry: 0, wet: 1, green: 2}));