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) from MachineLearning.linear_regression import linear_regression, gaussian from scipy import matrix print "\n=== Linear Regression ===" x = [3, 4, 5, 6]
) 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 x = [0, 1, 2, 3, 4] y = [3, 6, 7, 8, 11] (w0, w1), err = linear_regression(x, y) print "(w0=%.1f, w1=%.1f) err=%.2f" % (w0, w1, err)
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) from MachineLearning.linear_regression import linear_regression, gaussian from scipy import matrix print "\n=== Linear Regression ==="