from sklearn.naive_bayes import MultinomialNB from sklearn.feature_extraction.text import CountVectorizer # Load text data docs = ['This is a sample document.', 'Another document to test.', 'A third sample for testing.'] # Convert text to numerical features vectorizer = CountVectorizer() X = vectorizer.fit_transform(docs) # Create MultinomialNB classifier clf = MultinomialNB() # Train classifier clf.fit(X, [0, 1, 0]) # Predict class of new text data new_doc = ['This is another test document.'] new_X = vectorizer.transform(new_doc) predicted_class = clf.predict(new_X) print(predicted_class) # Output: [1]
from sklearn.datasets import load_digits from sklearn.naive_bayes import MultinomialNB # Load image data digits = load_digits() # Create MultinomialNB classifier clf = MultinomialNB() # Train classifier clf.fit(digits.data, digits.target) # Predict class of new image data new_data = digits.data[0].reshape(1, -1) predicted_class = clf.predict(new_data) print(predicted_class) # Output: [0]This example demonstrates how to use the MultinomialNB classifier to classify images of handwritten digits into 10 classes (0 to 9) based on the pixel values. In both examples, the MultinomialNB classifier is imported from the sklearn.naive_bayes module of the Scikit-Learn library for Python.