Skip to content

Instantly share code, notes, and snippets.

@hoenirvili
Created November 9, 2017 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoenirvili/d9bef7fa2b4e6c9611fc5b7a24ee5b6a to your computer and use it in GitHub Desktop.
Save hoenirvili/d9bef7fa2b4e6c9611fc5b7a24ee5b6a to your computer and use it in GitHub Desktop.
naive bayes
x1 x2 y
0 0 0
0 0 0
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
1 0 0
1 0 0
1 0 0
1 0 0
1 0 1
0 1 0
0 1 0
0 1 0
0 1 0
0 1 1
1 1 0
1 1 0
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
#!/usr/bin/env python3
import csv
import sys
def main():
if len(sys.argv) < 2:
raise ValueError('Requires path to *.csv file')
name = sys.argv[1]
data = [] # the hole csv data
with open(name, 'r') as fd:
r = csv.reader(fd)
data = [d for d in r]
print("Attributes = {}".format(data[0][:-1]))
print("Classifier = {}".format(data[0][-1]))
instance_to_classify = ['0', '0']
y = naive_bayes(data[1:], instance_to_classify)
print("Instance to classify = {}".format(instance_to_classify))
print("Bayes naive decision for y = {}".format(y))
y = joint_bayes(data[1:], instance_to_classify)
print("Joint bayes decision for y = {}".format(y))
def column(data, column):
return [row[column] for row in data]
def naive_bayes(data, instance):
target_names = list(set(column(data, -1)))
mle = {}
for target_name in target_names:
targets = column(data, -1).count(target_name)
maximum_likelihood = 0
p = 1
for index, element in enumerate(instance):
nominator = 0
for row in data:
if (target_name == row[-1] and
element == row[index]):
nominator += 1
p = (p * (nominator / targets))
maximum_likelihood += (p * (targets/len(data)))
mle[target_name] = maximum_likelihood
return max(mle, key=mle.get)
def compare(one, two):
if len(one) != len(two):
raise ValueError("Lists needs to have the same length")
for i, j in zip(one, two):
if i != j:
return False
return True
def joint_bayes(data, instance):
target_names = list(set(column(data, -1)))
mle = {}
for target_name in target_names:
targets = column(data, -1).count(target_name)
maximum_likelihood = 0
nominator = 0
for row in data:
if (target_name == row[-1] and
compare(instance, row[:-1])):
nominator += 1
mle[target_name] = ((nominator/targets) * (targets/len(data)))
return max(mle, key=mle.get)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment