|
|
|
|
|
|
| dataset = [
|
| {"Assignment": "Good", "Project": "A", "Exam": "High", "Label": "Pass"},
|
| {"Assignment": "Good", "Project": "B", "Exam": "High", "Label": "Pass"},
|
| {"Assignment": "Bad", "Project": "B", "Exam": "Low", "Label": "Fail"},
|
| {"Assignment": "Bad", "Project": "C", "Exam": "High", "Label": "Fail"},
|
| {"Assignment": "Good", "Project": "C", "Exam": "Low", "Label": "Fail"},
|
| {"Assignment": "Good", "Project": "C", "Exam": "High", "Label": "Pass"},
|
| {"Assignment": "Bad", "Project": "B", "Exam": "High", "Label": "Pass"},
|
| {"Assignment": "Good", "Project": "A", "Exam": "Low", "Label": "Pass"},
|
| {"Assignment": "Bad", "Project": "A", "Exam": "Low", "Label": "Fail"},
|
| {"Assignment": "Good", "Project": "B", "Exam": "Low", "Label": "Pass"}
|
| ]
|
|
|
|
|
| def prior(c,ci):
|
| total = len(dataset)
|
| count = 0.0
|
| for student in dataset:
|
| if(student[c] is not None and student[c] == ci):
|
| count+=1
|
| return count/total
|
|
|
|
|
| def likelihood(f, fi, c, ci):
|
| c_count = 0.0
|
| f_count = 0.0
|
| for student in dataset:
|
| if(student[c] is not None and student[c] == ci):
|
| if(student[f] is not None and student[f] == fi):
|
| f_count+=1
|
| c_count+=1
|
| if(c_count > 0.0):
|
| return f_count/c_count
|
| return None
|
|
|
|
|
| def posterior(c,ci, feature_dictonary):
|
| p_c = prior(c, ci)
|
| for key in feature_dictonary.keys():
|
| p_c *= likelihood(key,feature_dictonary[key],c,ci)
|
| return p_c
|
|
|
| print("Probability for passing:{0}".format(posterior("Label","Pass",{"Assignment": "Bad", "Project": "A", "Exam": "High"})))
|
| print("Probability for failing:{0}".format(posterior("Label","Fail",{"Assignment": "Bad", "Project": "A", "Exam": "High"}))) |