朴素贝叶斯-从“可能性”到“概率”

时间:2019-03-02 03:30:04

标签: python gaussian naivebayes

我正在按照这个非常好的教程从头开始实现高斯朴素贝叶斯:https://machinelearningmastery.com/naive-bayes-classifier-scratch-python/

显然,一切正常。我修改了代码,使其可以与来自http://archive.ics.uci.edu/ml/datasets/statlog+(heart)

的其他数据集一起使用

由于本教程使用“高斯概率密度函数”,因此提供了可能性。我一直在尝试调整这个想法,以确保我的输出也显示给我一个概率,而不仅仅是一个可能性。决策过程运行良好,正在预测正确。但是,我的似然度输出显示例如“ 1.2803300002643495e-15”,我无法“转换”为概率。

我试图从可能性变为可能性:

probfor1=(probabilities[1]/(probabilities[1] + probabilities[2]))
probfor2=probabilities[2]/(probabilities[1] + probabilities[2]) 

但是,它不能为我提供正确的概率(即输出0.9995918295017332或2.8694419736742756e-06显然是不正确的)。请让我知道是否需要补充说明。

我的整个代码是:

# -*- coding: utf-8 -*-
"""

"""

import csv
import math


"""
-------------------
Start: Load data
-------------------
It reads the .dat files and return as floats
Used for both training & test files
"""
def loadCsv(filename):
    lines = csv.reader(open(filename, "rt"),delimiter=' ')
    dataset = list(lines)
    for i in range(len(dataset)):
        dataset[i] = [float(x) for x in dataset[i]]
    return dataset
"""
-------------------
End: Load data
-------------------
"""


def separateByClass(dataset):
    separated = {}
    for i in range(len(dataset)):
        vector = dataset[i]
        if (vector[-1] not in separated):
            separated[vector[-1]] = []
        separated[vector[-1]].append(vector)
    return separated

def mean(numbers):
    #print(sum(numbers)/float(len(numbers)))
    return sum(numbers)/float(len(numbers))

def stdev(numbers):
    avg = mean(numbers)
    variance = sum([pow(x-avg,2) for x in numbers])/float(len(numbers)-1)
    #print(math.sqrt(variance))     
    return math.sqrt(variance)

def summarize(dataset):
    summaries = [(mean(attribute), stdev(attribute)) for attribute in zip(*dataset)]
    del summaries[-1]
    return summaries

def summarizeByClass(dataset):
    separated = separateByClass(dataset)
    summaries = {}
    for classValue, instances in separated.items():
        summaries[classValue] = summarize(instances)
    #print(summaries)   
    return summaries

def calculateProbability(x, mean, stdev):
    exponent = math.exp(-(math.pow(x-mean,2)/(2*math.pow(stdev,2))))
    #print((1 / (math.sqrt(2*math.pi) * stdev)) * exponent)
    return (1 / (math.sqrt(2*math.pi) * stdev)) * exponent

def calculateClassProbabilities(summaries, inputVector):
    probabilities = {}
    for classValue, classSummaries in summaries.items():
        probabilities[classValue] = 1
        for i in range(len(classSummaries)):
            mean, stdev = classSummaries[i]
            x = inputVector[i]
            probabilities[classValue] *= calculateProbability(x, mean, stdev)
    print("Here")           
    print(probabilities[1])
    print("Here2")          
    print(probabilities[2])

    #print("Here3")         
    #print(probabilities[1]/(probabilities[1] + probabilities[2]))
    return probabilities

def predict(summaries, inputVector):
    print("-----------------------------")
    probabilities = calculateClassProbabilities(summaries, inputVector)
    print('Probabilities for each class: {0}'.format(probabilities))
    probfor1=(probabilities[1]/(probabilities[1] + probabilities[2]))
    probfor2=probabilities[2]/(probabilities[1] + probabilities[2])        
    bestLabel, bestProb = None, -1
    for classValue, probability in probabilities.items():
        if bestLabel is None or probability > bestProb:

            bestProb = probability
            bestLabel = classValue
            print(bestProb)
            print(bestLabel)
            if bestLabel == 1:
                print("hereeeee11111")
                print(probfor1)
            else:
                print("hereeeee2222")
                print(probfor2)                

    return bestLabel

def getPredictions(summaries, testSet):
    predictions = []
    for i in range(len(testSet)):
        result = predict(summaries, testSet[i])
        predictions.append(result)
        print(result)
    return predictions

def getAccuracy(testSet, predictions):
    correct = 0
    for i in range(len(testSet)):
        if testSet[i][-1] == predictions[i]:
            correct += 1
    return (correct/float(len(testSet))) * 100.0

def main():
    filename_training = 'heart.training.dat'
    filename_testing = 'heart.test.dat'
    #splitRatio = 0.67
    trainingSet = loadCsv(filename_training)
    testSet = loadCsv(filename_testing)
    #trainingSet, testSet = splitDataset(trainingset, splitRatio)
    #print('Split {0} rows into train={1} and test={2} rows'.format(len(dataset), len(trainingSet), len(testSet)))
    # prepare model
    summaries = summarizeByClass(trainingSet)
    # test model
    predictions = getPredictions(summaries, testSet)
    accuracy = getAccuracy(testSet, predictions)
    print('Accuracy: {0}%'.format(accuracy))

main()

0 个答案:

没有答案