I am leading a Gaussian Naive Bayes from scratch for this really good tutorial: https://machinelearningmastery.com/naive-bayes-classifier-scratch-python/

Everything seems to work well. I've adapted the code to work with other records coming from here http://archive.ics.uci.edu/ml/datasets/statlog+(heart)

Because this tutorial uses a Gaussian probability density function, it provides probability. I've tried to tweak that idea to make sure that my output also shows a probability, not just a probability. The decision-making process works well, he predicts correctly. However, my probability output shows, for example, "1.2803300002643495e-15," which I can not "convert" with a probability.

I've tried switching from probability to probability:

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

However, it does not provide correct probabilities (i.e., it outputs 0.9991818295017332 or 2,8694419736742756e-06, which is clearly incorrect). Please let me know if I should add more explanations.

My whole code is:

```
# - * - Coding: utf-8 - * -
"" "
"" "
Import csv
Import math
"" "
-------------------
Start: load data
-------------------
It reads the DAT files and returns them as floats
Used for training and test files
"" "
def loadCsv (filename):
lines = csv.reader (open (filename, "rt"), separator = & # 39; & # 39;)
Record = list (rows)
for i in range (len (record)):
record[i] =[Float(x)fürximDatensatz[Float(x)forxindataset[float(x)fürximDatensatz[float(x)forxindataset[i]]Return dataset
"" "
-------------------
End: load data
-------------------
"" "
def separateByClass (record):
separated = {}
for i in range (len (record)):
Vector = record[i]
if (vector[-1] not separated
separated[Vector[vector[Vektor[vector[-1]]= []
separated[Vector[vector[Vektor[vector[-1]].append (vector)
Return separately
def mean (numbers):
#print (sum (pay) / float (len (pay)))
Result sum (numbers) / Float (Len (numbers))
def stdev (numbers):
avg = average (numbers)
Deviation = sum ([pow(x-avg,2) for x in numbers]) / float (len (pay) -1)
#print (math.sqrt (deviation))
Return math.sqrt (deviation)
Merge Def (Record):
Summaries [(mean(attribute), stdev(attribute)) for attribute in zip(*dataset)]
del summaries[-1]
Return summaries
def summarizeByClass (record):
separated = separateByClass (record)
Summaries = {}
for classValue instances in separate.items ():
summaries[classValue] = summarize (instances)
#print (summaries)
Return summaries
def calculable (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] * = calculate predictability (x, mean, stdev)
print ("here")
Pressure (probabilities)[1])
print ("Here2")
Pressure (probabilities)[2])
#print ("Here3")
#print (probabilities)[1]/ (Probabilities[1] + Probabilities[2]))
return probabilities
Def predictions (summaries, input vector):
to press("-----------------------------")
Probabilities = convertClassProbabilities (summaries, inputVector)
print (& # 39; probabilities for each class: {0} & # 39;. 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 = class value
print (bestProb)
print (bestLabel)
if bestLabel == 1:
print ("hereeeee11111")
print (probfor1)
otherwise:
Print ("Hereeeee2222")
print (probfor2)
return bestLabel
def getPredictions (summaries, testSet):
Predictions = []
for i in range (len (testSet)):
Result = predictions (summaries, TestSet.)[i])
predictions.append (result)
print (result)
Return predictions
def getAccuracy (testSet, Predictions):
right = 0
for i in range (len (testSet)):
if testSet[i][-1] == predictions[i]:
right + = 1
return (true / float (len (testSet))) * 100.0
def main ():
filename_training = & # 39; heart.training.dat & # 39;
filename_testing = & heart; test.dat & # 39;
#splitratio = 0.67
trainingSet = loadCsv (filename_training)
testSet = loadCsv (filename test)
#trainingSet, testSet = splitDataset (trainingset, splitRatio)
divide #print (& # 39; {0} lines into train = {1} and test = {2} lines & # 39; format (len (record), len (trainingSet), len (testSet)))
# Prepare the model
summaries = summarizeByClass (trainingSet)
# Test model
Predictions = GetPredictions (summaries, TestSet)
accuracy = getAccuracy (testSet, predictions)
print (& # 39; accuracy: {0}% & # 39; format (precision))
Main()
```