Example code of naive bayes
- User
Example code of naive bayes
- Assistant
Below is an example of using the Gaussian Naive Bayes classifier from scikit-learn on the Iris dataset. You can run this code to see how the classifier performs:
import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.naive_bayes import GaussianNB from sklearn.metrics import accuracy_score # Load the Iris dataset iris = datasets.load_iris() X = iris.data # features y = iris.target # labels # Split the dataset into training (80%) and testing (20%) sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Create a Gaussian Naive Bayes classifier gnb = GaussianNB() # Train the classifier gnb.fit(X_train, y_train) # Make predictions on the test set y_pred = gnb.predict(X_test) # Calculate and print the accuracy accuracy = accuracy_score(y_test, y_pred) print(f"Accuracy: {accuracy * 100:.2f}%")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.