Example code of naive bayes
- User
Example code of naive bayes
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Example code of naive bayes
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sure! I'll provide a simple example of implementing the Naive Bayes algorithm in Python. Naive Bayes is a probabilistic machine learning classifier based on Bayes' theorem, assuming that features are independent of each other. It's commonly used for classification tasks like spam detection or sentiment analysis.
For this example, I'll use the popular Iris dataset (a built-in dataset in scikit-learn) to classify iris flowers into one of three species based on their features. We'll use the Multinomial Naive Bayes classifier, which is suitable for discrete data, but note that for continuous data like Iris, Gaussian Naive Bayes is often more appropriate. I'll demonstrate Gaussian Naive Bayes here for simplicity.
Here's the complete code example:
# Import necessary libraries
from sklearn.datasets import load_iris # For loading the Iris dataset
from sklearn.model_selection import train_test_split # For splitting data
from sklearn.naive_bayes import GaussianNB # Gaussian Naive Bayes classifier
from sklearn.metrics import accuracy_score # For evaluating the model
# Step 1: Load the Iris dataset
iris = load_iris()
X = iris.data # Features (sepal length, sepal width, petal length, petal width)
y = iris.target # Labels (0: Setosa, 1: Versicolor, 2: Virginica)
# Step 2: Split the data into training and testing sets (e.g., 80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Step 3: Create and train the Gaussian Naive Bayes model
model = GaussianNB()
model.fit(X_train, y_train) # Fit the model to the training data
# Step 4: Make predictions on the test data
y_pred = model.predict(X_test) # Predictions
# Step 5: Evaluate the model's accuracy
accuracy = accuracy_score(y_test, y_pred) # Compare predicted vs actual labels
print(f"Accuracy of the Naive Bayes model: {accuracy * 100:.2f}%")
# Optional: Print the first few predictions for verification
print(f"Predicted labels: {y_pred[:5]}")
print(f"Actual labels: {y_test[:5]}")
pip install scikit-learn
naive_bayes_example.py) or a Jupyter Notebook.Accuracy of the Naive Bayes model: 100.00%
Predicted labels: [1 0 2 1 1]
Actual labels: [1 0 2 1 1]
(The exact accuracy may vary slightly due to random splitting.)MultinomialNB instead.from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
# Sample text data
documents = ['Chinese Beijing Chinese', 'Chinese Chinese Shanghai', 'Chinese Macao', 'Tokyo Japan Chinese']
labels = [0, 0, 0, 1] # 0: Chinese, 1: Not Chinese
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(documents)
model = MultinomialNB()
model.fit(X, labels)
predictions = model.predict(vectorizer.transform(['Chinese Chinese Tokyo Japan']))
print(predictions) # Output: [1]