
13.5
モデルの適用
■
195
assert model.tokens == {"spam", "ham", "rules", "hello"}
assert model.spam_messages == 1
assert model.ham_messages == 2
assert model.token_spam_counts == {"spam": 1, "rules": 1}
assert model.token_ham_counts == {"ham": 2, "rules": 1, "hello": 1}
次に、予測を行いましょう。(面倒ですが)ナイーブベイズロジックを手作業で実行
し、同じ結果が得られることを確認します。
text = "hello spam"
probs_if_spam = [
(1 + 0.5) / (1 + 2 * 0.5), # "spam"
(存在する)
1 - (0 + 0.5) / (1 + 2 * 0.5), # "ham"
(存在しない)
1 - (1 + 0.5) / (1 + 2 * 0.5), # "rules"
(存在しない)
(0 + 0.5) / (1 + 2 * 0.5) # "hello"
(存在する)
]
probs_if_ham = [
(0 + 0.5) / (2 + 2 * 0.5), # "spam"
(存在する)
1 - (2 + 0.5) / (2 + 2 * 0.5), # "ham"
(存在しない)
1 - (1 + 0.5) ...