Skip to content
StackDecode
AI & ML Atlas
Machine LearningalgorithmInteractive simulation

Multinomial Naive Bayes

Naive Bayes for word counts.

The idea

It treats a document as a bag of words and models how often each word appears in each class. A document's score is the log prior plus the sum of count × log P(word | class). Laplace smoothing adds 1 to every count so unseen words do not zero out the result.

P(w | c) = (count(w, c) + 1) / (total(c) + |V|)

Watch it run

This is a real computation, not a drawing. Pause, step, change the settings, or try new random data.

freemoneymeetingspam50%not spam50%
  • spam
  • not spam

A message arrives containing three words. Is it spam? Start with the prior: before reading, spam and not-spam are equally likely.

prior:
50%
1/5

Word frequencies come from a tiny training set of 6 spam and 6 normal messages, with add-one smoothing.

Step by step

  1. 1Count each word in the documents of each class.
  2. 2Smooth the counts and turn them into probabilities.
  3. 3For a new document sum log P(word | class) over its words.
  4. 4Add the log prior and pick the best class.

Where it is used

Spam detection, topic and sentiment classification.

Watch out for

Ignores word order and assumes independent words.

Related