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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.