Professional Documents
Culture Documents
Question 2
Question 2
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
accuracy_score(y_test, test_predictions)
0.9666666666666667