Model Pipeline - Naive Bayes for text
This pipeline shows how a Naive Bayes model learns to classify text messages into categories by counting word frequencies and using probabilities.
Jump into concepts and practice - no test required
This pipeline shows how a Naive Bayes model learns to classify text messages into categories by counting word frequencies and using probabilities.
Loss
0.7 |****
0.6 |****
0.5 |****
0.4 |****
0.3 |****
+----
1 5 Epochs
| Epoch | Loss ↓ | Accuracy ↑ | Observation |
|---|---|---|---|
| 1 | 0.65 | 0.7 | Initial training with basic word counts |
| 2 | 0.5 | 0.8 | Model learns better word-class associations |
| 3 | 0.4 | 0.85 | Improved smoothing and probability estimates |
| 4 | 0.35 | 0.88 | Model converges with stable accuracy |
| 5 | 0.33 | 0.89 | Final epoch with slight improvement |
['love this movie']?
from sklearn.feature_extraction.text import CountVectorizer from sklearn.naive_bayes import MultinomialNB texts = ['I love this movie', 'I hate this movie'] labels = ['positive', 'negative'] vectorizer = CountVectorizer() X = vectorizer.fit_transform(texts) model = MultinomialNB() model.fit(X, labels) new_text = vectorizer.transform(['love this movie']) prediction = model.predict(new_text) print(prediction[0])
from sklearn.feature_extraction.text import CountVectorizer from sklearn.naive_bayes import MultinomialNB texts = ['spam spam spam', 'ham ham ham'] labels = ['spam', 'ham'] vectorizer = CountVectorizer() X = vectorizer.fit_transform(texts) model = MultinomialNB() model.fit(X, labels) new_text = vectorizer.transform(['spam ham spam']) prediction = model.predict(new_text) print(prediction[0])The output is unexpected. What is the likely cause?