In this code we have used iris dataset of sklearn library. You can copy the code and execute it in juypter or PyCharm.
from sklearn.datasets import load_iris
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Load the iris dataset
iris = load_iris()
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=42)
# Train a naive Bayes classifier
clf = GaussianNB()
clf.fit(X_train, y_train)
# Use the trained classifier to make predictions on the test set
y_pred = clf.predict(X_test)
# Evaluate the accuracy of the classifier
acc = accuracy_score(y_test, y_pred)
print("Accuracy : {:.3f}".format(acc))
print(y_pred)
Accuracy:0.978
[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 2 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
0 0 0 2 1 1 0 0]
Advertisement
Advertisement