Classifying documents with Naive Bayes
Naive Bayes is a really interesting model. It's somewhat similar to KNN in the sense that it makes some assumptions that might oversimplify reality, but still it performs well in many cases.
Getting ready
In this recipe, we'll use Naive Bayes to do document classification with sklearn
. An example I have personal experience of is using a word that makes up an account descriptor in accounting, such as accounts payable, and determining if it belongs to the income statement, cash flow statement, or balance sheet.
The basic idea is to use the word frequency from a labeled test corpus to learn the classifications of the documents. Then, we can turn it on a training set and attempt to predict the label.
We'll use the newgroups
dataset within sklearn
to play with the Naive Bayes model. It's a non-trivial amount of data, so we'll fetch it instead of loading it. We'll also limit the categories to rec.autos
and rec.motorcycles
:
import numpy as np from sklearn.datasets...