Implementing Naïve Bayes with scikit-learn

Coding from scratch and implementing on your own solutions is the best way to learn about machine learning model. Of course, we can take a shortcut by directly using the MultinomialNB class from the scikit-learn API:

>>> from sklearn.naive_bayes import MultinomialNB

Let's initialize a model with a smoothing factor (specified as alpha in scikit-learn) of 1.0, and prior learned from the training set (specified as fit_prior in scikit-learn):

>>> clf = MultinomialNB(alpha=1.0, fit_prior=True)

To train the Naïve Bayes classifier with the fit method, use the following command:

>>> clf.fit(term_docs_train, Y_train)

And to obtain the prediction results with the predict_proba method, use the following commands: ...

Get Python Machine Learning By Example - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.