# Is Naive Bayes Classification Supervised Or Unsupervised?

What kind of learning is naive Bayes classification—and why?

Naive Bayes classification is a form of supervised learning. It is considered to be supervised since naive Bayes classifiers are trained using labeled data, ie. data that has been pre-categorized into the classes that are available for classification.

This contrasts with unsupervised learning, where there is no pre-labeled data available. This type of learning seeks to find natural structures that may be present in a data set, with no pre-determined knowledge about how to classify the data.

The training of a simple naive Bayes classifier involves learning the probabilities that underpin the classification tasks that the classifier performs. Using pre-labeled data, these probabilities may be estimated from frequency counts of the data, based on which classes they fall into (ie. based on their pre-labeled classes).

More complex Bayes classification models assume generative distributions for the data being classified. Their training may involve learning the parameters of the assumed distributions rather than raw frequency counts.

## Simple naive Bayes classification

Naive Bayes classifiers are often used for text classification tasks. A popular application of naive Bayes, for instance, is classifying emails into spam (ie. emails that you don’t wish to read) and ham (ie. emails that you do wish to read).

Naive Bayes classifiers assume conditional independence amongst the features (or input variables) of the data. This makes calculations much easier. In the case of simple text classification, this translates to assuming that the words in the text documents being analyzed are independent of each other within each class.

The conditional independence assumption, however, rarely holds true in the real world. Yet, naive Bayes classifiers perform surprisingly well in practice. To understand why this is the case, here’s a common-sense explanatory article.

## Naive Bayes supervised learning in action—A simple illustration

To illustrate how naive Bayes classifiers learn through supervision, let’s look at a simple example.

### Naive Bayes text classification

We’ll consider a text classification task. Say, we want to classify emails into one of two classes: Work-Related (WIP) or Personal.

Naive Bayes classifiers can learn as more and more (labeled) data is passed to them for training. To keep things simple, in our example we’ll assume that there are only 5 words in our vocabulary: “chores”, “pending”, “action”, “this” and “morning”.

Our goal will be to classify an email containing the single phrase “chores pending action” as either WIP or Personal.

This is an interesting task because this phrase could easily be interpreted as either work-related or personal—have you ever had chores to do at either work or at home, for instance?

In this example we’ll see how this phrase (or more specifically, the email containing this phrase) can be classified based on the supervised training of the classifier.

We’ll also see how this classification can change with updated data—as new training data is received, the classification decision can change to reflect the updated supervised learning of the classifier.

### Training data—First batch

Let’s consider a first batch of 100 emails for training our classifier.

Since the learning is supervised, each email is labeled with a class, either WIP or Personal.

Let’s assume there are 40 WIP emails and 60 Personal emails in the first batch.

We’ll also assume that we have the following frequency counts, ie. the number emails—classed as WIP or Personal—that include words from our vocabulary in them:

Using Bayes theorem (see this article for an introductory explanation of Bayes theorem), we can find the probability that an email belongs to certain classgiven that it has a certain word in it—as follows:

P(class | word) = [ P(word | class) * P(class) ] / P(word)

So, the probability that an email containing the phrase “chores pending action” (and nothing else) belongs to a certain class is:

P(class | chores pending action) = [ P(chores pending action | class) * P(class) ] / P(chores pending action)

This can be applied to each class, hence:

P(WIP | chores pending action) = [ P(chores pending action | WIP) * P(WIP) ] / P(chores pending action)

And,

P(Personal | chores pending action) = [ P(chores pending action | Personal) * P(Personal) ] / P(chores pending action)

We can calculate these using the data from our first training batch, as follows (for a detailed step-through of how this process works, see this example):

Note the role that the conditional independence assumption plays—without it, calculating P(chores pending action | class) would be far more difficult than the simple multiplications shown above.

We can now classify our email as either WIP or Personal depending on which has the higher probability, given that it contains the phrase “chores pending action” and nothing else.

Based on our calculations:

P(WIP | chores pending action) = 0.65

And,

P(Personal | chores pending action) = 0.35

So, we can classify our email as WIP.

### Training data—Second Batch

Let’s consider adding another batch of 100 emails to our training data—batch 2. Assume there are 55 WIP emails and 45 Personal emails in this second batch.

Assume we have the following frequency counts for batch 2:

If we add the frequency counts for batches 1 and 2 together, or in other words if we update our batch 1 frequency counts with the new data in batch 2, we’ll get the following frequency counts:

Using the same process as before, we can now calculate updated probabilities for the classification of our email as follows:

Based on our updated calculations:

P(WIP | chores pending action) = 0.53

And,

P(Personal | chores pending action) = 0.47

So, we still classify our email as WIP.

Notice, however, that the probabilities for each class have changed—this is the process of learning using labeled data, ie. supervised learning.

### Training data—Third Batch

Let’s now add one more batch of 100 emails to our training data—batch 3. Assume there are 45 WIP emails and 55 Personal emails in this third batch.

Assume we have the following frequency counts for batch 3:

As before, we’ll add the frequency counts for batch 3 to those of 1 and 2, ie. we’ll update our frequency counts with the new data in batch 3.

Once we do this, we’ll get the following updated frequency counts:

Again, using the same process as before we can calculate updated probabilities for the classification of our email:

Based on our updated calculations:

P(WIP | chores pending action) = 0.48

And,

P(Personal | chores pending action) = 0.52

We now see that the classification for our email changes—from WIP to Personal—based on the updated supervised learning of our classifier.

## Labeled training data for supervised learning

This example shows how supervised learning works for simple naive Bayes classification.

The important thing to note is that the training data is labeled with the classes that each data point belongs to. This labeling guides—or supervises—the training of the classifier, since the classifier will learn to classify new data based on these labels.

## In summary

• Naive Bayes classification is a form of supervised learning—it uses training data that is pre-labeled with the available classifications
• Unsupervised learning, in contrast to naive Bayes classification, does not use pre-labeled data and has no pre-determined knowledge about how to classify the data
• Naive Bayes classification assumes conditional independence between input variables—this makes calculations much simpler
• Through supervised learning, simple naive Bayes classifiers learn the probabilities that underpin classification decisions (or the parameters of assumed generative distributions in more complex versions of naive Bayes classification)
• As more pre-labeled training data becomes available, naive Bayes classifiers can update their probability estimates through continued supervised learning