-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive-bayes-scratch.py
110 lines (94 loc) · 3.27 KB
/
naive-bayes-scratch.py
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
99
100
101
102
103
104
105
106
107
108
109
'''
This python code is re-modeled for Python3, based on
http://machinelearningmastery.com/naive-bayes-classifier-scratch-python/
'''
import csv
import random
import math
import sys
# Reading .csv & performing clean-up
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
#splitting into train & test
def splitDataset(dataSet,splitRatio):
trainSize = int(len(dataSet)*splitRatio)
trainSet=[]
testSet=list(dataSet)
while (len(trainSet)<trainSize):
index = random.randrange(len(testSet))
trainSet.append(testSet.pop(index))
return trainSet,testSet
#seperate training data to dictionary for fishy websites(1) & for non-fishy websites(0)
def seperateByClass(dataSet):
seperated={}
for i in range(len(dataSet)):
vector = dataSet[i]
if (vector[-1] not in seperated):
seperated[vector[-1]]=[]
seperated[vector[-1]].append(vector)
return seperated
#summarizing each attribute in the dataset
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 summarizeByClass(dataset):
seperated = seperateByClass(dataset)
summaries = {}
for classval, instances in seperated.items():
summaries[classval] = summarize(instances)
return summaries
#making predictions
def calculateProbability(x, mean, stdev):
if stdev==0: return 0
else:
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 i in range(len(testSet)):
if testSet[i][-1] == predictions[i]:
correct += 1
return (correct/float(len(testSet))) * 100.0
#main function
filename = 'fishy_websites.csv' #Dataset name
dataset = loadCsv(filename)
trainSet, testSet = splitDataset(dataset, 0.75) #functionality same as train_test_split() from sklearn
seperated = seperateByClass(trainSet)
summary = summarizeByClass(trainSet)
predictions = getPredictions(summary, testSet)
accuracy = getAccuracy(testSet, predictions)
print('Accuracy: '+ str(round(accuracy,2))+"%" )