forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnaive_bayes_text_classification.py
More file actions
202 lines (163 loc) · 6.73 KB
/
Copy pathnaive_bayes_text_classification.py
File metadata and controls
202 lines (163 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""
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
"""
from __future__ import annotations
import re
from collections import Counter, defaultdict
from math import exp, log
class NaiveBayesTextClassifier:
"""
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.
"""
def __init__(self, alpha: float = 1.0) -> None:
if alpha <= 0:
raise ValueError("alpha must be greater than 0.")
self.alpha = alpha
self.classes_: list[str] = []
self.vocabulary_: set[str] = set()
self.class_document_counts_: Counter[str] = Counter()
self.class_token_counts_: dict[str, Counter[str]] = defaultdict(Counter)
self.class_total_tokens_: Counter[str] = Counter()
self.class_log_prior_: dict[str, float] = {}
self.is_fitted_ = False
@staticmethod
def _tokenize(text: str) -> list[str]:
"""
Split text into lowercase alphanumeric tokens.
>>> NaiveBayesTextClassifier._tokenize("Hello, NLP world!")
['hello', 'nlp', 'world']
"""
return re.findall(r"[a-z0-9']+", text.lower())
def fit(self, 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.
"""
if not texts:
raise ValueError("training data must not be empty.")
if len(texts) != len(labels):
raise ValueError("texts and labels must have the same length.")
self.classes_ = sorted(set(labels))
self.vocabulary_.clear()
self.class_document_counts_.clear()
self.class_token_counts_ = defaultdict(Counter)
self.class_total_tokens_.clear()
self.class_log_prior_.clear()
for text, label in zip(texts, labels):
if not isinstance(text, str) or not isinstance(label, str):
raise TypeError("texts and labels must contain strings only.")
tokens = self._tokenize(text)
self.class_document_counts_[label] += 1
self.class_token_counts_[label].update(tokens)
self.class_total_tokens_[label] += len(tokens)
self.vocabulary_.update(tokens)
total_documents = len(texts)
self.class_log_prior_ = {
label: log(self.class_document_counts_[label] / total_documents)
for label in self.classes_
}
self.is_fitted_ = True
def predict_proba(self, 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.
"""
if not self.is_fitted_:
raise ValueError("model has not been fitted yet.")
if not isinstance(text, str):
raise TypeError("text must be a string.")
tokens = self._tokenize(text)
vocabulary_size = len(self.vocabulary_)
log_posteriors: dict[str, float] = {}
for label in self.classes_:
log_prob = self.class_log_prior_[label]
token_counts = self.class_token_counts_[label]
denominator = self.class_total_tokens_[label] + self.alpha * vocabulary_size
for token in tokens:
count = token_counts[token]
log_prob += log((count + self.alpha) / denominator)
log_posteriors[label] = log_prob
max_log = max(log_posteriors.values())
exp_scores = {
label: exp(score - max_log) for label, score in log_posteriors.items()
}
normalizer = sum(exp_scores.values())
return {label: score / normalizer for label, score in exp_scores.items()}
def predict(self, 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'
"""
probabilities = self.predict_proba(text)
return max(probabilities, key=lambda label: probabilities[label])
def 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']
"""
texts = [
"buy cheap meds now",
"cheap meds available online",
"win cash prizes now",
"project meeting schedule attached",
"let us discuss the project timeline",
"team meeting moved to monday",
]
labels = ["spam", "spam", "spam", "ham", "ham", "ham"]
return texts, labels
if __name__ == "__main__":
import doctest
doctest.testmod()
sample_texts, sample_labels = build_toy_dataset()
classifier = NaiveBayesTextClassifier(alpha=1.0)
classifier.fit(sample_texts, sample_labels)
print("Prediction:", classifier.predict("cheap prizes available now"))
print("Prediction:", classifier.predict("team meeting about project timeline"))