Naive Bayes for the Superbowl

22
Naïve Bayes for the Superbowl John Liu Nashville Machine Learning Meetup January 27 th , 2015

Transcript of Naive Bayes for the Superbowl

Page 1: Naive Bayes for the Superbowl

Naïve Bayes for the Superbowl John Liu Nashville Machine Learning Meetup January 27th, 2015

Page 2: Naive Bayes for the Superbowl

NashML Goals

Create a hub for like-minded people to come together, share knowledge and collaborate on interesting domains.

Experienced MLers

ML Hackers

Topic Presentations

Collaborative Practice

Common Platform

Page 3: Naive Bayes for the Superbowl

Platform

! IPython Notebook (Project Jupyter)

!   Java, Scala, Python (others?)

! Scikit-learn

!   PyLearn2/Theano

! iTorch

!   AWS/Mahout/Spark/Mllib?

Page 4: Naive Bayes for the Superbowl

Rev. Thomas Bayes

“An Essay towards solving a Problem in the Doctrine of Chances” published posthumously 1763

I now send you an essay which I have found among the papers of our deceased friend Mr. Bayes, and which, in my opinion, has great merit, and well deserves to be preserved. Experimental philosophy, you will find, is nearly interested in the subject of it; and on this account there seems to be particular reason for thinking that a communication of it to the Royal Society cannot be improper…

Page 5: Naive Bayes for the Superbowl

Bayes’ Theorem

P(A | B)P(B) = P(A∩B) = P(B | A)P(A)

P(A | B) = P(A∩B)P(B)

P(A | B) = P(B | A)P(A)P(B)

Page 6: Naive Bayes for the Superbowl

Statistical Inference

P(A | B) = P(B | A)P(A)P(B)

Likelihood Prior

Evidence Posterior (What we want to find)

Page 7: Naive Bayes for the Superbowl

Intuition Behind Bayes

A Priori Initial Belief (model)

Evidence See the Data

Likelihood How likely to see Data given Belief

A Posteriori Updated Belief after seeing Data

posterior = prior• likelihoodevidence

Page 8: Naive Bayes for the Superbowl

Example

Blackjack Insurance Bet:

What is the probability that a dealer with an Ace showing has Blackjack?

Page 9: Naive Bayes for the Superbowl

Example

Prior P(Dealer has Blackjack) 32/663

Evidence P(Ace showing) 1/13

Likelihood P(Ace showing|has Blackjack) 1/2

Posterior P(Blackjack|Ace showing) 16/51

16/51 = 31% or less than 1/3 of the time.

Page 10: Naive Bayes for the Superbowl

Are you a Bayesian?

You read Burton Malkiel’s book “A Random Walk down Wall Street” and believe in the Efficient Market Hypothesis. Your broker gives you a tip to buy Tesla. You ignore the broker and Tesla rises 100 days in a row.

As a Bayesian, do you believe:

A)  The stock is long due for a correction

B)  It is possible for Tesla to rise another 100 days in a row

C)  You were fooled by randomness

Page 11: Naive Bayes for the Superbowl

Naïve Bayes

You observe outcome Y with some n features X1, X2, ..Xn. The joint density can be expressed using the chain rule:

P(Y, X1, X2, ..Xn) = P(X1, X2, ..Xn|Y) P(Y)

= P(Y) P(X1,Y) P(X2|Y,X1) P(X3|Y,X1,X2)…

This is messy, but simplifies if we naively assume independence,

P(X2|Y,X1) = P(X2|Y)

P(X3|Y,X2,X1) = P(X3|Y)

P(Xn|Y,Xn…X2,X1) = P(Xn|Y)

Naïve Bayes Assumption

Page 12: Naive Bayes for the Superbowl

Naïve Bayes Classifier

Let K classes be denoted ck. The (conditional) probability of class ck given that we observed features x1, x2,..xn is:

A Naïve Bayes classifier simply chooses the class with highest probability (maximum a posteriori):

P(ck | x1, x2, ..xn ) = P(ck ) P(xi | ck )i=1

n

cNB = argmaxk∈K

P(ck ) P(xi | ck )i=1

n

Page 13: Naive Bayes for the Superbowl

Gaussian Naïve Bayes

When features xi are continuous valued, typically make the assumption they are normally distributed:

Variance σki can be independent of xi and/or ck.

P(xi | ck ) =12πσ ki

2e−12xi−µkiσ ki

"

#$

%

&'2

cNB = argmaxk∈K

P(ck ) P(xi | ck )i=1

n

Page 14: Naive Bayes for the Superbowl

Multinomial Naïve Bayes

When features xi are the number of occurrences of n possible events (words, votes, etc…)

pki = probability of i-th event occuring in class k

xi = frequency of i-th event

The multinomial Naïve Bayes classifier becomes:

cNB = argmaxk∈K

logP(ck )+ xi • log pkii=1

n

∑#

$%

&

'(

Page 15: Naive Bayes for the Superbowl

Naïve Bayes Intuition

!   Assumes all features are independent with each other

!   Independence assumption decouples the individual distributions for each feature

!   Decoupling can overcome curse of dimensionality

!   Performance robust to irrelevant features

!   Very fast, low storage footprint

!   Good performance with multiple equally important features

Page 16: Naive Bayes for the Superbowl

Naïve Bayes Applications

!   Document Categorization

!   NLP

!   Email Sorting

!   Collaborative Filtering

!   Sports Prediction

!   Sentiment Analysis

Page 17: Naive Bayes for the Superbowl

Example: Doc Classification

Want to classify documents into k topics. Document d consisting of words wi is assigned to the topic cNB:

P(ck) = topic frequency = P(wi|ck) = word wi frequency in all topic ck docs

cNB = argmaxk∈K

P(ck ) P(wi | ck )i∏

Ndocs(topic=ck )

Ndocs

=Nword=wi (topic=ck )

Nword=wi (topic=ck )i∑

Page 18: Naive Bayes for the Superbowl

Laplace (add-1) Smoothing

What happens with P(wi|ck) = 0 for a particular i, k?

Solution is to add 1 to numerator & denominator:

P(ck | x1, x2, ..xn ) = P(ck ) P(xi | ck )i=1

n

∏ = 0!

P(wi | ck ) =Nword=wi (topic=ck )

+1Nword=wi (topic=ck )

+1( )i∑

Page 19: Naive Bayes for the Superbowl

NBC Application Roadmap

!   Read Dataset

!   Transform Dataset

!   Create Classifier

!   Train Classifier

!   Make Prediction

Page 20: Naive Bayes for the Superbowl

Sports Prediction

Who is favored to win the Superbowl?

Given the 2014 season game statistics for two teams, how can we make a prediction on the outcome of the next game using a Naïve Bayes classifier?

Page 21: Naive Bayes for the Superbowl

Sentiment Analysis

Which team is most favored in each state?

What if we analyzed tweets by sentiment and location using a Naïve Bayes Classifier?

Page 22: Naive Bayes for the Superbowl

Starter Code

Repo with Starter Code at:

https://github.com/guard0g/NaiveBayesForSuperbowl

IPython notebook: NB4Superbowl.ipynb

Datasets: SeattleStats.csv

NewEnglandStats.csv