P(n, {"B":True}, {"C":True})
P(n, {"C":True}, {"B":True})

print "\n=== Problem 8 ==="
from MachineLearning.bayes import NaiveBayesClassifier, result
SPAM = (
    "Top Gun",
    "Shy People",
    "Top Hat",
)
HAM = (
    "Top Gear",
    "Gun Shy",
)
c = NaiveBayesClassifier(SPAM, HAM, 1)
result("OLD", c.spam.p)
result("Top|OLD", c.spam.p_word("Top"))
result("OLD|Top", c.p_spam_given_word("Top"))


print "\n=== Problem 10 ==="
from MachineLearning.linear_regression import linear_regression, gaussian
x = [1.0, 3.0, 4.0, 5.0,  9.0]
y = [2.0, 5.2, 6.8, 8.4, 14.8]
(w0, w1), err = linear_regression(x, y)
print "(w0=%.1f, w1=%.1f) err=%.2f" % (w0, w1, err)


print "\n=== Problem 12 ==="
from Logic.logic import Proposition, implies
print Proposition(
MOVIE = (
    "a perfect world",
    "my perfect woman",
    "pretty woman"
)
SONG = (
    "a perfect day",
    "electric storm",
    "another rainy day"
)
c = NaiveBayesClassifier(MOVIE, SONG, 1)
print "Size of vocabulary: %d" % c.different_words

print "\n=== Homework 3.1 ==="
result("MOVIE", c.spam.p)
result("SONG", c.ham.p)
result("perfect|MOVIE", c.spam.p_word("perfect"))
result("perfect|SONG",  c.ham.p_word("perfect"))
result("storm|MOVIE", c.spam.p_word("storm"))
result("storm|SONG",  c.ham.p_word("storm"))

print "\n=== Homework 3.2 ==="
result("MOVIE|perfect storm)", c.p_spam_given_phrase("perfect storm"))

print "\n=== Homework 3.3 ==="
c = NaiveBayesClassifier(MOVIE, SONG)
result("MOVIE|perfect storm)", c.p_spam_given_phrase("perfect storm"))

print "\n=== Homework 3.4 ==="
from MachineLearning.linear_regression import linear_regression
示例#3
0
    "offer is secret",
    "click secret link",
    "secret sports link",
)
HAM = (
    "play sports today",
    "went play sports",
    "secret sports event",
    "sports is today",
    "sports costs money",
)

print "=== Naive Bayes CLassifier ==="
c = NaiveBayesClassifier(SPAM, HAM)
print "Size of vocabulary: %d" % c.different_words
result("SPAM", c.spam.p, 0.3750)
result("secret|SPAM", c.spam.p_word("secret"), 0.3333)
result("secret|HAM", c.ham.p_word("secret"), 0.0667)
result("SPAM|sports", c.p_spam_given_word("sports"), 0.1667)
result("SPAM|secret is secret)", c.p_spam_given_phrase("secret is secret"),
       0.9615)
result("SPAM|today is secret)", c.p_spam_given_phrase("today is secret"), 0)

print "\n=== Naive Bayes CLassifier with Laplace Smoothing ==="
c = NaiveBayesClassifier(SPAM, HAM, 1)
result("SPAM", c.spam.p, 0.4)
result("HAM", c.ham.p, 0.6)
result("today|SPAM", c.spam.p_word("today"), 0.0476)
result("today|HAM", c.ham.p_word("today"), 0.1111)
result("SPAM|today is secret)", c.p_spam_given_phrase("today is secret"),
       0.4858)
    "offer is secret",
    "click secret link",
    "secret sports link",
)
HAM = (
    "play sports today",
    "went play sports",
    "secret sports event",
    "sports is today",
    "sports costs money",
)

print "=== Naive Bayes CLassifier ==="
c = NaiveBayesClassifier(SPAM, HAM)
print "Size of vocabulary: %d" % c.different_words
result("SPAM", c.spam.p, 0.3750)
result("secret|SPAM", c.spam.p_word("secret"), 0.3333)
result("secret|HAM",  c.ham.p_word("secret"), 0.0667)
result("SPAM|sports", c.p_spam_given_word("sports"), 0.1667)
result("SPAM|secret is secret)", c.p_spam_given_phrase("secret is secret"), 0.9615)
result("SPAM|today is secret)", c.p_spam_given_phrase("today is secret"), 0)

print "\n=== Naive Bayes CLassifier with Laplace Smoothing ==="
c = NaiveBayesClassifier(SPAM, HAM, 1)
result("SPAM", c.spam.p, 0.4)
result("HAM", c.ham.p, 0.6)
result("today|SPAM", c.spam.p_word("today"), 0.0476)
result("today|HAM",  c.ham.p_word("today"), 0.1111)
result("SPAM|today is secret)", c.p_spam_given_phrase("today is secret"), 0.4858)

P(n, {"C": True}, {"B": True})

print "\n=== Problem 8 ==="
from MachineLearning.bayes import NaiveBayesClassifier, result

SPAM = (
    "Top Gun",
    "Shy People",
    "Top Hat",
)
HAM = (
    "Top Gear",
    "Gun Shy",
)
c = NaiveBayesClassifier(SPAM, HAM, 1)
result("OLD", c.spam.p)
result("Top|OLD", c.spam.p_word("Top"))
result("OLD|Top", c.p_spam_given_word("Top"))

print "\n=== Problem 10 ==="
from MachineLearning.linear_regression import linear_regression, gaussian

x = [1.0, 3.0, 4.0, 5.0, 9.0]
y = [2.0, 5.2, 6.8, 8.4, 14.8]
(w0, w1), err = linear_regression(x, y)
print "(w0=%.1f, w1=%.1f) err=%.2f" % (w0, w1, err)

print "\n=== Problem 12 ==="
from Logic.logic import Proposition, implies

print Proposition(lambda a: not a, "not a").satisfiability_report()