如何在 python 的朴素贝叶斯分类器中对用户输入测试集进行分类?

Posted

技术标签:

【中文标题】如何在 python 的朴素贝叶斯分类器中对用户输入测试集进行分类?【英文标题】:How to classify user input testset in Naive Bayes Classifier in python? 【发布时间】:2019-06-04 23:48:15 【问题描述】:

我正在尝试获取用户提供的输入的结果。以下是我遇到的通用代码,其工作原理如下: 输入数据集分为训练集和测试集。训练集用于训练朴素贝叶斯模型,测试集用于测试训练模型的结果。结果,它预测了如何正确预测测试集的准确性。

import csv
import math
import random

""" 
Load the CSV File
"""

def loadCSV(filename):
    lines = csv.reader(open(r'diabetes.csv'))
    dataset = list(lines)
    for i in range(len(dataset)):
        dataset[i] = [float(x) for x in dataset[i]]
    return dataset

"""
Training
"""

def splitDataset(dataset, splitRatio):
    trainSize = int(len(dataset) * splitRatio)
    trainSet = []
    copy = list(dataset)
    while len(trainSet) < trainSize:
        index = random.randrange(len(copy))
        trainSet.append(copy.pop(index))
    return [trainSet, copy]

def seperateByClass(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):
    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)
    return math.sqrt(variance)

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

def summariesByClass(dataset):
    separated = seperateByClass(dataset)
    summaries = 
    for classValue, instances in separated.items():
        summaries[classValue] = summarize(instances)
    return summaries

"""
Prediction
"""

def calculateProbability(x, mean, stdev):
    exponent = math.exp(-(math.pow(x-mean, 2)/(2*math.pow(stdev, 2))))
    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)
        return probabilities

def predict(summaries, inputVector):
    probabilities = calculateClassProbabilities(summaries, inputVector)
    bestLabel, bestProb = None, -1
    for classValue, probability in probabilities.items():
        if bestLabel is None or probability > bestProb:
            bestProb = probability
            bestLabel = classValue
    return bestLabel

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

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

"""
Main Method
"""

def main():
    filename = 'diabetes.csv'
    splitRatio = 0.66
    dataset = loadCSV(filename)
    trainSet, testSet = splitDataset(dataset, splitRatio)
    print('Split 0 rows into train = 1 and test = 2 rows'.format(len(dataset),len(trainSet), len(testSet)))

    summaries = summariesByClass(trainSet)

    # Test Model
    predictions = getPredictions(summaries, testSet)
    print(predictions)

    accuracy = getAccuracy(testSet, predictions)

    print('Accuracy : 0%'.format(accuracy))

if __name__ == '__main__':
    main()

我想要做的修改不是将数据集拆分为训练和测试数据集,而是完全使用数据集来训练模型并提供用户输入并检查我们是否得到结果。 即在我们的dataset 中,我们根据提供给模型的数据集来预测患者是否会成为糖尿病的受害者。所以我想给用户输入这样的东西:

testSet = [[6, 148, 72, 36, 0, 33.6, 0.627, 50], [8, 183, 64, 0, 0, 23.3, 0.672, 32]]

注意:这些是我们数据集的随机两行,只是为了测试输出。

这个给定测试集的预期输出是:

result = 0.0 # For 1st sample 
result = 1.0 # For 2nd sample

请帮帮我。提前谢谢你。

【问题讨论】:

【参考方案1】:

要使用所有数据作为训练集来训练模型,只需设置

splitRatio=1

实际上,训练集的大小是使用以下表达式计算的:

trainSize = int(len(dataset) * splitRatio)

为了接受用户的输入并将其转换为列表,您可以使用:

# user input 6 148 72 36 0 33.6 0.627 50
testSet=[int(x) for x in input().split()]

【讨论】:

这里的输入是 int 和 float 所以 testSet=[int(x) for x in input().split()] 这是失败的.. :( @BlackPanther 我的错。只需将代码从 testSet=[int(x) for x in input().split()] 更改为 testSet=[float(x) for x in input().split()]。

以上是关于如何在 python 的朴素贝叶斯分类器中对用户输入测试集进行分类?的主要内容,如果未能解决你的问题,请参考以下文章

如何从朴素贝叶斯分类器中的概率密度函数计算概率?

如何在朴素贝叶斯分类器中使用 k 折交叉验证?

如何在斯坦福分类器中使用朴素贝叶斯分类器、SVM 和最大熵

文档的朴素贝叶斯分类器中的长度归一化

机器学习系列-朴素贝叶斯分类器

朴素贝叶斯行分类