Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Classify the iris dataset using a Bayes classifier.

Divide the dataset into training and testing in the ratio 80:20. Use the functions from the
sklearn package. Assume the data follows a gaussian distribution. Display the training and testing accuracy, confusion matrix.

import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from sklearn.metrics import ConfusionMatrixDisplay, accuracy_score

X, y=load_iris(return_X_y=True, as_frame=True)
X_train, X_test, y_train, y_test=train_test_split(X, y, test_size=0.2, random_state=0)

clsf=GaussianNB()
clsf.fit(X_train, y_train)

▾ GaussianNB

GaussianNB()

train_predictions=clsf.predict(X_train)
test_predictions=clsf.predict(X_test)

accuracy_score(y_train, train_predictions)

0.95

ConfusionMatrixDisplay.from_predictions(y_train, train_predictions, display_labels=clsf.classes_);

accuracy_score(y_test, test_predictions)

0.9666666666666667

ConfusionMatrixDisplay.from_predictions(y_test, test_predictions, display_labels=clsf.classes_);


Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js

You might also like