Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. def filterFun(msg,spam,ham): prob_spam = float(len(spam))/(len(spam)+len(ham)) prob_ham = 1-prob_spam spam_ele_dict = {} ham_ele_dict = {} # Set is used to handle condition 4 of counting the numbers only once for ele in list(set(msg)): spam_ele_dict[ele] = 0 ham_ele_dict[ele] = 0 for sp in spam: if (ele in sp): spam_ele_dict[ele] += 1.0 for hp in ham: if (ele in hp): ham_ele_dict[ele] += 1.0 for key,value in spam_ele_dict.iteritems(): spam_ele_dict[key] = value/len(spam) for key,value in ham_ele_dict.iteritems(): ham_ele_dict[key] = value/len(ham) prob_ele_spam = prob_spam prob_ele_ham = prob_ham for ele in list(set(msg)): prob_ele_spam *= spam_ele_dict[ele] prob_ele_ham *= ham_ele_dict[ele] prob_msg_spam = prob_ele_spam / (prob_ele_spam + prob_ele_ham) prob_msg_ham = prob_ele_ham / (prob_ele_spam + prob_ele_ham) # Uncomment to look at the actual values # print ("Probability of msg being spam = ",prob_msg_spam) # print ("Probability of msg being ham = ",prob_msg_ham) return (prob_msg_spam>prob_msg_ham) msg = [1,2] spam = [[0,1],[0,2],[0,3]] ham = [[1],[1,2],[0,2]] print (filterFun(msg,spam,ham)) msg = [0,1] spam = [[0,1,2],[0,1,3],[2,3,4]] ham = [[2,3,5],[6,1,0]] print (filterFun(msg,spam,ham))
  2.  
  3. Sample Output:
  4.  
  5. ('Probability of msg being spam = ', 0.19999999999999998)
  6. ('Probability of msg being ham = ', 0.7999999999999999)
  7. False
  8.  
  9.  
  10. ('Probability of msg being spam = ', 0.7272727272727273)
  11. ('Probability of msg being ham = ', 0.2727272727272728)
  12. True
  13.  
  14. 2 Comments
  15.  
  16. Comments
  17.  
  18. Anonymous posted 6 months ago
  19.  
  20. for python 3 use dict.items() rather than dict.iteritems() for lines 18 and 20, in 3 they got rid of that particular function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement