Upcoming SlideShare
×

# Classification with Naive Bayes

27,885 views

Published on

A Deep Dive into Classification with Naive Bayes. Along the way we take a look at some basics from Ian Witten's Data Mining book and dig into the algorithm.

Presented on Wed Apr 27 2011 at SeaHUG in Seattle, WA.

19 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
27,885
On SlideShare
0
From Embeds
0
Number of Embeds
41
Actions
Shares
0
673
0
Likes
19
Embeds 0
No embeds

No notes for slide
• https://cwiki.apache.org/MAHOUT/books-tutorials-and-talks.html
• Contrasts with “1Rule” method (1Rule uses 1 attribute)NB allows all attributes to make contributions that are equally important and independent of one another
• This classifier produces a probability estimate for each class rather than a predictionConsidered “Supervised Learning”
• comparison with other classification methods in 2006 showed that Bayes classification is outperformed by more current approaches, such as boosted trees or random forestsAn advantage of the naive Bayes classifier is that it requires a small amount of training data to estimate the parameters (means and variances of the variables) necessary for classification.
• Pr[E|H] -&gt; all evidence for instances with H-&gt;”yes”Pr[H] -&gt; percent of instances w/ this outcomePr[E] -&gt; sum of the values ( ) for all outcomes
• Book reference: snow crashFor each attribute “a” there are multiple values, and given these combinations we need to look at how many times the instances were actually classified each class.In training we use the term “outcome”, in classification we use the term “class”Example: say we have 2 attributes to an instance
• We don’t take into account some of the other things like “missing values” here
• Now that we’ve established the case for Naïve Bayes + Text  show how it fits in with other classifications algos
• *** Need to sell case for using another feature calculating mechanic ***when one class has more training examples than anotherNaive Bayes selects poor weights for the decision boundary. To balance the amount of training examples used per estimatethey introduced a “complement class” formulation of Naive Bayes.A document is treated as a sequence of words and it is assumed that each word position is generated independently of every other word
• Term frequency =num occurrences of the considered term ti in document dj / sizeof ( words in doc dj )Normalized to protect against bias in larger docsIDF = log( Normalized Frequency for a term(feature) in a document is calculated by dividing the term frequency by the root mean square of terms frequencies in that documentWeight Normalized Tffor a given feature in a given label = sum of Normalized Frequency of the feature across all the documents in the label.
• Need to get a better handle on Sigma_kirSigmaWijhttps://cwiki.apache.org/MAHOUT/bayesian.html
• https://cwiki.apache.org/confluence/display/MAHOUT/Twenty+Newsgroups
• Can also test sequentially
• ### Classification with Naive Bayes

1. 1. Classification with Naïve Bayes<br />A Deep Dive into Apache Mahout<br />
2. 2. Today’s speaker – Josh Patterson<br />josh@cloudera.com / twitter: @jpatanooga<br />Master’s Thesis: self-organizing mesh networks<br />Published in IAAI-09: TinyTermite: A Secure Routing Algorithm<br />Conceived, built, and led Hadoop integration for the openPDC project at TVA (Smartgrid stuff)<br />Led small team which designed classification techniques for time series and Map Reduce<br />Open source work at http://openpdc.codeplex.com<br />Now: Solutions Architect at Cloudera<br />2<br />
3. 3. What is Classification?<br />Supervised Learning<br />We give the system a set of instances to learn from<br />System builds knowledge of some structure<br />Learns “concepts”<br />System can then classify new instances<br />
4. 4. Supervised vs Unsupervised Learning<br />Supervised<br />Give system examples/instances of multiple concepts<br />System learns “concepts”<br />More “hands on”<br />Example: Naïve Bayes, Neural Nets<br />Unsupervised<br />Uses unlabled data<br />Builds joint density model<br />Example: k-means clustering<br />
5. 5. Naïve Bayes<br />Called Naïve Bayes because its based on “Baye’sRule” and “naively” assumes independence given the label<br />It is only valid to multiply probabilities when the events are independent<br />Simplistic assumption in real life<br />Despite the name, Naïve works well on actual datasets<br />
6. 6. Naïve Bayes Classifier<br />Simple probabilistic classifier based on <br />applying Baye’s theorem (from Bayesian statistics) <br />strong (naive) independence assumptions. <br />A more descriptive term for the underlying probability model would be “independent feature model".<br />
7. 7. Naïve Bayes Classifier (2)<br />Assumes that the presence (or absence) of a particular feature of a class is unrelated to the presence (or absence) of any other feature. <br />Example: <br />a fruit may be considered to be an apple if it is red, round, and about 4" in diameter. <br />Even if these features depend on each other or upon the existence of the other features, a naive Bayes classifier considers all of these properties to independently contribute to the probability that this fruit is an apple.<br />
8. 8. A Little Bit o’ Theory<br />
9. 9. Condensing Meaning<br />To train our system we need<br />Total number input training instances (count)<br />Counts tuples: <br />{attributen,outcomeo,valuem} <br />Total counts of each outcomeo<br />{outcome-count}<br />To Calculate each Pr[En|H]<br />({attributen,outcomeo,valuem} / {outcome-count} )<br />…From the Vapor of That Last Big Equation<br />
10. 10. A Real Example From Witten, et al<br />
11. 11. Enter Apache Mahout<br />What is it?<br />Apache Mahout is a scalable machine learning library that supports large data sets<br />What Are the Major Algorithm Type?<br />Classification<br />Recommendation<br />Clustering<br />http://mahout.apache.org/<br />
12. 12. Mahout Algorithms<br />
13. 13. Naïve Bayes and Text<br />Naive Bayes does not model text well. <br />“Tackling the Poor Assumptions of Naive Bayes Text Classifiers”<br />http://people.csail.mit.edu/jrennie/papers/icml03-nb.pdf<br />Mahout does some modifications based around TF-IDF scoring (Next Slide)<br />Includes two other pre-processing steps, common for information retrieval but not for Naive Bayes classification<br />
14. 14. High Level Algorithm<br />For Each Feature(word) in each Doc:<br />Calc: “Weight Normalized Tf-Idf”<br />for a given feature in a label is the Tf-idf calculated using standard idf multiplied by the Weight Normalized Tf<br />We calculate the sum of W-N-Tf-idf for all the features in a label called Sigma_k, and alpha_i == 1.0<br />Weight = Log [ ( W-N-Tf-Idf + alpha_i ) / ( Sigma_k + N ) ]<br />
15. 15. BayesDriver Training Workflow<br />Naïve Bayes Training MapReduce Workflow in Mahout<br />
16. 16. Logical Classification Process<br />Gather, Clean, and Examine the Training Data<br />Really get to know your data!<br />Train the Classifier, allowing the system to “Learn” the “Concepts”<br />But not “overfit” to this specific training data set<br />Classify New Unseen Instances<br />With Naïve Bayes we’ll calculate the probabilities of each class wrt this instance<br />
17. 17. How Is Classification Done?<br />Sequentially or via Map Reduce<br />TestClassifier.java<br />Creates ClassifierContext<br />For Each File in Dir<br />For Each Line<br />Break line into map of tokens<br />Feed array of words to Classifier engine for new classification/label<br />Collect classifications as output<br />
18. 18. A Quick Note About Training Data…<br />Your classifier can only be as good as the training data lets it be…<br />If you don’t do good data prep, everything will perform poorly<br />Data collection and pre-processing takes the bulk of the time<br />
19. 19. Enough Math, Run the Code<br />Download and install Mahout<br />http://www.apache.org<br />Run 20Newsgroups Example<br />https://cwiki.apache.org/confluence/display/MAHOUT/Twenty+Newsgroups<br />Uses Naïve Bayes Classification<br />Download and extract 20news-bydate.tar.gz from the 20newsgroups dataset<br />
20. 20. Generate Test and Train Dataset<br />Training Dataset:<br />mahout org.apache.mahout.classifier.bayes.PrepareTwentyNewsgroups <br /> -p examples/bin/work/20news-bydate/20news-bydate-train <br /> -o examples/bin/work/20news-bydate/bayes-train-input <br /> -a org.apache.mahout.vectorizer.DefaultAnalyzer<br /> -c UTF-8<br />Test Dataset:<br />mahout org.apache.mahout.classifier.bayes.PrepareTwentyNewsgroups <br /> -p examples/bin/work/20news-bydate/20news-bydate-test <br /> -o examples/bin/work/20news-bydate/bayes-test-input <br /> -a org.apache.mahout.vectorizer.DefaultAnalyzer <br /> -c UTF-8<br />
21. 21. Train and Test Classifier<br />Train:<br />\$MAHOUT_HOME/bin/mahout trainclassifier <br /> -i 20news-input/bayes-train-input <br /> -o newsmodel <br /> -type bayes <br /> -ng 3 <br /> -source hdfs<br />Test:<br />\$MAHOUT_HOME/bin/mahout testclassifier <br /> -m newsmodel <br /> -d 20news-input <br /> -type bayes <br /> -ng 3 <br /> -source hdfs <br /> -method mapreduce<br />
22. 22. Other Use Cases<br />Predictive Analytics<br />You’ll hear this term a lot in the field, especially in the context of SAS<br />General Supervised Learning Classification<br />We can recognize a lot of things with practice<br />And lots of tuning!<br />Document Classification<br />Sentiment Analysis<br />
23. 23. Questions?<br />We’re Hiring!<br />Cloudera’sDistro of Apache Hadoop:<br />http://www.cloudera.com<br />Resources<br />“Tackling the Poor Assumptions of Naive Bayes Text Classifiers”<br />http://people.csail.mit.edu/jrennie/papers/icml03-nb.pdf<br />