MODELCATALOG

Probabilistic, absurdly fast, surprisingly hard to beat on text.

sklearn.naive_bayes

naive_bayes

Probabilistic, absurdly fast, surprisingly hard to beat on text.

1 Get data Something to practise on
2 Split & tune An honest test set
3 Prepare Into numbers a model accepts
4 Compose One object, no leakage
5 Model The algorithm itself
6 Unsupervised No labels available
7 Measure Is it any good, and why
8 Utilities Glue and extension

What it is for

Assumes features are independent given the class, which is false for text and works anyway. Trains in seconds on data that would take a transformer hours.

Contents

MultinomialNB common Text with counts. The classic fast baseline
ComplementNB Text with imbalanced classes — usually better than Multinomial there
GaussianNB Continuous features
BernoulliNB Binary features
CategoricalNB Categorical features

Worked examples 1

Runnable, with the arguments that change the outcome called out. Output shapes are shown as comments where they help.

The fastest useful text baseline there is

Trains in seconds on data that would take a transformer hours. Worth running before you provision anything.

from sklearn.naive_bayes import MultinomialNB, ComplementNB
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.pipeline import make_pipeline
from sklearn.metrics import classification_report

# ComplementNB is designed for imbalanced text and usually beats
# MultinomialNB there - an easy swap most people never try.
for NB in (MultinomialNB, ComplementNB):
    pipe = make_pipeline(TfidfVectorizer(ngram_range=(1, 2), min_df=2), NB())
    pipe.fit(X_train, y_train)
    print(NB.__name__)
    print(classification_report(y_test, pipe.predict(X_test), digits=3))

# Set your expectations from this number. If a fine-tuned transformer
# is not clearly beating it, the transformer is not earning its
# serving cost.