Skip to content

Instantly share code, notes, and snippets.

@Hussain-Safwan
Last active September 19, 2022 14:37
Show Gist options
  • Save Hussain-Safwan/551cbc1c8e21c8b9934bbe38422f8e5e to your computer and use it in GitHub Desktop.
Save Hussain-Safwan/551cbc1c8e21c8b9934bbe38422f8e5e to your computer and use it in GitHub Desktop.
Naive Bayes Classifier
no fever cough respiratory_problems runny_nose throat_ache tested
1 0 1 0 1 0 0
2 0 1 0 0 1 1
3 1 0 1 1 1 1
4 1 1 0 1 0 0
5 1 1 0 0 1 1
6 1 0 0 1 1 1
7 0 0 0 0 0 0
8 0 0 0 1 1 0
9 0 0 0 0 0 0
10 1 1 0 1 1 0
import pandas as pd
dataFile = pd.read_csv('data.csv')
df = dataFile.copy()
dataArray = df.values
pos = 0
neg = 0
total = 10
for i in range(len(dataArray)):
if dataArray[i][6] == 1.0:
pos+=1
else:
neg+=1
pPos = pos/total
pNeg = neg/total
prob = []
for i in range(1, 6):
pos = 0
neg = 0
for j in range(len(dataArray)):
if (dataArray[j][i] == dataArray[j][6]):
pos += 1
else:
neg += 1
pos = pos/total
neg = neg/total
prob.append({'pos': pos, 'neg': neg})
test_pos = 1
test_neg = 1
for i in range(len(prob)):
test_pos = test_pos * prob[i]['pos']
test_neg = test_neg * prob[i]['neg']
def isInfected(case):
boolMap = {0: 'pos', 1: 'neg'}
evidence = 1
for i in range(len(case)):
_bool = boolMap[case[i]]
evidence = evidence * prob[i][_bool]
if (test_neg / evidence > test_pos / evidence):
return 0
else:
return 1
print(isInfected(case = [1, 0, 0, 1, 1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment