반응형
1. 나이브 베이즈 확률
def main():
sensitivity = float(input())
prior_prob = float(input())
false_alarm = float(input())
print("%.2lf%%" % (100 * mammogram_test(sensitivity, prior_prob, false_alarm)))
def mammogram_test(sensitivity, prior_prob, false_alarm):
p_a1_b1 = sensitivity # p(A = 1 | B = 1)
p_b1 = prior_prob # p(B = 1)
p_b0 = 1-prior_prob # p(B = 0)
p_a1_b0 = false_alarm # p(A = 1|B = 0)
p_a1 = p_b0*p_a1_b0+p_b1*p_a1_b1 # p(A = 1)
p_b1_a1 = p_a1_b1*p_b1/p_a1 # p(B = 1|A = 1)
return p_b1_a1
if __name__ == "__main__":
main()
2. 나이브베이즈 분류기
# [나이브베이즈 분류기]
import re
import math
import numpy as np
def main():
M1 = {'r': 0.7, 'g': 0.2, 'b': 0.1} # M1 기계의 사탕 비율
M2 = {'r': 0.3, 'g': 0.4, 'b': 0.3} # M2 기계의 사탕 비율
test = {'r': 4, 'g': 3, 'b': 3}
print(naive_bayes(M1, M2, test, 0.7, 0.3))
def naive_bayes(M1, M2, test, M1_prior, M2_prior):
a=M1_prior*((M1["r"]**test["r"])*(M1["g"]**test["g"])*(M1["b"]**test["b"]))
b=M2_prior*((M2["r"]**test["r"])*(M2["g"]**test["g"])*(M2["b"]**test["b"]))
mySum=a+b
return [a/mySum, b/mySum]
if __name__ == "__main__":
main()
3. 감정분석기 bag of words
# [감정분석기 bag of Words]
import re
special_chars_remover = re.compile("[^\w'|_]")
def main():
sentence = input()
bow = create_BOW(sentence)
print(bow)
def create_BOW(sentence):
sentence=sentence.lower()
sentence=remove_special_characters(sentence)
sentence=sentence.split()
bag={}
for word in sentence:
if len(word)<1:
continue
if word in bag:
bag[word]+=1
else:
bag[word]=1
return bag
def remove_special_characters(sentence):
return special_chars_remover.sub(' ', sentence)
if __name__ == "__main__":
main()
반응형