在 Naive Bayes 分类器中,概率计算错误通常可以归结为几个常见的问题和解决方法。以下是可能导致概率计算错误的一些常见情况及其解决方法,希望本文能对你有帮助。

Naive Bayes 分类器中概率计算错误_解决方法

1、问题背景

在实现一个朴素贝叶斯分类器时,作者发现分类器的准确率只有61%左右,并且分类器计算出的概率值与预期不符,即两类的概率值之和不等于1。

2、解决方案

  • 朴素贝叶斯分类器不会直接计算概率,而会计算一个“原始分数”,然后将该分数与其他标签的分数进行比较,以对实例进行分类。
  • 这个分数很容易转换成[0, 1]范围内的“概率”: total = sum(probs.itervalues()) for label, score in probs.iteritems(): probs[label] = score / total
  • 然而,需要记住的是,这仍然不是一个真正的概率,正如这个答案中提到的: 朴素贝叶斯倾向于预测概率,这些概率几乎总是非常接近于零或非常接近于一。

代码例子:

import csv

# 加载数据
def load_data(filename):
    data = []
    tgts = []

    with open(filename, 'r') as fil:
        reader = csv.reader(fil)
        for line in reader:
            d = [x.strip() for x in line]
            if '?' in d:
                continue

            if not len(d):
                continue

            data.append(d[:-1])
            tgts.append(d[-1:][0])

    return data, tgts

# 训练朴素贝叶斯分类器
def train(data, targets):
    global words_stats, words_cnt, targets_stats, items_cnt, class_stats

    num = len(data)
    for item in xrange(num):
        class_stats[targets[item]] = class_stats.get(targets[item], 0) + 1

        for i in xrange(len(data[item])):
            word = data[item][i]
            if not words_stats.has_key(word):
                words_stats[word] = {}

            tgt = targets[item]

            cnt = words_stats[word].get(tgt, 0)
            words_stats[word][tgt] = cnt + 1

            targets_stats[tgt] = targets_stats.get(tgt, 0) + 1
            words_cnt += 1

    items_cnt = num

# 对新数据进行分类
def classify(doc, tgt_set):
    global words_stats, words_cnt, targets_stats, items_cnt

    probs = {} #the probability itself P(c|W) = P(W|c) * P(c) / P(W)
    pc = {} #probability of the class in document set P(c)
    pwc = {} #probability of the word set in particular class. P(W|c)
    pw = 1 #probability of the word set in documet set

    for word in doc:
        if word not in words_stats:
            continue #dirty, very dirty
        pw = pw * float(sum(words_stats[word].values())) / words_cnt

    for tgt in tgt_set:
        pc[tgt] = class_stats[tgt] / float(items_cnt)
        for word in doc:
            if word not in words_stats:
                continue #dirty, very dirty
            tgt_wrd_cnt = words_stats[word].get(tgt, 0)
            pwc[tgt] = pwc.get(tgt, 1) * float(tgt_wrd_cnt) / targets_stats[tgt]

        probs[tgt] = (pwc[tgt] * pc[tgt]) / pw

    l = sorted(probs.items(), key = lambda i: i[1], reverse=True)
    print probs
    return l[0][0]

# 检查分类结果
def check_results(dataset, targets):
    num = len(dataset)
    tgt_set = set(targets)
    correct = 0
    incorrect = 0

    for item in xrange(num):
        res = classify(dataset[item], tgt_set)
        if res == targets[item]:
            correct = correct + 1
        else:
            incorrect = incorrect + 1

    print 'correct:', float(correct) / num, ' incorrect:', float(incorrect) / num

if __name__ == '__main__':
    filename = 'adult.data'
    data, tgt = load_data(filename)
    train(data, tgt)

    test_filename = 'adult.test'
    test_data, test_tgt = load_data(test_filename)

    check_results(test_data, tgt)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.

通过以上代码,相信大家应该能够诊断和解决 Naive Bayes 分类器中概率计算错误的常见问题。如果问题仍然存在,建议提供具体的错误信息或代码片段,以便更详细地帮助您解决问题。