machine_learning.naive_bayes_text_classification

Naive Bayes text classification using a multinomial event model.

The implementation in this module is intentionally educational and keeps the logic explicit: token counting, prior probabilities, and posterior scoring in log-space.

References: - https://en.wikipedia.org/wiki/Naive_Bayes_classifier - https://scikit-learn.org/stable/modules/naive_bayes.html

Attributes

classifier

Classes

NaiveBayesTextClassifier

Multinomial Naive Bayes classifier for short text documents.

Functions

build_toy_dataset(→ tuple[list[str], list[str]])

Build a tiny text dataset for examples and quick local testing.

Module Contents

class machine_learning.naive_bayes_text_classification.NaiveBayesTextClassifier(alpha: float = 1.0)

Multinomial Naive Bayes classifier for short text documents.

Args:

alpha: Additive (Laplace) smoothing parameter. Must be greater than 0.

>>> NaiveBayesTextClassifier(alpha=0)
Traceback (most recent call last):
...
ValueError: alpha must be greater than 0.
static _tokenize(text: str) list[str]

Split text into lowercase alphanumeric tokens.

>>> NaiveBayesTextClassifier._tokenize("Hello, NLP world!")
['hello', 'nlp', 'world']
fit(texts: list[str], labels: list[str]) None

Fit the classifier from labeled training texts.

>>> model = NaiveBayesTextClassifier()
>>> model.fit(["cheap meds", "project meeting"], ["spam", "ham"])
>>> sorted(model.classes_)
['ham', 'spam']
>>> model.fit(["only one text"], ["ham", "spam"])
Traceback (most recent call last):
...
ValueError: texts and labels must have the same length.
>>> model.fit([], [])
Traceback (most recent call last):
...
ValueError: training data must not be empty.
predict(text: str) str

Predict the most likely class label for a text.

>>> train_texts, train_labels = build_toy_dataset()
>>> model = NaiveBayesTextClassifier(alpha=1.0)
>>> model.fit(train_texts, train_labels)
>>> model.predict("free cheap meds")
'spam'
>>> model.predict("project meeting schedule")
'ham'
predict_proba(text: str) dict[str, float]

Return posterior probabilities for every class.

>>> train_texts, train_labels = build_toy_dataset()
>>> model = NaiveBayesTextClassifier()
>>> model.fit(train_texts, train_labels)
>>> probs = model.predict_proba("cheap meds available now")
>>> round(sum(probs.values()), 6)
1.0
>>> probs['spam'] > probs['ham']
True

An empty input text has no tokens, so predictions fall back to class priors. >>> empty_probs = model.predict_proba(“”) >>> round(empty_probs[‘spam’], 3), round(empty_probs[‘ham’], 3) (0.5, 0.5)

>>> NaiveBayesTextClassifier().predict_proba("hello")
Traceback (most recent call last):
...
ValueError: model has not been fitted yet.
alpha = 1.0
class_document_counts_: collections.Counter[str]
class_log_prior_: dict[str, float]
class_token_counts_: dict[str, collections.Counter[str]]
class_total_tokens_: collections.Counter[str]
classes_: list[str] = []
is_fitted_ = False
vocabulary_: set[str]
machine_learning.naive_bayes_text_classification.build_toy_dataset() tuple[list[str], list[str]]

Build a tiny text dataset for examples and quick local testing.

>>> texts, labels = build_toy_dataset()
>>> len(texts), len(labels)
(6, 6)
>>> sorted(set(labels))
['ham', 'spam']
machine_learning.naive_bayes_text_classification.classifier