Jovian
⭐️
Sign In
In [1]:
import csv
import random
import math
In [2]:
def loadCsv(filename):
    lines = csv.reader(open(filename, "r"))
    dataset = list(lines)
    for i in range(len(dataset)):
        dataset[i] = [float(x) for x in dataset[i]]
    return dataset
In [3]:
def split(dataset, ratio):
    trainSize = int(len(dataset)*ratio)
    trainSet = []
    copy = list(dataset)
    while len(trainSet)<trainSize:
        index = random.randrange(len(copy))
        trainSet.append(copy.pop(index))
    return trainSet, copy
In [4]:
def separate(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
In [5]:
def mean(numbers):
    return sum(numbers)/float(len(numbers))
In [6]:
def stddev(numbers):
    avg = mean(numbers)
    variance = sum([pow(x-avg, 2) for x in numbers])/float(len(numbers)-1)
    return math.sqrt(variance)
In [7]:
def summarize(dataset):
    summaries = [(mean(attribute), stddev(attribute)) for attribute in zip(*dataset)]
    del summaries[-1]
    return summaries
In [8]:
def summByClass(dataset):
    separated = separate(dataset)
    summaries = {}
    for classValue, instances in  separated.items():
        summaries[classValue] = summarize(instances)
    return summaries
In [9]:
def calcProb(x, mean, stddev):
    exponent = math.exp(-(math.pow(x-mean, 2)/(2*math.pow(stddev, 2))))
    return (1/math.sqrt(2*math.pi)*stddev)*exponent
In [10]:
def calcClassProb(summaries, inputV):
    probs = {}
    for classValue, classSumm in summaries.items():
        probs[classValue] = 1
        for i in range(len(classSumm)):
            mean, stddev = classSumm[i]
            x = inputV[i]
            probs[classValue] *= calcProb(x, mean, stddev)
    return probs
In [11]:
def predict(summaries, inputV):
    probs = calcClassProb(summaries, inputV)
    bestLabel, bestPeob = None, -1
    for classValue, probability in probs.items():
        if bestLabel is None or probability>bestProb:
            bestProb = probability
            bestLabel = classValue
    return bestLabel
In [12]:
def getPreds(summaries, testSet):
    preds = []
    for i in range(len(testSet)):
        result = predict(summaries, testSet[i])
        preds.append(result)
    return preds
In [13]:
def getAccuracy(testSet, preds):
    correct = 0
    for i in range(len(testSet)):
        if testSet[i][-1] == preds[i]:
            correct += 1
    return (correct/float(len(testSet)))*100.0
In [14]:
def main():
    ratio = 0.67
    filename = "datasets/5.csv"
    dataset = loadCsv(filename)
    trainingSet, testSet = split(dataset, ratio)
    print('Train Size', len(trainingSet), 'Test Size', len(testSet))
    summaries = summByClass(trainingSet)
    preds = getPreds(summaries, testSet)
    acc = getAccuracy(testSet, preds)
    print('Accuracy: ',acc)
In [15]:
main()
Train Size 514 Test Size 254 Accuracy: 52.75590551181102
In [ ]: