Middle — Senior
What is a naive Bayes classifier?
sobes.tech AI
Answer from AI
Naive Bayes classifier is a simple probabilistic classifier based on applying Bayes' theorem with the assumption of independence between features.
Idea:
- Calculate the probability of an object belonging to a class, considering feature values.
- Assume all features are independent, which simplifies calculations.
Example: classifying emails as spam or not spam based on words in the text.
Advantages:
- Fast and easy to implement.
- Works well on large datasets.
Limitations:
- The assumption of feature independence is often not met, which can reduce accuracy.
Example in Python using sklearn:
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer
texts = ["spam message", "important meeting", "buy now", "project update"]
labels = [1, 0, 1, 0] # 1 - spam, 0 - not spam
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)
model = MultinomialNB()
model.fit(X, labels)
new_text = ["buy meeting"]
X_new = vectorizer.transform(new_text)
prediction = model.predict(X_new)
print(prediction) # Will output [1] - spam