Multinomial Naive Bayes is a fast, surprisingly strong baseline for text classification such as spam detection or ticket routing.
Implement a class NaiveBayes(alpha=1.0):
fit(docs, labels): tokenise each document by lowercasing and splitting on whitespace. Learn:
self.predict(doc): return the class maximising over the document's words, counting repeats. Ignore words not in the vocabulary. Break ties by the class that sorts first.Work in log space so long documents don't underflow.